(一)JDBC過程
1,注冊驅動com.mysql.jdbc.Driver
DriverManager.registerDriver(new Driver());
這種注冊方式有兩個缺點 :
解決方案: Class.forName(“com.mysql.jdbc.Driver”);
2,獲取數據庫連接java.sql.Connection
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/jtdb", "root","root");
3,獲取傳輸器java.sql.Statement
Statement st = conn.createStatement();
常用方法:
executeQuery() --- 用來執行查詢的SQL
executeUpdate() -- 用來執行增刪改的SQL
4,執行SQL。java.sql.ResultSet
String sql ="select * from user";
ResultSet rs = st.executeQuery(sql);
5,遍歷結果集
while(rs.next()){
//根據列的索引獲取第一列的數據
String id = rs.getString(1);
//根據列的索引獲取第二列的數據
String username = rs.getString(2);
//根據列的索引獲取第三列的數據
String password = rs.getString(3);
System.out.println(id+username+password);
}
6,釋放資源
在jdbc的開發中,釋放資源的過程是必須要保證完成的,jdbc資源非常稀缺。在釋放資源的過程中,會發生異常,這個時候為了保證資源一定會被釋放,需要把釋放資源的代碼放在finally語句塊中,來保證finally塊中的代碼一定會被執行到。
正着開,倒着關
rs.close();//釋放結果集資源
st.close();//釋放傳輸器資源
conn.close();//釋放連接資源
(二)對其進行優化和改進(JDBCUtils工具類)
因為每次 第一步注冊驅動 和 第二步獲取數據庫連接 和 第六步釋放資源 每次連接數據庫這三步都是一樣的。所以將其封裝成工具方法。對其進行調用就可以了,沒有必要每次進行重寫。
我們將注冊驅動不要寫死,並將jdbcUrl和root和password都放到屬性文件中,這樣可以靈活的修改。
編寫屬性文件注意事項:
位置:src new-file jdbc.properties 后綴名:properties
格式:key=value,想要獲取value時,通過get(key)來實現。
調用屬性文件中的內容,需要用到
ResourceBundle rb = ResourceBundle.getBundle("jdbc"); //標紅的為你的屬性文件的名字,后綴必須為properties!!!
1 public class JDBCUtils { 2 3 4 5 //1,私有化構造函數,外界無法直接創建對象 6 7 private JDBCUtils(){} 8 9 10 11 //終極優化:只加載一次屬性文件 12 13 static ResourceBundle rb = null; 14 15 static{ 16 17 //a,,,,讀取屬性文件 18 19 rb = ResourceBundle.getBundle("jdbc"); 20 21 } 22 23 24 25 26 27 //2,提供公共的,靜態的,getConnection 方法, 28 29 //用來給外界提供數據庫連接 30 31 public static Connection getConnection(){ 32 33 try { 34 35 36 37 //1,注冊驅動 38 39 //b,,獲取屬性文件里的內容 40 41 Class.forName(rb.getString("driverClass")); 42 43 44 45 //2,獲取數據庫連接 46 47 String url=rb.getString("jdbcUrl"); 48 49 String user=rb.getString("user"); 50 51 String password=rb.getString("password"); 52 53 54 55 Connection conn = 56 57 DriverManager.getConnection( 58 59 url, user, password); 60 61 62 63 return conn; 64 65 } catch (Exception e) { 66 67 e.printStackTrace(); 68 69 } 70 71 return null; 72 73 } 74 75 76 77 //3,提供公共的,靜態的,close方法, 78 79 //用來釋放資源 80 81 public static void close(ResultSet rs, 82 83 Statement st, 84 85 Connection conn){ 86 87 //釋放結果集資源 88 89 //非空判斷,防止空指針異常 90 91 if(rs!=null){ 92 93 try { 94 95 rs.close(); 96 97 } catch (SQLException e) { 98 99 e.printStackTrace(); 100 101 }finally{ 102 103 rs=null;//手動置空 104 105 } 106 107 } 108 109 //釋放傳輸器資源 110 111 //非空判斷,防止空指針異常 112 113 if(st!=null){ 114 115 try { 116 117 st.close(); 118 119 } catch (SQLException e) { 120 121 e.printStackTrace(); 122 123 }finally{ 124 125 st=null;//手動置空 126 127 } 128 129 } 130 131 //釋放數據庫連接資源 132 133 //非空判斷,防止空指針異常 134 135 if(conn!=null){ 136 137 try { 138 139 conn.close(); 140 141 } catch (SQLException e) { 142 143 e.printStackTrace(); 144 145 }finally{ 146 147 conn=null;//手動置空 148 149 } 150 151 } 152 153 154 155 156 157 }
測試代碼如下所示
1 public class TestUtils { 2 3 //單元測試方法:@Test + void 4 5 @Test 6 7 public void hello(){ 8 9 Connection conn = null;//聲明連接對象 10 11 Statement st = null;//聲明傳輸器對象 12 13 ResultSet rs = null;//聲明結果集對象 14 15 16 17 try { 18 19 //1,注冊驅動 2,獲取數據庫連接 20 21 conn = JDBCUtils.getConnection(); 22 23 24 25 // 3,獲取傳輸器java.sql.Statement 26 27 st = conn.createStatement(); 28 29 30 31 // 4,執行SQL。java.sql.ResultSet 32 33 String sql ="select * from user"; 34 35 rs = st.executeQuery(sql); 36 37 38 39 // 5,遍歷結果集 40 41 while(rs.next()){ 42 43 //根據列的索引獲取第一列的數據 44 45 // String id = rs.getString(1); 46 47 //根據列的名字獲取第一列的數據 48 49 String id = rs.getString("id"); 50 51 // int id1 = rs.getInt(1); 52 53 54 55 //根據列的索引獲取第二列的數據 56 57 String username = rs.getString(2); 58 59 60 61 //根據列的索引獲取第三列的數據 62 63 String password = rs.getString(3); 64 65 66 67 System.out.println(id+username+password); 68 69 70 71 } 72 73 74 75 } catch (SQLException e) { 76 77 e.printStackTrace(); 78 79 }finally{//保證資源一定會被釋放 80 81 // 6,釋放資源 82 83 JDBCUtils.close(rs, st, conn); 84 85 } 86 87 88 89 } 90 91 }