博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dao层持久层运用QueryRunner+C3P0实现对数据库的所有操作 增删改查 特殊条件查询
阅读量:3962 次
发布时间:2019-05-24

本文共 2851 字,大约阅读时间需要 9 分钟。

QueryRunner*

获取连接

C3P0Utils.getDataSource()

封装到QueryRunner中去,数据库对象

QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());

查询所有

public List
findAllBook() throws SQLException{
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); return qr.query("select * from j2ee_books", new BeanListHandler
(Book.class)); }

添加对象数据

public void addBook(Book book) throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); qr.update("INSERT INTO j2ee_books VALUES(?,?,?,?,?,?,?,?,?)", book.getBook_id(),book.getBook_name(),book.getBook_type(), book.getAuthor() ,book.getPress(),book.getPublish_date(), book.getPrice(),book.getRegister_time(),book.getIs_borrow()); }

通过ID查询数据 关键值

public Book findBookByid(String id) throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); return qr.query("select * from j2ee_books where book_id=?", new BeanHandler
(Book.class),id); }

更新数据

public void updateBook(Book book) throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); qr.update("update j2ee_books set book_name=?,book_type=?, " + "author=?,press=?,publish_date=?,price =?," + "register_time=?, is_borrow=? where book_id=?", book.getBook_name(),book.getBook_type(), book.getAuthor() ,book.getPress(),book.getPublish_date(), book.getPrice(),book.getRegister_time(),book.getIs_borrow(),book.getBook_id()); }

删除数据对象 通过id

public void delBook(String id) throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); qr.update("delete from j2ee_books where book_id =?", id); }

通过输入 来搜索数据对象 展示

public List
searchBook(String name) throws SQLException {
// TODO Auto-generated method stub QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); return qr.query("select * from j2ee_books where book_name like concat('%',?,'%') or author like concat('%',?,'%') or press like concat('%',?,'%') or book_id like concat('%',?,'%') or price like concat('%',?,'%')", new BeanListHandler
(Book.class),name,name,name,name,name); }

返回某列数据 条件查询

ColumnListHandler

public List
findBorrowBookidList(int id) throws SQLException {
// TODO Auto-generated method stub QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); List
list = qr.query("select book_id from j2ee_borrow where reader_id=? and is_return=0", new ColumnListHandler
(),id); return list; }

删除第一行数据 条件

限制在第一行

limit 1

public void delBorrowbook(String huanBookid, int readerId) throws SQLException {
// TODO Auto-generated method stub QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); qr.update("update j2ee_borrow set is_return=1 where book_id =? and reader_id=? limit 1", huanBookid,readerId); }

转载地址:http://zxrzi.baihongyu.com/

你可能感兴趣的文章
su- 与su的区别
查看>>
linux下发邮件mail
查看>>
echo如何手动输出换行
查看>>
身份证的正确使用方法——非常重要的知识
查看>>
ExtJS & Ajax
查看>>
Tomcat在Windows下的免安装配置
查看>>
JMeter常用测试元件
查看>>
JMeter——使用技巧
查看>>
Hibernate 实体层设计--Table per subclass
查看>>
JavaScriptHelper之 observe_field
查看>>
JavaScriptHelper之 periodically_ajax_tag
查看>>
Ruby on Rails(ROR) 小结(一) 绑定controller and view
查看>>
Ruby on Rails(ROR) 小结(一) 通过Schema Migrations来创建数据表
查看>>
form表单post请求发送及回收
查看>>
confluence5.8.10 安装与破解
查看>>
Testlink使用文档
查看>>
Ruby on Rails(ROR) 实例开发之一 配置数据库Mysql
查看>>
Ruby on Rails(ROR) 实例开发之一 创建开发项目环境
查看>>
Ruby on Rails(ROR) 实例开发之一 创建数据表
查看>>
Android_Note(一)——主题界面设计
查看>>