數據庫操作類,將所有連接數據庫的配置信息以及基本的CRUD操作封裝在一個類里,方便項目里使用,將連接數據庫的基本信息放在配置文件 "dbinfo.properties" 中,通過類加載器調用(也可以通過ServletContext調用配置文件,或者配置在web.xml里通過ServletConfig調用),需要修改數據庫連接信息時,只需修改配置文件即可。
1 package com.latiny.db; 2 3 import java.io.*; 4 import java.sql.*; 5 import java.util.ArrayList; 6 import java.util.Properties; 7 8 /* 9 * 數據庫操作類 10 */ 11 12 public class DBUtil { 13 14 //定義需要的變量 15 private static String driver =null; 16 private static String url =null; 17 private static String user=null; 18 private static String password=null; 19 20 private static Connection conn; 21 //使用PreparedStatment可以防止sql注入 22 private static PreparedStatement ps; 23 private static ResultSet rs; 24 private static CallableStatement cs; 25 26 //讀配置文件 27 private static Properties pp=null; 28 private static InputStream fis=null; 29 30 31 //加載驅動,只需要執行一次 32 static 33 { 34 try 35 { 36 pp = new Properties(); 37 38 //當我們使用java web的時候,讀取文件要使用類加載器 39 fis = DBUtil.class.getClassLoader().getResourceAsStream("dbinfo.properties"); 40 41 pp.load(fis); 42 driver = pp.getProperty("DRIVER"); 43 url = pp.getProperty("URL"); 44 user = pp.getProperty("USER"); 45 password = pp.getProperty("PASSWORD"); 46 47 // 1 加載驅動 48 Class.forName(driver); 49 50 } 51 catch(Exception e) 52 { 53 e.printStackTrace(); 54 } 55 finally 56 { 57 try 58 { 59 fis.close(); 60 } catch (IOException e) { 61 // TODO Auto-generated catch block 62 e.printStackTrace(); 63 } 64 fis = null; 65 } 66 } 67 68 /* 69 * 獲取Connection連接 70 */ 71 public static Connection getConn() 72 { 73 try 74 { 75 // 2 獲取數據庫連接 76 conn = DriverManager.getConnection(url, user, password); 77 } 78 catch (SQLException e) 79 { 80 // TODO Auto-generated catch block 81 e.printStackTrace(); 82 } 83 84 return conn; 85 } 86 87 /* 88 * 直接返回rs結果,此方法不能關閉rs,因為后面調用它的類還會用到,如果關閉則不能正常使用 89 */ 90 public static ResultSet queryResult(String sql, String[] parameters) 91 { 92 try 93 { 94 conn = getConn(); 95 // 3 創建Statement對象 96 ps = conn.prepareStatement(sql); 97 // 4 給問號賦值,即給sql語句的條件參數賦值如果需要的話 98 if(parameters!=null) 99 { 100 for(int i=1; i<=parameters.length; i++) 101 { 102 ps.setString(i, parameters[i-1]); 103 } 104 } 105 106 // 5 執行sql獲取返回結果 107 rs = ps.executeQuery(); 108 } 109 catch (SQLException e) 110 { 111 // TODO Auto-generated catch block 112 e.printStackTrace(); 113 } 114 115 return rs; 116 } 117 118 /* 119 * 將rs結果封裝成ArrayList,然后可以關閉rs,節省數據庫訪問資源 120 */ 121 public static ArrayList queryResult2(String sql, String[] parameters) 122 { 123 ArrayList al = new ArrayList(); 124 125 try 126 { 127 //2 獲取數據庫連接 128 conn = getConn(); 129 //3 創建Statement對象 130 ps = conn.prepareStatement(sql); 132 //4 給問號賦值,即給sql語句的條件參數賦值如果需要的話 133 if(parameters!=null) 134 { 135 for(int i=1; i<=parameters.length; i++) 136 { 137 ps.setString(i, parameters[i-1]); 138 } 139 } 140 141 //5 執行sql語句獲取返回結果 142 rs = ps.executeQuery(); 143 144 //獲取rs的結構 145 ResultSetMetaData rsmd = rs.getMetaData(); 146 //獲取查詢語句的列數 147 int column = rsmd.getColumnCount(); 148 149 while(rs.next()) 150 { 151 //對象數組,存儲一行數據 152 Object[] objs = new Object[column]; 153 for(int i=0; i<objs.length; i++) 154 { 155 objs[i] = rs.getObject(i+1); 156 } 157 al.add(objs); 158 } 159 160 } 161 catch (SQLException e) 162 { 163 // TODO Auto-generated catch block 164 e.printStackTrace(); 165 } 166 finally 167 { 168 //關閉資源 169 close(rs, ps, conn); 170 } 171 172 return al; 173 } 174 175 //調用存儲過程,帶輸入輸出參數的 176 public static CallableStatement callProcedure(String sql, String[] inputPara, Integer[] outputPara) 177 { 178 179 try 180 { 181 conn = getConn(); 182 cs = conn.prepareCall(sql); 183 for(int i=0; inputPara!=null && i<inputPara.length; i++) 184 { 185 cs.setObject(i+1, inputPara[i]); 186 } 187 188 //給output參數賦值 189 for(int j=0; outputPara!=null && j<outputPara.length; j++) 190 { 191 cs.registerOutParameter(inputPara.length+1+j, outputPara[j]); 192 } 193 194 cs.execute(); 195 196 } catch (SQLException e) { 197 // TODO Auto-generated catch block 198 e.printStackTrace(); 199 } 200 finally 201 { 202 close(rs, ps, conn); 203 } 204 205 return cs; 206 207 } 208 209 //update, insert, delete 210 public static Integer updateData(String sql, String[] parameters) 211 { 212 int result = 0; 213 try 214 { 215 conn = getConn(); 216 ps = conn.prepareStatement(sql); 217 if(parameters!=null) 218 { 219 for(int i=0; i<parameters.length; i++) 220 { 221 ps.setObject(i+1, parameters[i]); 222 } 223 } 224 225 //執行executeUpdate並且返回受影響的行數 226 result = ps.executeUpdate(); 227 228 } 229 catch(Exception e) 230 { 231 e.printStackTrace(); 232 } 233 finally 234 { 235 close(rs, ps, conn); 236 } 237 238 return result; 239 } 240 241 //關閉對應的數據庫連接資源 242 public static void close(ResultSet rs1, PreparedStatement ps1, Connection conn1) 243 { 244 245 try 246 { 247 if(rs1!=null) 248 { 249 rs1.close(); 250 } 251 if(ps1!=null) 252 { 253 ps1.close(); 254 } 255 if(conn1!=null) 256 { 257 conn1.close(); 258 } 259 260 } catch (SQLException e) { 261 // TODO Auto-generated catch block 262 e.printStackTrace(); 263 } 264 265 } 266 }
dbinfo.properties文件配置信息
DRIVER=com.mysql.jdbc.Driver URL=jdbc:mysql://localhost:3306/servlet USER=latiny PASSWORD=123456
