JDBC连接数据库流程
- 注册驱动
jdbc:mysql://localhost:3306/DatabaseName
jdbc | 协议 |
---|---|
mysql | 子协议 |
localhost:3306/DatabaseName | 子名称 |
驱动程序
1.JDBC URL的标准由三部分组成,各部分间用冒号分隔。
2. jdbc:子协议:子名称
3. 协议:JDBC URL中的协议总是jdbc
4. 子协议:子协议用于标识一个数据库驱动程序
5. 子名称:一种标识数据库的方法。子名称可以依不同的子协议而变化,用子名称的目的是为了定位数据库提供足够的信息。包含主机名(对应服务端的ip地址),端口号,数据库名
- 建立连接(Connection)
- 可以调用 DriverManager 类的 getConnection() 方法建立到数据库的连接
- User,password可以用“属性名=属性值”方式告诉数据库;
- JDBC URL用于标识一个被注册的驱动程序,驱动程序管理器通过这个 URL 选择正确的
- Driver 接口:
4.1java.sql.Driver 接口是所有 JDBC 驱动程序需要实现的接口。这个接口是提供给数据库厂商使用的,不同数据库厂商提供不同的实现
4.2在程序中不需要直接去访问实现了 Driver 接口的类,而是由驱动程序管理
- 创建执行SQL的语句(Statement)
- 执行语句
- 处理执行结果(ResultSet)
- 释放资源
方式一:
- 简介:加载 JDBC 驱动需调用 Class 类的静态方法 forName(),向其传递要加载的 JDBC 驱动的类名
- 实例:
public void testConn1() throws SQLException {
// 获取Driver实现类对象
Driver driver = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/test";
// 将用户名和密码封装在Properties中
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "abc123");
Connection conn = driver.connect(url, properties);
}
方式二:
为了使程序解耦合,将第三方API分离,使用Class.forName()反射
public void testConn2() throws Exception {
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
String url = "jdbc:mysql://localhost:3306/test";
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "abc123");
Connection conn = driver.connect(url, info);
}
方式三:
使用DriverManager
public void testConn3() throws Exception {
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "abc123";
// 注册驱动
DriverManager.registerDriver(driver);
Connection conn = DriverManager.getConnection(url, user, password);
}
方式四:
在mysql的Driver实现类中,已经声明了如下的操作:
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
} }
故可以省略注册操作
public void testConne4() throws Exception {
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "abc123";
Class.forName("com.mysql.jdbc.Driver");//加载操作
Connection conn = DriverManager.getConnection(url, user, password);
}
方式五:
将基本信息存入properties文件中方便进行不同数据库的切换
- 实现数据与代码的分离
- 避免修改单个配置文件信息影响整体程序的打包
jdbc.properties
user=root
password=123456
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
driverClass=com.mysql.jdbc.Driver
Oracle 数据库连接 | jdbc:oracle:thin:@localhost:1521:DatabaseName |
---|---|
SQLServer 数据库连接 | jdbc:microsoft:sqlserver//localhost:1433; DatabaseName=sid |
MYSQL 数据库连接 | jdbc:mysql://localhost:3306/DatabaseName |
public void getConn5() throws Exception{
//通过输入流读取配置文件的信息
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
//2.加载驱动
Class.forName(driverClass);
//3.获取连接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
结语:(送给每一个在努力前行的人)
以梦为马,不负韶华
流年笑掷,未来可期