使用传统的JDBC开发,冗余的代码过多。为了简化代码,使用DBUtils进行开发。
DBUtils是Java编程中的数据库操作实用工具,它封装了对JDBC的操作,简化了JDBC的操作,大大简化了代码。
1.QueryRunner(DataSource ds),提供数据源(连接池),DBUtils底层会自动维护连接对象。
QueryRunner的核心方法:
update(String sql,Object...params):该方法用于执行数据的添加,删除,和更新相关操作。
query(String sql,ResultSetHandler rsh,Object...params):该方法用于执行数据库的查询操作。
注:如果是调用了QueryRunner()无参的构造方法来创建QueryRunner对象,则在需要数据库进行添加,删除,更新操作就要调用update(Connection con,String sql,Object…params)方法;查询操作调用query(Connection con,String sql,ResultSetHandler rsh,Object…params)
4.1:添,删,改的例子
步骤:
1.导入相应的jar包,如:数据库驱动,c3p0的jar包,DBUtils的jar包.
2.编写c3p0的配置文件
3.编写c3p0的工具类,用于获取数据源
4.编写BDUtils的代码,先创建QueryRunner对象,在编写SQL语句,如果需要参数就设置参数,执行SQL语句.
c3p0的配置文件:c3p0-config.xml
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driverproperty>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydatabaseproperty>
<property name="user">rootproperty>
<property name="password">123property>
<property name="maxIdleTime">30property>
<property name="maxPoolSize">100property>
<property name="minPoolSize">10property>
<property name="maxStatements">200property>
default-config>
<named-config name="mysql">
<property name="driverClass">com.mysql.jdbc.Driverproperty>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydatabaseproperty>
<property name="user">rootproperty>
<property name="password">123property>
<property name="maxIdleTime">30property>
<property name="maxPoolSize">100property>
<property name="minPoolSize">10property>
<property name="maxStatements">200property>
named-config>
c3p0-config>
c3p0的工具类:
package com.wgy.JDBC.Utils;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Utils {
// 创建下面的类会自动加载位于src目录下的配置文件(c3p0-config.xml),并且加载名字为MySQL的数据
// ComboPooledDataSource dataSource2 = new ComboPooledDataSource("mysql");
private static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql");
private static Connection con = null;
public static ComboPooledDataSource getComboPooledDataSource() {
return dataSource;
}
public static Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
DBUtils的测试类:
package com.wgy.JDBC.Datasource.Test;
import java.sql.SQLException;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;
import com.wgy.JDBC.Utils.C3P0Utils;
public class DBUtilsTest {
//使用DBUtils实现数据的删除
@Test
public void DelectUser(){
try {
QueryRunner qr = new QueryRunner(C3P0Utils.getComboPooledDataSource());
String sql = "DELETE FROM new_user_table WHERE uid = ?";
Object[] params = {13};
int rows = qr.update(sql, params);
if(rows>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//使用DBUtils实现数据的更行
@Test
public void updateUser(){
try {
QueryRunner qr = new QueryRunner(C3P0Utils.getComboPooledDataSource());
String sql = "update new_user_table set uage = ? where uid = ?";
Object[] pararms = {20,13};
int rows = qr.update(sql, pararms);
if(rows>0){
System.out.println("更新成功");
}else{
System.out.println("更新失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//使用DBUtils實現數據的添加
@Test
public void InsertUser(){
try {
QueryRunner qr = new QueryRunner(C3P0Utils.getComboPooledDataSource());
//編寫sql語句
String sql = "insert into new_user_table values(null,?,?)";
//設置參數
Object[] params = {"汾九",1};
//執行SQL語句
int rows = qr.update(sql,params);
if(rows>0){
System.out.println("插入成功");
}else{
System.out.println("插入失敗");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4.2:查询的例子
步骤:
1.导入相应的jar包,如:数据库驱动,c3p0的jar包,DBUtils的jar包.
2.编写c3p0的配置文件
3.编写c3p0的工具类,用于获取数据源
4.编写BDUtils的代码,先创建QueryRunner对象,在编写SQL语句,如果需要参数就设置参数,执行SQL语句.
c3p0的配置文件:c3p0-config.xml
"1.0" encoding="UTF-8"?>
<default-config>
"driverClass">com.mysql.jdbc.Driver
"jdbcUrl">jdbc:mysql://localhost:3306/mydatabase
"user">root
"password">123
"maxIdleTime">30
"maxPoolSize">100
"minPoolSize">10
"maxStatements">200
default-config>
"mysql">
"driverClass">com.mysql.jdbc.Driver
"jdbcUrl">jdbc:mysql://localhost:3306/mydatabase
"user">root
"password">123
"maxIdleTime">30
"maxPoolSize">100
"minPoolSize">10
"maxStatements">200
c3p0的工具类:
package com.wgy.JDBC.Utils;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Utils {
// 创建下面的类会自动加载位于src目录下的配置文件(c3p0-config.xml),并且加载名字为MySQL的数据
// ComboPooledDataSource dataSource2 = new ComboPooledDataSource("mysql");
private static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql");
private static Connection con = null;
public static ComboPooledDataSource getComboPooledDataSource() {
return dataSource;
}
public static Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
DBUtils的测试类:
package com.wgy.JDBC.Datasource.Test;
import java.sql.SQLException;
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.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;
import com.wgy.JDBC.Utils.C3P0Utils;
import com.wgy.domain.User;
public class DBUtilsTestQuery {
// 查询所有对象
@Test
public void QueryUserAll() {
try {
// 1.获取核心类QueryRunner
QueryRunner qr = new QueryRunner(C3P0Utils.getComboPooledDataSource());
// 2.编写SQL语句
String sql = "select * from new_user_table";
// 3.执行SQL语句
List list = qr.query(sql, new BeanListHandler(User.class));
for (User user : list) {
System.out.println(user.getUid() + "," + user.getUname() + "," + user.getUage());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
//通过id查询对象
@Test
public void QueryUserById(){
try {
//1.创建核心类
QueryRunner qr = new QueryRunner(C3P0Utils.getComboPooledDataSource());
//2.编写SQL语句
String sql = "select * from new_user_table where uid = ?";
//3.设置参数
int id = 2;
//4.执行SQL语句
User user = qr.query(sql, id, new BeanHandler(User.class));
System.out.println(user.getUid() + "," + user.getUname() + "," + user.getUage());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//查询所有用户的数量
@Test
public void QueryCount(){
try {
QueryRunner qr = new QueryRunner(C3P0Utils.getComboPooledDataSource());
String sql = "select count(*) from new_user_table";
long count = (long) qr.query(sql, new ScalarHandler());
System.out.println(count);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
注:在使用QueryRunner方法调用query方法时,处理器的选择标准如下:
1. MapHandler:单行处理器!把结果集转换成Map,其中列名为键!
2. MapListHandler:多行处理器!把结果集转换成List