csdn博文地址:jdbc基礎 (二) 通過properties配置文件連接數據庫
上一篇描述了對mysql數據庫的簡單操作,下面來看一下開發中應該如何靈活應用。
因為jdbc對數據庫的驅動加載、連接獲取、釋放資源的代碼都是相同的,為了提高代碼的復用性,我們可以寫一個工具類,將數據庫驅動加載、獲取連接、資源釋放的代碼封裝起來。同時,為了提高工具類的靈活性,可以將數據庫的驅動、url、用戶名、密碼等信息以鍵值對的形式存放在properties文件中,工具類初始化時從配置文件中讀取所要連接數據庫的信息。當需要更改連接的數據庫時,只需要更改配置文件即可,而不必改寫工具類的代碼。
下面是工具類代碼的實現:
1 package com.cream.ice.jdbc; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.sql.Connection; 6 import java.sql.DriverManager; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 import java.util.Properties; 11 12 public class JdbcUtils { 13 14 private static String driverName; 15 private static String url; 16 private static String user; 17 private static String password; 18 19 /* 20 * 靜態代碼塊,類初始化時加載數據庫驅動 21 */ 22 static { 23 try { 24 // 加載dbinfo.properties配置文件 25 InputStream in = JdbcUtils.class.getClassLoader() 26 .getResourceAsStream("dbinfo.properties"); 27 Properties properties = new Properties(); 28 properties.load(in); 29 30 // 獲取驅動名稱、url、用戶名以及密碼 31 driverName = properties.getProperty("driverName"); 32 url = properties.getProperty("url"); 33 user = properties.getProperty("user"); 34 password = properties.getProperty("password"); 35 36 // 加載驅動 37 Class.forName(driverName); 38 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } catch (ClassNotFoundException e) { 42 e.printStackTrace(); 43 } 44 } 45 46 /* 47 * 獲取連接 48 */ 49 public static Connection getConnection() throws SQLException { 50 51 return DriverManager.getConnection(url, user, password); 52 53 } 54 55 /* 56 * 釋放資源 57 */ 58 public static void releaseResources(ResultSet resultSet, 59 Statement statement, Connection connection) { 60 61 try { 62 if (resultSet != null) 63 resultSet.close(); 64 } catch (SQLException e) { 65 e.printStackTrace(); 66 } finally { 67 resultSet = null; 68 try { 69 if (statement != null) 70 statement.close(); 71 } catch (SQLException e) { 72 e.printStackTrace(); 73 } finally { 74 statement = null; 75 try { 76 if (connection != null) 77 connection.close(); 78 } catch (SQLException e) { 79 e.printStackTrace(); 80 } finally { 81 connection = null; 82 } 83 } 84 } 85 86 } 87 88 }
這里dbinfo.properties文件中信息如下,讀者可自行更改:
1 driverName=com.mysql.jdbc.Driver 2 url=jdbc:mysql://localhost:3306/jdbc 3 user=root 4 password=123456
這里我們來舉個例子使用工具類。我們寫一個類JdbcCURD實現對特定數據庫的增刪改查操作,並在main函數中使用。
JdbcCURD.java代碼如下:
1 package com.cream.ice.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 public class JdbcCURD { 9 10 private Connection connection; 11 private Statement statement; 12 private ResultSet resultSet; 13 14 //更新操作 15 public void update(String sql) { 16 try { 17 connection = JdbcUtils.getConnection(); 18 statement = connection.createStatement(); 19 //可執行創建、修改、刪除表,添加、刪除、修改元組以及查詢sql語句 20 statement.execute(sql); 21 } catch (SQLException e) { 22 e.printStackTrace(); 23 } finally { 24 JdbcUtils.releaseResources(resultSet, statement, connection); 25 } 26 } 27 28 //查詢操作 29 public void Query(String sql) { 30 try { 31 connection = JdbcUtils.getConnection(); 32 statement = connection.createStatement(); 33 resultSet = statement.executeQuery(sql); 34 35 while(resultSet.next()){ 36 System.out.println("name:"+resultSet.getString("name")); 37 System.out.println("id:"+resultSet.getString("Tid")); 38 } 39 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 } finally { 43 JdbcUtils.releaseResources(resultSet, statement, connection); 44 } 45 } 46 47 //添加操作 48 public void addElement(String sql) { 49 update(sql); 50 } 51 52 //刪除操作 53 public void removeElement(String sql) { 54 update(sql); 55 } 56 57 //創建一個表 58 public void createTable(String sql){ 59 update(sql); 60 } 61 62 //刪除一個表 63 public void dropTable(String sql){ 64 update(sql); 65 } 66 67 }
我們來寫一個main函數來測試:
1 package com.cream.ice.jdbc; 2 3 import java.sql.SQLException; 4 5 public class JdbcTest { 6 7 public static void main(String[] args) throws ClassNotFoundException, 8 SQLException { 9 10 JdbcCURD curd=new JdbcCURD(); 11 12 String sql = null; 13 14 //添加表Teacher 15 sql="create table Teacher (Tid char(9) primary key,name char(9) unique)"; 16 curd.createTable(sql); 17 18 //添加元組 19 sql = "insert into Teacher (Tid,name) values ('0001','Tom')"; 20 curd.addElement(sql); 21 22 //查詢Teacher表 23 sql="select * from Teacher"; 24 curd.Query(sql); 25 26 //刪除元組 27 sql="delete from Teacher where Tid='0001'"; 28 curd.removeElement(sql); 29 30 //刪除表Teacher 31 sql="drop table Teacher"; 32 curd.dropTable(sql); 33 } 34 35 }
經測試,將在控制台輸出下列信息:
1 name:Tom 2 id:0001
與上一篇中對數據庫的操作相比,從配置文件中讀取要連接數據庫的信息,大大提高了代碼的復用性以及靈活性,省去了當更改數據庫時還要更改代碼的麻煩。