package book;
import cn.edu.swu.db.DBEngine;
import cn.edu.swu.db.RecordVisitor;
import cn.edu.swu.user.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class BookRepo {
private static BookRepo instance = new BookRepo();
private BookRepo(){
}
public static BookRepo getInstance() {
return instance;
}
public void saveBook(Book book) throws SQLException {
if (book.getId() > 0) {
this.updateBook(book);
} else {
this.insertBook(book);
}
}
private void insertBook(Book book) throws SQLException {
String template =
"INSERT INTO `book`(`name`, `author`, `price`, `describe`) " +
"VALUES (\"%s\", \"%s\", %s, \"%s\")";
String sql = String.format(template, book.getName(), book.getAuthor(), book.getPrice(), book.getDescribe());
DBEngine.getInstance().execute(sql);
}
private void updateBook(Book book) throws SQLException {
String template =
"UPDATE `book` SET `name`=\"%s\", `author`=\"%s\", `price`=%s, `describe`=\"%s\" " +
"WHERE `id`=%s";
String sql = String.format(template, book.getName(),
book.getAuthor(), book.getPrice(), book.getDescribe(), book.getId());
DBEngine.getInstance().execute(sql);
}
public void deleteBook(Book book) throws SQLException {
String template = "DELETE FROM `book` WHERE `id` = %s";
String sql = String.format(template, book.getId());
DBEngine.getInstance().execute(sql);
}
public void deleteBook(Long id) throws SQLException {
String template = "DELETE FROM `book` WHERE `id` = %s";
String sql = String.format(template, id);
DBEngine.getInstance().execute(sql);
}
public List<Book> getAll() throws SQLException {
String sql = "SELECT `id`, `name`, `author`, `price`, `describe` FROM `book`";
return DBEngine.getInstance().query(sql, new RecordVisitor<Book>() {
@Override
public Book visit(ResultSet rs) throws SQLException {
return BookRepo.getBookFromResultset(rs);
}
});
}
public Book getById(String id) throws SQLException {
String sql = "SELECT * FROM `book` WHERE `id` = %s";
List<Book> books = DBEngine.getInstance().query(
String.format(sql, id), new RecordVisitor<Book>() {
@Override
public Book visit(ResultSet rs) throws SQLException {
return BookRepo.getBookFromResultset(rs);
}
});
return books.size() == 0 ? null : books.get(0);
}
private static Book getBookFromResultset(ResultSet rs) throws SQLException {
Book book = new Book();
book.setId(rs.getLong("id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setDescribe(rs.getString("describe"));
book.setPrice(rs.getFloat("price"));
return book;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
大三学习javaweb的作业.zip 大三学习javaweb的作业.zip大三学习javaweb的作业.zip大三学习javaweb的作业.zip大三学习javaweb的作业.zip大三学习javaweb的作业.zip大三学习javaweb的作业.zip大三学习javaweb的作业.zip大三学习javaweb的作业.zip大三学习javaweb的作业.zip大三学习javaweb的作业.zip大三学习javaweb的作业.zip
资源推荐
资源详情
资源评论
收起资源包目录
大三学习javaweb的作业.zip (70个子文件)
javawebj-master
project-6
doc
db_init.sql 189B
src
book
Book.java 838B
ListBookServlet.java 2KB
BookRepo.java 3KB
UpdateBookServlet.java 2KB
SaveBookServlet.java 2KB
DeleteBookServlet.java 709B
user
LogoutServlet.java 628B
ValidateCodeServlet.java 3KB
User.java 671B
LoginServlet.java 2KB
AuthFilter.java 2KB
UserRepo.java 2KB
Index.java 3KB
Web
debug.log 99B
submit-book.html 681B
login.html 594B
admin.html 462B
images
b11.jpg 19KB
project-6.iml 494B
.gitignore 278B
project-1
Main.java 2KB
Student
StudentHttpReader.java 711B
StudentManager.java 2KB
IStudentVisitor.java 100B
IStudentReader.java 169B
StudentAbstractReader.java 1KB
StudentReaderFactory.java 499B
Student.java 587B
StudentFileReader.java 636B
BookWeb
lib
javax.jms.jar 25KB
javax.annotation.jar 8KB
javax.transaction.jar 9KB
javax.servlet.jsp.jar 77KB
javax.persistence.jar 127KB
javax.servlet.jar 68KB
javax.ejb.jar 46KB
javax.servlet.jsp.jstl.jar 27KB
javax.resource.jar 43KB
src
WebRoot
book.html 5KB
BookWeb.iml 879B
.idea
libraries
ognl_3_1_26.xml 232B
thymeleaf_3_0_12_RELEASE.xml 258B
Java_EE_6_Java_EE_6.xml 775B
commons_dbcp2_2_9_0.xml 1009B
workspace.xml 7KB
misc.xml 278B
modules.xml 261B
runConfigurations
build.xml 250B
build.xml 2KB
project-7
doc
db_init.sql 189B
project-7.iml 570B
src
book
Book.java 0B
ListBookServlet.java 0B
BookRepo.java 0B
UpdateBookServlet.java 0B
SaveBookServlet.java 0B
DeleteBookServlet.java 0B
Index.java 3KB
conf
CharsetFilter.java 0B
db
DBEngine.java 0B
.idea
uiDesigner.xml 9KB
libraries
lib.xml 683B
workspace.xml 2KB
misc.xml 177B
modules.xml 265B
WebRoot
style.css 271B
submit-book.html 904B
login.html 737B
admin.html 652B
共 70 条
- 1
资源评论
程序员张小妍
- 粉丝: 1w+
- 资源: 3474
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 计算机网络四次实验报告
- (175549404)基于微信小程序的十二神鹿点餐(外卖小程序)(毕业设计,包括数据库,源码,教程).zip
- (179941432)基于MATLAB车牌识别系统【GUI含界面】.zip
- (179941434)基于MATLAB车牌识别系统【含界面GUI】.zip
- (178021462)基于Javaweb+ssm的医院在线挂号系统的设计与实现.zip
- (178047214)基于springboot图书管理系统.zip
- 张郅奇 的Python学习过程
- (23775420)欧姆龙PLC CP1H-E CP1L-E CJ2M CP1E 以太网通讯.zip
- (174590622)计算机课程设计-IP数据包解析
- (175550824)泛海三江全系调试软件PCSet-All2.0.3 1
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功