package DButils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;
import pool.Customers;
import pool.DruidConnection;
import pool.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
/**
* @author
* @date 2021/4/8 15:24
* 向里边传递的handle返回值就是query的返回值
*/
public class QueryRunnerTest {
/**
* @Author 小吕
* @Description 没有返回结果的sql语句操作
* @Date 2021/4/8 16:21
**/
@Test
public void update() throws SQLException {
QueryRunner runner = new QueryRunner();
DruidConnection druidConnection = new DruidConnection();
Connection connection = druidConnection.getConnection();
String sql="UPDATE customers SET name =? WHERE id=?";
int i = runner.update(connection, sql, "汪峰", 1);
System.out.println(i);
}
/**
* @Author 小吕
* @Description 查询并返回单个结果
* @Date 2021/4/8 16:20
**/
@Test
public void select() throws SQLException {
QueryRunner runner = new QueryRunner();
DruidConnection druidConnection = new DruidConnection();
Connection connection = druidConnection.getConnection();
String sql="SELECT id,name,email,birth FROM customers where id=?";
BeanHandler<Customers> handler=new BeanHandler<Customers>(Customers.class);
Customers i = runner.query(connection,sql,handler,1);
System.out.println(i);
}
/**
* @Author 小吕
* @Description 查询并返回列表
* @Date 2021/4/8 16:20
**/
@Test
public void select2() throws SQLException {
QueryRunner runner = new QueryRunner();
DruidConnection druidConnection = new DruidConnection();
Connection connection = druidConnection.getConnection();
String sql="SELECT id,name,email,birth FROM customers where id<?";
BeanListHandler<Customers> handler= new BeanListHandler<>(Customers.class);
List<Customers> i = runner.query(connection,sql,handler,19);
i.forEach(System.out::println);
JDBCUtils.closeResource(connection,null);
}
/**
* @Author 小吕
* @Description 查询特殊值操作
* @Date 2021/4/8 16:20
**/
@Test
public void select3() throws SQLException {
QueryRunner runner = new QueryRunner();
DruidConnection druidConnection = new DruidConnection();
Connection connection = druidConnection.getConnection();
String sql="SELECT max(birth)FROM customers";
ScalarHandler scalarHandler=new ScalarHandler();
Object date = runner.query(connection,sql,scalarHandler);
System.out.println((Date)date);
JDBCUtils.closeResource(connection,null);
}
/**
* @Author 小吕
* @Description 关于handle的理解
* @Date 2021/4/8 16:29
**/
@Test
public void select4() throws SQLException {
QueryRunner runner = new QueryRunner();
//获取druid链接
DruidConnection druidConnection = new DruidConnection();
Connection connection = druidConnection.getConnection();
String sql="SELECT id,name,email,birth FROM customers where id=?";
/**
* 自定义handle类
*/
ResultSetHandler<Customers> handler=new ResultSetHandler<Customers>() {
@Override
public Customers handle(ResultSet resultSet) throws SQLException {
//方法体内写什么下边query就返回什么
if (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String email = resultSet.getString("email");
java.sql.Date birth = resultSet.getDate("birth");
return new Customers(id,name,email,birth);
}
return null;
}
};
Customers customers = runner.query(connection,sql,handler,19);
System.out.println(customers);
DbUtils.close(connection);
}
}
DbUtils增删改查的实现
最新推荐文章于 2023-03-20 16:19:58 发布