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語句,可是返回值有區別。前者執行後返回的是表中的插入行生成的主鍵值,後者返回的是受語句影響的行數。編碼

相關文章
相關標籤/搜索