大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
DBUtils
commons-dbutils是Apache组织提供的一个开源JDBC工具类库,封装了针对于数据库的增删改查操作
API
- QueryRunner
- ResulSetHandler
- Dbutils
插入举例
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JBBCUtils.getConnections3();
String sql = "insert into customers(name,emial,birth) values(?,?,?)";
int insertCount = runner.update(conn,sql,"蔡旭坤","caixukun@163.com","1993-03-01");
System.out.println(insertCount);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.closeResource(conn,null);
}
更新
public static void main(String[] args) {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JBBCUtils.getConnections3();
String sql = "select id,name,email,birth from customers where id < ?";
BeanListHandler<Customer> handler = new BeanListHandler<Cunstomer>(customer.class);
List<Customer> query = runner.query(conn.sql, handler, 23);
System.out.println(insertCount);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.closeResource(conn,null);
}
}
查询表中特殊值
public static void main(String[] args) {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JBBCUtils.getConnections3();
String sql = "select count(*) from customers";
ScalarHandler handler = new ScalarHandler();
Long count = (Long)runner.query(conn.sql, handler);
System.out.println(count);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.closeResource(conn,null);
}
}
自定义ResultSetHandler
public static void main(String[] args) {
Connection conn = null;
try {
QueryRunner runner = new QueryRunner();
conn = JBBCUtils.getConnections3();
String sql = "select id,name,email,birth from customers where id = ?";
ResultSetHandler<Customers> resultSetHandler = new ResultSetHandler<Customers>() {
@Override
public Customers handle(ResultSet rs) throws SQLException {
if(resultSet.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
String email = rs.getString(3);
Date birth = rs.getDate(4);
Customer customer = new Cunstomer(id,name,email,birth);
return customer;
}
return null;
}
};
Long count = (Long)runner.query(conn.sql, handler);
System.out.println(count);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.closeResource(conn,null);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/168938.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...