e.printStackTrace();
}
}
public static Connection getCon() {
if (ds == null) {
try {
ds = (DruidDataSource) DruidDataSourceFactory.createDataSource(pt);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
return ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void closeCon(Connection con) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
具体使用模板:
public static void main(String[] args) throws Exception{
//使用自己的工具类,获取数据库的连接对象
Connection conn = MyJDBCUtils.getConnection();
//创建QueryRunner类对象
QueryRunner qr = new QueryRunner();
String sql = “UPDATE gjp_ledger set money=? , ldesc=? where lid=?”;
Object[] params = {998,“买钻石”,3};
int result = qr.update(conn, sql, params);
System.out.println(result);
DbUtils.close(conn);
}
二.QueryRunner类获取查询结果集ResultSetHandler
===================================================================================================
1.常用Handler
- ArrayHandler 将结果集第一行转成对象数组
public static void main(String[] args) throws Exception{
Connection conn = MyJDBCUtils.getConnection();
String sql = “SELECT * FROM gjp_ledger where lid=?”;
//结果集处理方式&#