1 importjava.sql.Connection;2 importjava.sql.DriverManager;3 importjava.sql.PreparedStatement;4 importjava.sql.ResultSet;5 importjava.sql.Statement;6
7 /**
8 * 数据库工具类,负责完成打开、关闭数据库,执行查询或更新9 *@authorMKing10 *11 */
12 public classDbHelper {13 /**
14 * 数据库URL15 */
16 private static final String URL = "jdbc:mysql://localhost:3306/bookstore";17 /**
18 * 登录用户名19 */
20 private static final String USER = "root";21 /**
22 * 登录密码23 */
24 private static final String PASSWORD = "12345";25
26 private static Connection connection = null;27 private static Statement statement = null;28
29 private static DbHelper helper = null;30
31 static{32 try{33 Class.forName("com.mysql.jdbc.Driver");34 } catch(ClassNotFoundException e) {35 e.printStackTrace();36 }37 }38
39 private DbHelper() throwsException {40 connection =DriverManager.getConnection(URL, USER, PASSWORD);41 statement =connection.createStatement();42 }43
44 /**
45 * 返回单例模式的数据库辅助对象46 *47 *@return
48 *@throwsException49 */
50 public static DbHelper getDbHelper() throwsException {51 if (helper == null || connection == null ||connection.isClosed())52 helper = newDbHelper();53 returnhelper;54 }55
56 /**
57 * 执行查询58 *@paramsql 要执行的SQL语句59 *@return查询的结果集对象60 *@throwsException61 */
62 public ResultSet executeQuery(String sql) throwsException {63 if (statement != null) {64 returnstatement.executeQuery(sql);65 }66
67 throw new Exception("数据库未正常连接");68 }69
70 /**
71 * 执行查询72 *@paramsql 要执行的带参数的SQL语句73 *@paramargs SQL语句中的参数值74 *@return查询的结果集对象75 *@throwsException76 */
77 public ResultSet executeQuery(String sql, Object...args) throwsException {78 if (connection == null ||connection.isClosed()) {79 DbHelper.close();80 throw new Exception("数据库未正常连接");81 }82 PreparedStatement ps =connection.prepareStatement(sql);83 int index = 1;84 for(Object arg : args) {85 ps.setObject(index, arg);86 index++;87 }88
89 returnps.executeQuery();90 }91
92 /**
93 * 执行更新94 *@paramsql 要执行的SQL语句95 *@return受影响的记录条数96 *@throwsException97 */
98 public int executeUpdate(String sql) throwsException {99 if (statement != null) {100 returnstatement.executeUpdate(sql);101 }102 throw new Exception("数据库未正常连接");103 }104
105 /**
106 * 执行更新107 *@paramsql 要执行的SQL语句108 *@paramargs SQL语句中的参数109 *@return受影响的记录条数110 *@throwsException111 */
112 public int executeUpdate(String sql, Object...args) throwsException {113 if (connection == null ||connection.isClosed()) {114 DbHelper.close();115 throw new Exception("数据库未正常连接");116 }117 PreparedStatement ps =connection.prepareStatement(sql);118 int index = 1;119 for(Object arg : args) {120 ps.setObject(index, arg);121 index++;122 }123 returnps.executeUpdate();124 }125
126 /**
127 * 获取预编译的语句对象128 *@paramsql 预编译的语句129 *@return预编译的语句对象130 *@throwsException131 */
132 public PreparedStatement prepareStatement(String sql) throwsException {133 returnconnection.prepareStatement(sql);134 }135
136 /**
137 * 关闭对象,同时将关闭连接138 */
139 public static voidclose() {140 try{141 if (statement != null)142 statement.close();143 if (connection != null)144 connection.close();145 } catch(Exception e) {146 e.printStackTrace();147 } finally{148 helper = null;149 }150 }151 }