#2020最新的JDBC学习路线,最全的JDBC知识点/第六部分Apache-DBUtils实现CRU == 写在前面:本博客是JDBC第一部分,JDBC学习路线目录为:== [Ω ** JDBC总目录* ](https://blog.csdn.net/weixin_44392716/article/details/104302226) 欢迎大家一起讨论,如有错误,敬请指正*
@ [toc](目录)
公共静态无效rollbackAndClose(连接conn)引发SQLException-rollbackAndCloseQuietly(连接)-公共静态布尔值loadDriver(java.lang.String driverClassName):这一方装载并注册JDBC驱动程序,如果成功就返回true。使用该方法,你不需要捕捉这个异常ClassNotFoundException。
rsh, Object… params) throws SQLException:只支持INSERT语句,其中 rsh - The handler used to create the result object from the ResultSet of auto-generated keys. 返回值: An object generated by the handler.即自动生成的键值
/*增加数据*/
public void testInsert() throws SQLException {
//使用QueryRunner
QueryRunner queryRunner = new QueryRunner();
//获取链接
Connection conn = JDBCUtils.getConnectionDruid();
String sql = "INSERT INTO customers(name,email,birth)values(?,?,?)";
queryRunner.update(conn,sql,"蔡徐坤","[email protected]","1999-09-09");
//执行
JDBCUtils.closeResource(conn,null);
}
public void testDelete() throws Exception {
QueryRunner runner = new QueryRunner();
Connection conn = JDBCUtils.getConnection3();
String sql = "delete from customers where id < ?";
int count = runner.update(conn, sql,3);
System.out.println("删除了" + count + "条记录");
JDBCUtils.closeResource(conn, null);
}
/*查询一条数据*/
public void testQueryOne() throws SQLException {
QueryRunner queryRunner = new QueryRunner();
Connection conn = JDBCUtils.getConnectionDruid();
String sql = "SELECT id,name,email,birth FROM customers WHERE id=?";
BeanHandler<Customer> handler = new BeanHandler<>(Customer.class);
Customer customer = queryRunner.query(conn, sql, handler, 23);
JDBCUtils.closeResource(conn,null);
System.out.println(customer);
}
/*查询多条数据*/
public void testQueryTwo() throws SQLException {
QueryRunner queryRunner = new QueryRunner();
Connection conn = JDBCUtils.getConnectionDruid();
String sql = "SELECT id,name,email,birth FROM customers WHERE id";
BeanListHandler<Customer> handler = new BeanListHandler<>(Customer.class);
List<Customer> list = queryRunner.query(conn, sql, handler, 23);
JDBCUtils.closeResource(conn,null);
list.forEach(System.out::println);
}
的的java / *查询特殊数据* / 公共无效testQueryThree()抛出的的SQLException { QueryRunner queryRunner =新QueryRunner(); 连接康恩= JDBCUtils.getConnectionDruid(); 字符串SQL =“从客户中选择COUNT( *)条”; ScalarHandler处理程序=新的ScalarHandler(); println(计数); long count =(长)queryRunner。查询(conn,sql,handler);系统。出来。}