DbUtils使用笔记

Apache Commons DbUtils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,能极大简化JDBC编码的工做量,同时也不会影响程序的性能。java

最新版本是Apache Commons DbUtils 1.6mysql

测试实例sql

package cn.iborder.test;

import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;

import javax.sql.DataSource;

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.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import cn.iborder.entity.Student;

public class Test3 {
	
	/**
	 * 数据源
	 * @return
	 */
	public DataSource createDataSource() {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		try {
			dataSource.setDriverClass("com.mysql.jdbc.Driver");
			dataSource.setJdbcUrl("jdbc:mysql://localhost/test");
			dataSource.setUser("root");
			dataSource.setPassword("root");
			dataSource.setInitialPoolSize(3);
			dataSource.setMaxPoolSize(6);
			dataSource.setMaxIdleTime(1000);
		} catch (PropertyVetoException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return dataSource;
	}
	
	@Test
	public void test1() {
		ComboPooledDataSource dataSource = (ComboPooledDataSource) createDataSource();
		try {
			QueryRunner run = new QueryRunner(dataSource);
			String sql = "update student set age= ? where userId = ?";
			//run.update(sql, new Random().nextInt(99)+1, 23);
			//run.update(sql, new Object[]{new Random().nextInt(99)+1, 23});
			//批量更新
			run.batch(sql, new Object[][]{{new Random().nextInt(99)+1, new Random().nextInt(29)+1},{new Random().nextInt(99)+1, new Random().nextInt(29)+1},{45,23},{12,7}});
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			dataSource.close();
		}
	}

	@Test
	public void test2() {
		ComboPooledDataSource dataSource = (ComboPooledDataSource) createDataSource();
		try {
			QueryRunner run = new QueryRunner(dataSource);
			String sql = "INSERT INTO student(userName,gender,age) VALUES(?,?,?)";
			//返回的结果集是Long类型
			long result = run.insert(sql, new ScalarHandler<Long>(), new Object[]{"张三", "m", 15});
			System.out.println(result);
			
			//批量插入
			List<Long> rs = run.insertBatch(sql, new ColumnListHandler<Long>(), new Object[][]{{"张杀死", "w", 33},{"张五", "a", 44},{"张六", "v", 55}});
			for (Long t : rs) {
				System.out.println(t);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dataSource.close();
		}
	}
	
	@Test
	public void test3() {
		ComboPooledDataSource dataSource = (ComboPooledDataSource) createDataSource();
		try {			
			QueryRunner run = new QueryRunner(dataSource);
			String sql = "select * from student";
			//用于对结果集进行封装
			ResultSetHandler<List<Student>> rs = new BeanListHandler<Student>(Student.class);
			List<Student> result = run.query(sql, rs, 22);
			
			for (Student student : result) {
				System.out.println(student.getUserId());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dataSource.close();
		}
	}
	
}

关于结果集

单行数据处理:ScalarHandler、ArrayHandler、MapHandler、BeanHandlerapache

多行数据处理:BeanListHandler、AbstractListHandler(ArrayListHandler 、dom

MapListHandler、ColumnListHandler)、AbstractKeyedHandler(KeyedHandler、工具

BeanMapHandler)性能

可供扩展的类:BaseResultSetHandler测试

Dbutils使用结果集的方法有query、insert、insertBatch三个。insert()和update()方法都能执行插入记录的sql语句,可是返回值有区别。前者执行后返回的是表中的插入行生成的主键值,后者返回的是受语句影响的行数。编码

相关文章
相关标签/搜索