使用DBUtils实现增删改查
使用DBUtils实现增删改查
- 书籍实体类(JavaBean)
- 需要使用到的jar包
- 建立书籍类数据库
- 创建dbcpUtils(使用dbcp连接池,通过配置文件方式)
- 创建DBUtilsDao类
- 创建测试类(使用junit)
- 项目整体目录结构
书籍实体类(JavaBean)
package com.lz.jiaotong.dujiale.pojo;
public class Book {
String book_id;//书籍编号
String book_name;//书籍名称
String book_author;//书籍作者
String book_type;//书籍类型
public Book() {
}
public String getBook_id() {
return book_id;
}
public String getBook_name() {
return book_name;
}
public String getBook_author() {
return book_author;
}
public String getBook_type() {
return book_type;
}
public void setBook_id(String book_id) {
this.book_id = book_id;
}
public void setBook_name(String book_name) {
this.book_name = book_name;
}
public void setBook_author(String book_author) {
this.book_author = book_author;
}
public void setBook_type(String book_type) {
this.book_type = book_type;
}
public Book(String book_id, String book_name, String book_author, String book_type) {
this.book_id = book_id;
this.book_name = book_name;
this.book_author = book_author;
this.book_type = book_type;
}
@Override
public String toString() {
return "Book{" +
"book_id='" + book_id + '\'' +
", book_name='" + book_name + '\'' +
", book_author='" + book_author + '\'' +
", book_type='" + book_type + '\'' +
'}';
}
}
需要使用到的jar包
注意把你的jar包放到lib目录下后,还需要把jar包加入到环境中去:
点击jar包->右键->add Library
- commons-dbcp2-2.9.0.jar
- commons-dbutils-1.4.jar
- commons-pool2-2.11.1.jar
- commons-logging-1.2.jar
- mysql-connector-java-5.1.37-bin.jar
- junit-4.8.2.jar
这里我已经下载好放到我的网盘(自取):
链接:https://pan.baidu.com/s/10q_zmR3dN4jrlwYIjw-lGQ
提取码:lzdj
建立书籍类数据库
- 数据库中的书籍表展示:
- 数据库创建以及使用当前数据库:
create database BookStore;
use BookStore;
- 建立书籍表
CREATE TABLE `book` (
`book_id` int(11) NOT NULL AUTO_INCREMENT,
`book_name` varchar(20) DEFAULT NULL,
`book_author` varchar(20) DEFAULT NULL,
`book_type` varchar(20) DEFAULT NULL,
PRIMARY KEY (`book_id`)
) ;
- 向书籍表中插入值
INSERT INTO book VALUES (1,'三体','刘慈欣','科幻类');
INSERT INTO book VALUES (2,'左传','左丘明','古籍类');
INSERT INTO book VALUES (3,'Effective Java','Josh Bloch ','编程类');
创建dbcpUtils(使用dbcp连接池,通过配置文件方式)
(1) dbcp连接池的配置文件
dbcp.properties:
注意:配置文件中的url、password使用你自己的url以及你的mysql数据库密码
#驱动全限定类名
driverClassName=com.mysql.jdbc.Driver
#数据库连接地址(这里使用你自己的数据库,我的数据库是day21)
url=jdbc:mysql://localhost:3306/day21?useUnicode=true&characterEncoding=UTF-8
#数据库用户名
username=root
#数据库密码
password=123456
#连接池建立时创建的连接的数量
initialSize=2
#连接池同一时间内最多能够分配的活动连接的数量
maxActive=15
#在其他连接没有被释放的情况下,连接池中最多能够保留的闲置连接
maxIdle=2
#在没有其他连接被创建的情况下,连接池中最少可以保留的闲置连接
minIdle=1
#当遇到请求,而连接池中没有连接可以被分配,连接池最大等待时间,超过这个时间将会抛出一个异常。为-1时,将会无限期等待。
maxWait=30000
(2) dbcp工具类
dbcpUtils:
package com.lz.jiaotong.dujiale.utils;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class dbcpUtils {
private static Properties pro = new Properties();
private static DataSource dataSource = null;
private static Connection con = null;
//加载dbcp配置
static {
try {
FileInputStream fis = new FileInputStream("G:\\IdeaProjects\\lzjtu_servlet_jsp_study\\dbcpDemo\\src\\com\\lz\\jiaotong\\dujiale\\resources\\dbcp.properties");
pro.load(fis);
dataSource = BasicDataSourceFactory.createDataSource(pro);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获得连接的方法
*
* @return 连接
* @throws Exception
*/
public static Connection getConnection() {
try {
con = dataSource.getConnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return con;
}
public static DataSource getDataSource() {
return dataSource;
}
/**
* 释放资源
*
* @param resultSet
* @param statement
* @param connection
*/
public static void release(ResultSet resultSet, Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
创建DBUtilsDao类
package com.lz.jiaotong.dujiale.dao;
import com.lz.jiaotong.dujiale.pojo.Book;
import com.lz.jiaotong.dujiale.utils.dbcpUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class DBUtilsDao {
private static QueryRunner queryRunner = new QueryRunner(dbcpUtils.getDataSource());
//查询所有书籍,返回List集合
public void findAll() {
//sql语句
String sql = "select * from book";
try {
List<Book> bookList = queryRunner.query(sql, new BeanListHandler<Book>(Book.class));
for (Book book : bookList) {
System.out.println(book);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
//增加书籍信息
public void insertBook(String args1, String args2, String args3) {
String sql = "insert into book values(null,?,?,?)";
try {
int row = queryRunner.update(sql, args1, args2, args3);
System.out.println("增加书籍成功,受影响的行数为:" + row);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
//删除书籍信息
public void deleteBookById(int id) {
String sql = "delete from book where book_id = ?";
try {
int row = queryRunner.update(sql, id);
System.out.println("删除成功,受影响的行数为:" + row);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
//修改书籍信息
public void updateBookById(int id, String args1, String args2, String args3) {
String sql = "update book set book_name = ? ,book_author = ? ,book_type = ? where book_id = ?";
try {
int row = queryRunner.update(sql, args1, args2, args3, id);
System.out.println("修改成功,受影响的行数为:" + row);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
创建测试类(使用junit)
package com.lz.jiaotong.dujiale.dao;
import org.junit.Test;
public class DBUtilsTest {
static DBUtilsDao dbUtilsDao = new DBUtilsDao();
@Test
public void findAll(){
System.out.println("------------查询所有书籍---------");
dbUtilsDao.findAll();
}
@Test
public void insertBook(){
System.out.println("------------增加书籍信息---------");
dbUtilsDao.insertBook("嫌疑人X的献身", "东野圭吾", "悬疑");
}
@Test
public void deleteBookById(){
System.out.println("------------删除书籍信息---------");
dbUtilsDao.deleteBookById(4);
}
@Test
public void updateBookById(){
System.out.println("------------修改书籍信息---------");
dbUtilsDao.updateBookById(2, "高一零班", "七根胡", "恐怖");
}
}