DBUtils——用来完成jdbc的CRUD

1.DBUtils是java编程中的数据库操作实用工具,小巧简单实用,它封装了对JDBC的操作,可以少写代码

2.DBUtils的三个核心功能:

  QueryRunner中提供对sql语句操作的API;

  ResultSetHandler接口用于定义select操作后,怎样封装结果集;

  DbUtils类里面定义了关闭资源与事务处理的方法

3.DBUtils的简单demo:

  

package c3p0;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

public class TestDBUtils {

@Test
public void testAdd(){
//1.创建核心类QueryRunner
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
//2.编写sql语句
String sql = "insert into user values(?, ?, ?)";
//3.为占位符设置值
Object[] params = {"1", "张三", "123456"};
//4.执行操作
try {
int rows = qr.update(sql, params);
if(rows > 0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

扫描二维码关注公众号,回复: 6258190 查看本文章

猜你喜欢

转载自www.cnblogs.com/Life-is-Demo/p/10892570.html