一. 介绍
DBUtils 是 Apache 组织开源的数据库工具类。
二. 使用步骤
①. 创建 QueryRunner 对象
②. 调用 update() 方法或者 query() 方法执行 sql 语句
三. 构造方法及静态方法
QueryRunner 类
1. 构造方法
①. 无参构造
QueryRunner qr =new QueryRunner();
使用无参构造的时候,调用 update 方法和 query 方法时就需要使用带 Connection 类型参数的重载形式
②. 有参构造
QueryRunner qr= new QueryRunner(DataSource dataSource);
这个参数是连接池对象
2. 静态方法
①.int update(Connection con ,String sql ,Param);
该方法用于增删改语句的操作
参数介绍:
参数一:连接池对象(这个在无参构造的时候使用)
参数二:sql 语句
参数三:可变参数(就是 sql 占位符的值)
返回值:int 类型的 返回受影响的行数
简单 update demo
- public class Demo {
- public static void main(String[] args) throws Exception {
- /*
- * 演示有参构造的update()方法
- *
- * 首先得导入jar包
- * 配置好C3P0的配置文件与准备好C3P0工具类
- * 然后创建QueryRunner对象
- * 调用update方法
- * 最后处理结果
- */
- QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
- int re = qr.update("update user set name=? where uid=?", "张三", 2);
- if (re > 0) {
- System.out.println("修改成功");
- } else {
- System.out.println("修改失败");
- }
- }
- }
附上简单的 C3P0 工具类
- public class C3P0Utils {
- private static DataSource dataSource = new ComboPooledDataSource();
- /**
- * 获得DataSource实现类对象
- * @return
- */
- public static DataSource getDataSource() {
- return dataSource;
- }
- /**
- * 获得连接
- * @return
- * @throws Exception
- */
- public static Connection getConnection() throws Exception {
- return dataSource.getConnection();
- }
- }
②.query(Connection con , String sql ,Param ...)
该方法用于出查询操作
参数介绍:
参数一:Connection 数据库连接对象, 使用带参构造时可以不用
参数二:sql 语句
参数三:表示对结果集的处理方式 (ResultSetHandler 接口)
ArrayHandler: 表示将结果集第一行的数据存入数组参数四:可变参数(就是 sql 占位符的值)
使用 BeanListHandler 处理方式的 demo:
- public void demo1() throws Exception {
- QueryRunner qr = new QueryRunner(MyC3P0Utils.getDataSource());
- List < Car > list = qr.query("select * from car where price<20 order by price desc", new BeanListHandler < >(Car.class));
- for (Car car: list) {
- System.out.println(car);
- }
- }
javaBean 类的编写:
- public class Car {
- private int cid;
- private String cname;
- private String company;
- private String grade;
- private double price;@Override public String toString() {
- return "Car [cid=" + cid + ", cname=" + cname + ", company=" + company + ", grade=" + grade + ", price=" + price + "]";
- }
- public int getCid() {
- return cid;
- }
- public void setCid(int cid) {
- this.cid = cid;
- }
- public String getCname() {
- return cname;
- }
- public void setCname(String cname) {
- this.cname = cname;
- }
- public String getCompany() {
- return company;
- }
- public void setCompany(String company) {
- this.company = company;
- }
- public String getGrade() {
- return grade;
- }
- public void setGrade(String grade) {
- this.grade = grade;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public Car(int cid, String cname, String company, String grade, double price) {
- super();
- this.cid = cid;
- this.cname = cname;
- this.company = company;
- this.grade = grade;
- this.price = price;
- }
- public Car() {
- super();
- // TODO Auto-generated constructor stub
- }
- }
来源: http://www.bubuko.com/infodetail-2451462.html