
1 package com.xs.db; 2 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 import java.sql.Connection; 6 import java.sql.DriverManager; 7 import java.sql.PreparedStatement; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 import java.sql.Statement; 11 import java.util.Properties; 12 13 public class DbUtil { 14 15 static String url ; 16 static String username; 17 static String pass ; 18 static String drivername ; 19 20 static FileInputStream stream = null; 21 static Properties properties = null; 22 23 static Connection con = null; 24 static PreparedStatement ps = null; 25 static ResultSet rs = null; 26 static { 27 try { 28 properties = new Properties(); 29 stream = new FileInputStream("dbinfo.properties"); 30 properties.load(stream); 31 url = properties.getProperty("url"); 32 username = properties.getProperty("username"); 33 pass = properties.getProperty("pass"); 34 drivername = properties.getProperty("drivername"); 35 con = DriverManager.getConnection(url, username, pass); 36 37 Class.forName(drivername); 38 } catch (Exception e) { 39 // TODO Auto-generated catch block 40 e.printStackTrace(); 41 } finally { 42 try { 43 stream.close(); 44 } catch (IOException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 stream = null; 49 } 50 } 51 52 public Connection getConnection() { 53 try { 54 con = DriverManager.getConnection(url, username, pass); 55 } catch (SQLException e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } 59 return con; 60 61 } 62 63 /** 64 * 如果只有一个sql语句,不需要考虑事务 select * from person where id=? and pass=?; 65 * 则parameters的值就是(1,"123") 利用占位符惊进行查找 66 * 67 * @param sql 68 * @param parameters 69 */ 70 public void executeUpdate(String sql, String[] parameters) { 71 72 try { 73 con = getConnection(); 74 ps = con.prepareStatement(sql); 75 if (parameters != null) { 76 for (int i = 0; i < parameters.length; i++) { 77 ps.setString(i + 1, parameters[i]); 78 } 79 } 80 ps.executeUpdate(); 81 } catch (SQLException e) { 82 // TODO Auto-generated catch block 83 e.printStackTrace(); 84 // 抛出异常 85 throw new RuntimeException(e.getMessage()); 86 } finally { 87 // 关闭函数 88 close(con, ps, rs); 89 } 90 91 } 92 93 /** 94 * 如果是多个sql语句的话,需要考虑事务啦 95 * 96 * @param sql 97 * @param parameters 98 */ 99 public void executeUpdate2(String sql[], String[][] parameters) { 100 101 try { 102 con = getConnection(); 103 con.setAutoCommit(false); 104 for (int i = 0; i < sql.length; i++) { 105 ps = con.prepareStatement(sql[i]); 106 if (parameters[i] != null) { 107 for (int j = 0; j < parameters.length; j++) { 108 ps.setString(j + 1, parameters[i][j]); 109 } 110 ps.executeUpdate(); 111 } 112 } 113 con.commit(); 114 } catch (SQLException e) { 115 // TODO Auto-generated catch block 116 e.printStackTrace(); 117 throw new RuntimeException(e.getMessage()); 118 } finally { 119 close(con, ps, rs); 120 } 121 } 122 123 public void close(Connection con, Statement st, ResultSet rs) { 124 if (st != null) { 125 try { 126 st.close(); 127 } catch (SQLException e) { 128 // TODO Auto-generated catch block 129 e.printStackTrace(); 130 } 131 st = null; 132 if (con != null) { 133 try { 134 con.close(); 135 } catch (SQLException e) { 136 // TODO Auto-generated catch block 137 e.printStackTrace(); 138 } 139 con = null; 140 } 141 if (rs != null) { 142 try { 143 rs.close(); 144 } catch (SQLException e) { 145 // TODO Auto-generated catch block 146 e.printStackTrace(); 147 } 148 rs = null; 149 } 150 } 151 } 152 153 }