@Test
public void testQueryMany() throws SQLException {//返回结果是多行的情况
//1.先得到连接(Druid)
Connection connetion = JDBCUtilsByDruid.getConnetion();
//2.使用DBUtils类和接口,先引入DBUtils的jar文件,加入到本Project
//3.创建QueryRunner
QueryRunner queryRunner = new QueryRunner();
//4.就可以执行相关的方法,返回ArrayList结果集
String sql = "select * from actor";
List list = queryRunner.query(connetion, sql, new BeanListHandler<>(Actor.class));
System.out.println("输出集合信息");
for(Actor actor:list){
System.out.println(actor);
}
//释放资源
//底层得到的reusltSet,会在query关闭,还会关闭PreparedStatement,就传入connection就行了
JDBCUtilsByDruid.close(null,null,connetion);
}
@Test
public void testQuerySingle() throws SQLException {
//得到连接(Druid)
Connection connetion = JDBCUtilsByDruid.getConnetion();
QueryRunner queryRunner = new QueryRunner();
String sql = "select * from actor where id = ?";
//因为我们知道返回的是单行记录,对应的是单个对象,而不是集合了,使用的Handler是BeanHandler,底层不会封装成集合了。
Actor actor = queryRunner.query(connetion, sql, new BeanHandler<>(Actor.class), 2);
System.out.println(actor);
JDBCUtilsByDruid.close(null,null,connetion);
}
这里要注意的是:我们知道返回的是单行记录,对应的是单个对象,而不是集合了,使用的Handler是BeanHandler,底层不会封装成集合了。
如果我们插入一个不存在的id,那么就会返回一个空,比如说我们传入一个10,那么如果是null的话,我们也可以写业务逻辑。
单行单列意思就是比如说我给id=2的信息,只查出这个人的名字。
//使用apache-DBUtils工具类+Druid完成查询结果是单行单列(比如我想查id为2的name是什么)
@Test
public void testScalar() throws SQLException {
Connection connetion = JDBCUtilsByDruid.getConnetion();
QueryRunner queryRunner = new QueryRunner();
//返回单行单列情况,返回的就是object,可能为空
String sql = "select name from actor where id = ?";
//因为返回的是一个对象,使用的handler,使用的是ScalarHandler
Object object = queryRunner.query(connetion, sql, new ScalarHandler<>(), 2);
System.out.println(object);
JDBCUtilsByDruid.close(null,null,connetion);
}
//演示apache-dbutils+druid 完成dml(update,insert,delete)
@Test
public void testDML() throws SQLException {
Connection connetion = JDBCUtilsByDruid.getConnetion();
QueryRunner queryRunner = new QueryRunner();
String sql="update actor set name=? where id=?";
//(1) 执行DML操作是queryRunner.update
//(2) 返回值是受影响的行数
int affectedRow = queryRunner.update(connetion, sql, "张三丰", 4);
System.out.println(affectedRow>0?"执行成功":"执行没有影响到表");
JDBCUtilsByDruid.close(null,null,connetion);
}
@Test
public void testDML() throws SQLException {
Connection connetion = JDBCUtilsByDruid.getConnetion();
QueryRunner queryRunner = new QueryRunner();
String sql="insert into actor values(null,?,?,?,?)";
//(1) 执行DML操作是queryRunner.update
//(2) 返回值是受影响的行数
int affectedRow = queryRunner.update(connetion, sql, "林青霞","女","1966-10-10","116");
System.out.println(affectedRow>0?"执行成功":"执行没有影响到表");
JDBCUtilsByDruid.close(null,null,connetion);
}
@Test
public void testDML() throws SQLException {
Connection connetion = JDBCUtilsByDruid.getConnetion();
QueryRunner queryRunner = new QueryRunner();
String sql="delete from actor where id=?";
//(1) 执行DML操作是queryRunner.update
//(2) 返回值是受影响的行数
int affectedRow = queryRunner.update(connetion, sql,5);
System.out.println(affectedRow>0?"执行成功":"执行没有影响到表");
JDBCUtilsByDruid.close(null,null,connetion);
}