1、 最古老的方法(经过 Driver 接口直接链接数据库)java
Driver dirver = new com.mysql.jdbc.Driver();
1 String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc"; 2 Properties info = new Properties(); 3 info.put("user", "root"); 4 info.put("password", "123456");
1 Connection connection = dirver.connect(url, info);
实例代码:mysql
1 @Test 2 public void testDriver() throws SQLException { 3 // 1. 建立一个 Driver 实现类的对象 4 Driver dirver = new com.mysql.jdbc.Driver(); 5 6 // 2. 准备连接数据库的基本信息:url, user, password 7 String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc"; 8 Properties info = new Properties(); 9 info.put("user", "root"); 10 info.put("password", "123456"); 11 12 // 3. 调用 Driver 借口的 connect(url, info) 获取数据库链接 13 Connection connection = dirver.connect(url, info); 14 System.out.println(connection); 15 }
2、编写一个通用的方法(经过Driver 接口实现)sql
1 String driverClass = null; 2 String jdbcUrl = null; 3 String user = null; 4 String password = null;
1 driver=com.mysql.jdbc.Driver 2 jdbcUrl=jdbc:mysql://127.0.0.1:3306/java_jdbc 3 user=root 4 password=123456
1 InputStream in = 2 getClass().getClassLoader().getResourceAsStream("jdbc.properties"); 3 Properties properties = new Properties(); 4 properties.load(in);
1 driverClass = properties.getProperty("driver"); 2 jdbcUrl = properties.getProperty("jdbcUrl"); 3 user = properties.getProperty("user"); 4 password = properties.getProperty("password");
1 Driver driver = (Driver) Class.forName(driverClass).newInstance();
1 Properties info = new Properties(); 2 info.put("user", user); 3 info.put("password", password); 4 Connection connection = driver.connect(jdbcUrl, info);
实例代码:数据库
1 public Connection getConnection() throws Exception { 2 String driverClass = null; 3 String jdbcUrl = null; 4 String user = null; 5 String password = null; 6 7 // 读取类路径下的jdbc.properties 文件 8 InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties"); 9 Properties properties = new Properties(); 10 properties.load(in); 11 12 driverClass = properties.getProperty("driver"); 13 jdbcUrl = properties.getProperty("jdbcUrl"); 14 user = properties.getProperty("user"); 15 password = properties.getProperty("password"); 16 17 // 经过反射建立 Driver 对象 18 Driver driver = (Driver) Class.forName(driverClass).newInstance(); 19 20 Properties info = new Properties(); 21 info.put("user", user); 22 info.put("password", password); 23 24 // 经过 Driver 的 connect 方法链接数据库. 25 Connection connection = driver.connect(jdbcUrl, info); 26 27 return connection; 28 } 29 30 @Test 31 public void testGetConnection() throws Exception { 32 System.out.println(getConnection()); 33 }
3、经过DriverManager 驱动管理类获取数据库链接ide
1).建立 properties 对象this
1 Properties properties = new Properties();
2).获取jdbc.properties 对应的输入流url
1 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
3).加载对应的输入流spa
1 properties.load(inputStream);
4).具体决定 user、password、url、driver四个字符串code
1 String driver = properties.getProperty("driver"); 2 String jdbcUrl = properties.getProperty("jdbcUrl"); 3 String user = properties.getProperty("user"); 4 String password = properties.getProperty("password");
2. 加载数据库驱动程序对象
1 Class.forName(driver);
3. 经过 Drivermanager 的 getConnection() 方法获取数据库链接
1 return DriverManager.getConnection(jdbcUrl, user, password);
实例代码:
1 @Test 2 public void testGetConnection23() throws Exception { 3 System.out.println(getConnection2()); 4 } 5 public Connection getConnection2() throws IOException, ClassNotFoundException, SQLException{ 6 7 Properties properties = new Properties(); 8 9 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"); 10 11 properties.load(inputStream); 12 13 String driver = properties.getProperty("driver"); 14 String jdbcUrl = properties.getProperty("jdbcUrl"); 15 String user = properties.getProperty("user"); 16 String password = properties.getProperty("password"); 17 18 Class.forName(driver); 19 20 return DriverManager.getConnection(jdbcUrl, user, password); 21 }
本文只是做者的笔记,只供参考,有错误欢迎指出!2017-10-28 11:14:03