commons-dbutils-1.6.jar:
可以操作JDBC连接数据库,并且执行SQL语句,处理返回的结果集
三个核心类:
DbUtils:关闭资源方法(安静的关闭,不要异常处理)
QueryRunner:执行SQL语句,并且返回你想要的结果集
ResuktSetHandler(参数): 接口(使用实现类,想要什么结果集,就传入什么该接口的实现类)
举例:
导入的包:
import java.sql .Connection
import java.sql .SQLException
import org.apache .commons .dbutils .DbUtils
import org.apache .commons .dbutils .QueryRunner
public static void insert () throws SQLException {
QueryRunner qr = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
String sql = "insert into sort values(null,'显示器',5,'这是一台曲面屏显示器')" ;
int row = qr.update(connection, sql);
if (row > 0 ) {
System.out .println("插入成功" );
}
DbUtils.closeQuietly(connection);
}
public static void main (String[] args) throws SQLException {
QueryRunner qr = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
String sql = "update sort set sname=?,sprice=?,sdesc=? where sid=5" ;
Object[] params = {"咖啡" ,1000 ,"Java里的咖啡" };
int row = qr.update(connection, sql, params );
if (row > 0 ) {
System.out .println("修改成功" );
}
DbUtils.closeQuietly(connection);
}
八个常见的结果集:
导入的包:
import java.sql .Connection
import java.sql .SQLException
import java.util .List
import java.util .Map
import org.apache .commons .dbutils .DbUtils
import org.apache .commons .dbutils .QueryRunner
import org.apache .commons .dbutils .handlers .ArrayHandler
import org.apache .commons .dbutils .handlers .ArrayListHandler
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 .MapHandler
import org.apache .commons .dbutils .handlers .MapListHandler
import org.apache .commons .dbutils .handlers .ScalarHandler
import org.junit .Test
ArrayHandler:
概论:把查询出来的数据第一行,放进对象数据中,并返回 。
@Test
public void select () throws SQLException {
QueryRunner qr = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
String sql = "select * from sort" ;
Object[] objects = qr.query(connection, sql, new ArrayHandler());
for (Object object : objects) {
System.out .println(object );
}
DbUtils.closeQuietly(connection);
}
ArrayListHandler:
概论:查询每一条记录,放到list 集合。每条记录是一个对象数组
@Test
public void select () throws SQLException {
QueryRunner qr = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
String sql = "select * from sort" ;
List list = qr.query(connection, sql, new ArrayListHandler());
for (Object[] objects : list) {
for (Object object : objects) {
System.out .println(object );
}
System.out .println();
}
DbUtils.closeQuietly(connection);
}
BeanHandler:
概论:把结果集的第一行封装成一个JavaBean对象
@Test
public void select () throws SQLException {
QueryRunner qr = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
String sql = "select * from sort" ;
Sort sort = qr.query(connection, sql, new BeanHandler<>(Sort.class));
if (sort == null ) {
System.out .println("没有该条数据" );
}
System.out .println(sort);
DbUtils.closeQuietly(connection);
}
BeanListHandler:
概论:把结果集封装成一个JavaBean对象,放入list 集合中。
@Test
public void select () throws SQLException {
QueryRunner qr = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
String sql = "select * from sort" ;
List list = qr.query(connection, sql, new BeanListHandler<>(Sort.class));
for (Sort sort : list) {
System.out.println(sort);
}
DbUtils.closeQuietly(connection);
}
ColumnListHandler:
概论:返回指定类的数据放在list 中返回
@Test
public void select5 () throws SQLException {
QueryRunner qr = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
String sql = "select * from sort" ;
List list = qr.query(connection, sql, new ColumnListHandler<>("sname" ));
for (Object object : list) {
System.out .println(object );
}
DbUtils.closeQuietly(connection);
}
ScalarHandler:
使用场景:查询结果是一个的时候。
@Test
public void select () throws SQLException {
QueryRunner qr = new QueryRunner();
Connection connection = JDBCUtil.getConnection();
String sql = "select count(*) from sort" ;
Long long1 = qr.query(connection, sql, new ScalarHandler());
System.out.println(long1);
DbUtils.closeQuietly(connection);
}
MapHandler:
@Test
public void select () throws SQLException {
QueryRunner qr = new QueryRunner();
Connection connection = JDBCUtil. getConnection();
String sql = "select * from sort" ;
Map < String , Object> map = qr. query(connection, sql, new MapHandler());
for (String key : map . keySet()) {
System. out. println(key + "---" + map . get(key));
}
DbUtils. closeQuietly(connection);
}
MapListHandler:
@Test
public void select () throws SQLException {
QueryRunner qr = new QueryRunner();
Connection connection = JDBCUtil. getConnection();
String sql = "select * from sort" ;
List < Map < String , Object>> list = qr. query(connection, sql, new MapListHandler());
for (Map < String , Object> map : list ) {
for (String key: map . keySet()) {
System. out. println(key + "---" + map . get(key));
}
}
DbUtils. closeQuietly(connection);
}
数据库连接池:
概论:为了解决频繁创建数据库连接和释放数据库连接的问题,使用了数据库链接池来解决,并且出了一套规范,数据库连接池规范(接口)
常用数据库连接池:
DBCP:dbcp架包依赖pool架包中的类
C3P0
commons-dbcp-1.4.jar与commons-pool-1.5.6.jar:
导入的包:
import javax.sql .DataSource
import org.apache .commons .dbcp .BasicDataSource
public class DataSourceUtil {
private static BasicDataSource dataSource = new BasicDataSource();
static {
dataSource.setDriverClassName("com.mysql.jdbc.Driver" );
dataSource.setUrl("jdbc:mysql://localhost:3306/myjdbc" );
dataSource.setUsername("root" );
dataSource.setPassword("123456" );
dataSource.setInitialSize(10 );
dataSource.setMaxActive(8 );
dataSource.setMaxIdle(5 );
dataSource.setMinIdle(2 );
}
public static DataSource getDataSource () {
return dataSource;
}
}
导入的包:
import java.sql .SQLException
import org.apache .commons .dbutils .QueryRunner
public static void main (String[] args) throws SQLException {
QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
String sql = "insert into sort values (null,?,?,?)" ;
Object[] params = {"试卷" ,10 ,"一份万份试卷" };
int row = qr.update(sql, params );
if (row > 0 ) {
System.out .println("插入成功" );
}
}