数据库连接池druid,以及数据库封装操作

1.首先下载两个jar包,一个是用于数据库连接池,一个是用于数据库简单的封装操作

下载

dbutils用于数据库的封装

druid是数据的连接池

具体下载到https://mvnrepository.com/下载

2.数据库的封装

原因:为了让数据库操作更加简单,比如再写sql语句时会更加简单,毕竟jdbc操作过于复杂

首先要进行类的封装

创建DruidUtils类,此类用于数据库连接池操作

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
//数据库连接池工具类
public class DruidUtils {

private static DruidDataSource druidDataSource;

static 
{
	
	try {
		InputStream is=DruidUtils.class.getResourceAsStream("druid.properties");
		Properties properties=new Properties();
		properties.load(is);
		druidDataSource=(DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	}
//获取连接池数据源
public static DataSource getDataSource() 
{
	return druidDataSource;
	}


//从数据库连接池获取连接对象
	public static Connection getConnection() 
	{
		Connection connection=null;
		try {
			connection=druidDataSource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	
}

何为数据库连接池

 要使用数据库连接池还要创建druid.properties(file文件)

#数据库连接信息
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_sgms?serverTimezone=GMT%2B8
username=root
password=123321abc

#连接池属性
#连接池的初始化连接数<创建数据库连接时默认初始化连接数>
initalSize=10
#连接池的最大连接数
maxActive=50
#最小空闲连接数(当数据库连接使用率很低时,连接池中的连接会被释放一部分)
minIdle=5
#超时等待时间(单位:ms)
maxWait=30000

这样子配置成功就已经完成数据库连接池的配置了

接下来介绍数据库封装

首先老样子创建dto类 老样子具体要做什么大家应该清楚

 dao类(数据库操作)

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 com.zhy.bookmall.ms.dto.Book;
import com.zhy.bookmall.ms.utils.DruidUtils;

public class BookDAO {
//添加图书信息
	public int insertBook(Book book) {

		int i = 0;

		try {
			String sql = "insert into books(book_id,book_name,book_author,book_price,book_img_path,book_desc,book_stock,book_type) values(?,?,?,?,?,?,?,?)";
			QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
			Object[] params = { book.getBookid(), book.getBookname(), book.getBookauthor(), book.getBookprice(),
					book.getBookimgpath(), book.getBookdesc(), book.getBookstock(), book.getBooktype() };
			i = queryRunner.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;

	}

//查询所有图书信息
	//start查询数据的起始行索引
	//limit最多返回的数据记录数(每页显示的条数)
	//booklist显示的是一页的数据
	public List<Book> selectBooks(int start,int limit) {
		List<Book> bookList = null;

		try {
			String sql = "select book_id bookid,book_name bookname,book_author bookauthor,book_price bookprice,book_img_path bookimgpath,book_desc bookdesc,book_stock bookstock,book_type booktype "
					+ "from books limit ?,?";
			QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
			bookList = queryRunner.query(sql, new BeanListHandler<Book>(Book.class),start,limit);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bookList;

	}
	
//查询总记录数
	public long getCount() 
	{
		long count=0;
		
		try {
			String sql = "select count(1) from books";
			QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
			count=queryRunner.query(sql, new ScalarHandler<Long>());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return count;
		
	}
	

//根据图书ID删除一个图书信息
	public int deleteBook(String bookid) {

		int i = 0;

		try {
			String sql = "delete from books where book_id=?";
			QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

			i = queryRunner.update(sql, bookid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;

	}
//根据图书ID查询一个图书信息

	public Book selectBookByBookId(String bookid) {
		Book book = null;

		try {
			String sql = "select book_id bookid,book_name bookname,book_author bookauthor,book_price bookprice,book_img_path bookimgpath,book_desc bookdesc,book_stock bookstock,book_type booktype from books where book_id=?";
			QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
			book = queryRunner.query(sql, new BeanHandler<Book>(Book.class), bookid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return book;

	}

//根据图书ID修改一个图书信息

	public int updateBook(Book book) {

		int i = 0;

		try {
			String sql = "update books set book_name=?,book_author=?,book_price=?,book_img_path=?,book_desc=?,book_stock=?,book_type=? where book_id=?";
			QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
			Object[] params = { book.getBookname(), book.getBookauthor(), book.getBookprice(), book.getBookimgpath(),
					book.getBookdesc(), book.getBookstock(), book.getBooktype(), book.getBookid() };
			i = queryRunner.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;

	}

}

每次使用创建BookDao对象,调用里面的方法

是不是已经简化很多了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值