DBUtils开源JDBC类库,对JDBC简单封装(作用是:简化编码工作量,同时不会影响程序的性能)
DBUtils:提高了程序的性能,编程更加简便
架包
mysql-connector-java-jar
commons-dbcp-1.4jar
commons-pool-1.5.5jar
commons-dbutils-jar导进去
-------------------------------------------------------------------------------
//数据库连接池:三个开源的架包导进去
数据库连接池连接代码(其方法在TestJDBC()类中):
public Connection testBasicDataSource() throws Exception{
BasicDataSource dataSource=null;
//1. 创建DBCP数据源实例
dataSource=new BasicDataSource();
//2.为数据源实例指定必须的属性
//driver=com.mysql.jdbc.Driver
//jdbcUrl=jdbc:mysql://localhost:3306/lxn
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/lxn");
dataSource.setUsername("root");
dataSource.setPassword("lxn123");
//指定数据源的一些可选的属性
dataSource.setInitialSize(10);//指定数据库连接池中初始化连接的个数
dataSource.setMaxActive(50);//指定最大的连接数:同一时刻可以向数据库同时申请的连接数
dataSource.setMinIdle(10);//指定最小连接数:在数据库连接池空闲状态下,在连接池中最少连接数
dataSource.setMaxWait(1000*5);//等待数据库连接池分配连接最长时间,单位为毫秒,超出时间,抛出异常
Connection connection=dataSource.getConnection();
return connection;
}
--------------------------------------------------------------------------------------------------------
DBUtils里面的各种方法:
/*
* DBUtils:
* */
public class DBUtiles {
/*
*测试QueryRunner类的update方法,可使实现增删改的功能;
*QueryRunner的query方法的返回值取决于其resultsethandller的返回值
*
* */
TestJDBC t=new TestJDBC();
@Test
/*
* ScalarHandler:把结果解转为一个数值(可以是任意数据类型,字符串,date等)返回
* */
public void testScalarHandler() throws Exception{
Connection connection=null;
QueryRunner queryRunner=new QueryRunner();
try {
//获取数据库连接池的方法
connection=t .testBasicDataSource();
String sql="select name from customer where id=?";
//new MapHandler()此方法输出一个结果的键值对;
Object result=
queryRunner.query(connection, sql,new ScalarHandler(),35);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(connection, null, null);
}
}
========================================================
/*
* MapListHanlder:将结果集转为一个map的list
* map:对一个查询的一条记录;键,是列的属性名,值,是键所对应属性名的值; 键不是列的别名
* 而MapListBanlder:返回多条记录的对应的map的集合
* */
public void testMapListHandler() throws Exception{
Connection connection=null;
QueryRunner queryRunner=new QueryRunner();
try {
//获取数据库连接池的方法
connection=t.testBasicDataSource();
String sql="select id,name,email,birth from customer";
//new MapHandler()此方法输出一个结果的键值对;
List<Map<String, Object>> result=
queryRunner.query(connection, sql,new MapListHandler());
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(connection, null, null);
}
}
======================================================
/*
* MapHanlder:返回sql对应的第一条记录对应的map对象
* 输出的是键值对:键,是列的属性名,值,是键所对应属性名的值;
* 键不是列的别名,
* */
public void testMapHandler() throws Exception{
Connection connection=null;
QueryRunner queryRunner=new QueryRunner();
try {
//获取数据库连接池的方法
connection=t.testBasicDataSource();
String sql="select id,name,email,birth from customer";
//new MapHandler()此方法输出一个结果的键值对;
Map<String, Object> map=
queryRunner.query(connection, sql,new MapHandler());
System.out.println(map);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(connection, null, null);
}
}
==================================================================
/*
* BeanListHanlder:把结果集转换为一个list,该list不为空,但可能为空集合(size(),方法返回为0)
* 若sql语句的确能够查到记录,list存放创建BeanListHanlder传入的class对象对应的对象
* */
public void testBeanListHandler() throws Exception{
Connection connection=null;
QueryRunner queryRunner=new QueryRunner();
try {
//获取数据库连接池的方法
connection=t.testBasicDataSource();
String sql="select id,name,email,birth from customer";
//BeanListHandler(Customer.class)此方法输出全部结果集
List<Customer> list=
queryRunner.query(connection, sql,new BeanListHandler(Customer.class));
System.out.println(list);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(connection, null, null);
}
}
============================================================
/*
* BeanHanlder:把结果集的第一条记录转为创建BeanHanlder对象时转入的Class参数对应的对象
* */
public void testBeanHandler() throws Exception{
Connection connection=null;
QueryRunner queryRunner=new QueryRunner();
try {
//获取数据库连接池的方法
connection=t.testBasicDataSource();
//11:String sql="select id,name,email,birth from customer where id=?";
String sql="select id,name,email,birth from customer";
//new BeanHandler(Customer.class)此方法只输出一个结果
Customer customer=
queryRunner.query(connection, sql,new BeanHandler(Customer.class));
//11: Customer customer=
// queryRunner.query(connection, sql,new BeanHandler(Customer.class),35);
System.out.println(customer);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(connection, null, null);
}
}
===============================================================
class MyResultSetHandler implements ResultSetHandler{
@Override
public Object handle(ResultSet resultSet) throws SQLException {
List<Customer> list=new ArrayList<Customer>();
while (resultSet.next()) {
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
String email=resultSet.getString(3);
Date birth=resultSet.getDate(4);
Customer customer=new Customer(id, name, email, (java.sql.Date) birth);
list.add(customer);
}
return list;
}
}
//查询方法
public void testQuery() throws Exception{
QueryRunner queryRunner=new QueryRunner();
Connection connection=null;
try {
//获取数据库连接池,t.testBasicDataSource();是已经建立好的方法
connection=t.testBasicDataSource();
String sql="select id,name,email,birth from customer";
// QueryRunner的query方法的返回值取决于其resultsethandller的返回值
Object obj=queryRunner.query(connection, sql, new MyResultSetHandler());
System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(connection, null, null);
}
}
=========================================================
//实现增删改
public void testQueryRunnerUpdate() throws Exception{
//1.创建QueryRunner的实现类
//删除语句
String sql="delete from customer where id in(?,?)";
//修改语句
//String sql="update customer set name='panpan' where id=90";
//增加语句
//String sql="insert into customer values(90,'jianjia','panpan','1992-5-3')";
Connection connection=null;
QueryRunner queryRunner=new QueryRunner();
try {
//调用数据库连接池的方法
connection=t.testBasicDataSource();
//2.使用其update方法实现增删改
queryRunner.update(connection, sql, 32,33);
} catch (Exception e) {
e.printStackTrace();
}finally {
close(connection, null, null);
}
}
=============================================================
//关闭资源的方法
public void close(Connection connection,
PreparedStatement preparedStatement,ResultSet resultSet) throws Exception{
if (resultSet!=null) {
resultSet.close();
}if (preparedStatement!=null) {
preparedStatement.close();
}if (connection!=null) {
connection.close();
}
}
}