一、前期准备
1、安装数据库(如:mysql5.5)
2、安装Eclipse(如:3.4)
3、下载数据库驱动包
4、下载dbutis工具包
5、在Eclipse创建名为 dbutils 的工程并在工程下创建名为lib的文件夹。
将commons-dbutils-1.2.jar与mySql的驱动包拷贝到lib目录
选中这两个jar包右击->build path-->Add to build path
二:
1、在scr目录下创建名为dbcpconfig.properties的文件内容如下
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
username=root
password=root
#
#
initialSize=10
#最大连接数量
maxActive=50
#
maxIdle=20
#
minIdle=5
#
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=utf8
#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的只读(read-only)状态。
#如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
defaultReadOnly=
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_COMMITTED
2、创建包(com.fly31.domain)在该包下创建实体bean
import java.util.Date;
public class User {
private int id;
private String name;
private String password;
private String email;
private Date birthday;
//getter和setter省略
}
然后依据实体bean创建数据表
create database mydb;
use mydb;
create table users(
id int primary key,
name varchar(40),
password varchar(40),
email varchar(60),
birthday date
);
3、创建JDBCUtils,用于创建连接
public class JdbcUtils {
private static DataSource ds;
private static ThreadLocal tl = new ThreadLocal(); //map
static{
try{
Properties prop = new Properties();
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
prop.load(in);
BasicDataSourceFactory factory = new BasicDataSourceFactory();
ds = factory.createDataSource(prop);
}catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static DataSource getDataSource(){
return ds;
}
public static Connection getConnection() throws SQLException{
try{
//得到当前线程上绑定的连接
Connection conn = tl.get();
if(conn==null){ //代表线程上没有绑定连接
conn = ds.getConnection();
tl.set(conn);
}
return conn;
}catch (Exception e) {
throw new RuntimeException(e);
}
}
}
现在准备工作已经完成,可以用dbutils做增删查改操作了。
三、用dbutis对数据进行增删查改操作。
创建包(com.fly31.dbutils),在该包下创建类名为TestDbutils类
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;
import cn.itcast.domain.User;
import cn.itcast.utils.JdbcUtils;
public class TestDbutils{
//使用dbutils完成数据库的crud
@Test
public void insert() throws SQLException{
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "insert into users(id,name,password,email,birthday) values(?,?,?,?,?)";
Object params[] = {2,"bbb","123","[email protected]",new Date()};
runner.update(sql, params);
}
@Test
public void update() throws SQLException{
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "update users set email=? where id=?";
Object params[] = {"[email protected]",1};
runner.update(sql, params);
}
@Test
public void delete() throws SQLException{
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "delete from users where id=?";
runner.update(sql, 1);
}
@Test
public void find() throws SQLException{
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from users where id=?";
User user = (User) runner.query(sql, 1, new BeanHandler(User.class));
System.out.println(user.getEmail());
}
@Test
public void getAll() throws Exception{
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from users";
List list = (List) runner.query(sql, new BeanListHandler(User.class));
System.out.println(list);
}
@Test
public void batch() throws SQLException{
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
String sql = "insert into users(id,name,password,email,birthday) values(?,?,?,?,?)";
Object params[][] = new Object[3][5];
for(int i=0;i