废话不多说,先直接上代码:
package com.study.jdbc.jdbc.create; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Driver; import java.sql.SQLException; import java.util.Properties; public class JdbcConnection { public static void main(String[] args) throws Exception { //Connection con = getSqlConnection(); JdbcConnection con = new JdbcConnection(); //Connection connection = con.getConnection(); Connection connection = con.getMysqlConnection(); System.out.println(con); } public static Connection getSqlConnection() { //1.JDBC所需的四个参数4个准备:user,password,url,driverClass(连接数据库所需的驱动) String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "root"; String driverClass = "com.mysql.jdbc.Driver";//这个是mysql-connector-java-5.1.8-bin.jar包中的,不是JDK自带的Driver。 Connection connection = null; try { //2.加载JDBC驱动程序 Class.forName(driverClass); //3.创建数据库连接:三种连接方案 //方案一:connection = DriverManager.getConnection(jdbcUrl); //方案二:connection = DriverManager.getConnection(url,user,password); //方案三:connection = DriverManager.getConnection(url,info); Properties info = new Properties(); info.setProperty("user",user);//property的key必须是"user" info.setProperty("password",password);//property的key必须是"password" connection = DriverManager.getConnection(url,info); return connection; } catch (ClassNotFoundException e) { //log.error("Not found driver class, load driver failed."); } catch (SQLException e) { //log.error("create db connection error,",e); } return null; } public Connection getConnection() throws Exception{ //1.准备连接数据库的4个字符串 //1).创建Properties对象 Properties info = new Properties(); //2).获取jdbc.properties对应的输入流 InputStream in = getClass().getClassLoader().getResourceAsStream("com/conf/properties/jdbc-conf.properties"); //3).加载2)对应的输入流 info.load(in); //4).具体决定user、password、url、driver这4个字符串 String driverClass = info.getProperty("driverClass"); String jdbcUrl = info.getProperty("jdbcUrl"); String user = info.getProperty("user"); String password = info.getProperty("password"); /** * Driver实现类中注册的静态代码块: * * static { * try { * DriverManager.registerDriver(new Driver()); * } catch (SQLException var1) { * throw new RuntimeException("Can't register driver!"); * } * } */ //2.加载数据库驱动程序(对应的Driver实现类中有注册的静态代码块) Class.forName(driverClass); //3.通过DriverManager的getConnection()方法获取Connection连接 return DriverManager.getConnection(jdbcUrl,user,password); } public Connection getMysqlConnection() throws Exception { InputStream in = getClass().getClassLoader().getResourceAsStream("com/conf/properties/jdbc-conf.properties"); Properties info = new Properties(); info.load(in); String driverClass = info.getProperty("driverClass"); String jdbcUrl = info.getProperty("jdbcUrl"); String user = info.getProperty("user"); String password = info.getProperty("password"); info.put("user",user); info.put("password",password); Driver driver = (Driver)Class.forName(driverClass).newInstance(); Connection connection = driver.connect(jdbcUrl, info); return connection; } }
连接属性放在配置文件中,方便统一管理:jdbc-conf.properties
jdbcUrl=jdbc:mysql://localhost:3306/test user=root password=root driverClass=com.mysql.jdbc.Driver
来一个驱动Driver的源码:
package com.mysql.jdbc; import java.sql.DriverManager; import java.sql.SQLException; public class Driver extends NonRegisteringDriver implements java.sql.Driver { public Driver() throws SQLException { } static { try { DriverManager.registerDriver(new Driver()); } catch (SQLException var1) { throw new RuntimeException("Can't register driver!"); } } }
使用driver实例获取connection(简称方法一)和使用DriverManager获取connection(简称方法二)方法比较:
1、方法一中要(Driver)Class.forName(driverClass).newInstance();实例化返回驱动Driver的一个实例对象,然后调用方法connect(jdbcUrl, info);
2、在方法二中,只需要手动的加载数据库驱动程序就行,不需要再手动的去实例化驱动了。然后,通过DriverManager的getConnection(jdbcUrl,user,password)方法获取Connection连接。
3、这里不再手动的去实例化驱动是有原因的,因为在加载驱动时,Driver有一段static代码,这段代码在类加载时会自动执行,所以就把自己注册到DriverManager的registerDriver。
4、而registerDriver是通过遍历registerDrivers中所有已经注册的驱动来常识获取连接。查看源码就能看出这里是怎么实现了。
DriverManager的获取Connection的部分源码:
for(DriverInfo aDriver : registeredDrivers) { // If the caller does not have permission to load the driver then // skip it. if(isDriverAllowed(aDriver.driver, callerCL)) { try { println(" trying " + aDriver.driver.getClass().getName()); Connection con = aDriver.driver.connect(url, info); if (con != null) { // Success! println("getConnection returning " + aDriver.driver.getClass().getName()); return (con); } } catch (SQLException ex) { if (reason == null) { reason = ex; } } } else { println(" skipping: " + aDriver.getClass().getName()); } }
可进一步参考:java中连接mysql时添加Class.forName("com.mysql.jdbc.Driver")的作用?
关于Class.forName(“com.mysql.jdbc.Driver”)