📄 bookdbmanager.java
字号:
package com.ciash.bms.db.manager.book.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import com.ciash.bms.config.AppSetting;
import com.ciash.bms.db.manager.book.BookDBEditManager;
import com.ciash.bms.db.manager.book.BookDBFindManager;
import com.ciash.bms.entity.Book;
import com.ciash.common.db.bean.ConnectionStringBean;
import com.ciash.common.db.bean.MySqlDBSource;
import com.ciash.common.db.bean.Parameter;
/**
* <p>Title: BMS</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2009</p>
* <p>Company: Cigarette Ash Inc.</p>
* @author Cigarette Ash
* @version 1.0
*/
public class BookDBManager
implements BookDBFindManager, BookDBEditManager {
public MySqlDBSource dbSource;
// ---------------------------------------------------------------------------------------------
// 插入语句块
private String inserCommand = "INSERT INTO book ("
+ "book_id, book_genus, book.[user],book_name,"
+ "book_concern,book_author, book_purchase_date, "
+ "book_price,book_info, book_image_addr, book_count"
+ ")"
+ "VALUES ("
+ "@id, @genus, @userId,@name,"
+ "@concern, @author, @pd, "
+ "@price,@info, @image, @count"
+ ")";
// ---------------------------------------------------------------------------------------------
// 变量块
Parameter id_pa = new Parameter("id", null);
Parameter genus_pa = new Parameter("genus", null);
Parameter userId_pa = new Parameter("userId", null);
Parameter name_pa = new Parameter("name", null);
Parameter concern_pa = new Parameter("concern", null);
Parameter author_pa = new Parameter("author", null);
Parameter pd_pa = new Parameter("pd", null);
Parameter price_pa = new Parameter("price", null);
Parameter info_pa = new Parameter("info", null);
Parameter image_pa = new Parameter("image", null);
Parameter count_pa = new Parameter("count", null);
// ---------------------------------------------------------------------------------------------
// 构造器
//instance the database
public BookDBManager(ConnectionStringBean connBean) {
dbSource = MySqlDBSource.getInstance(connBean);
}
// ---------------------------------------------------------------------------------------------
// 实现自 接口 BookFindManager
/**
* 通过出版社查询
*
* @param concernName 出版社名
* @return 结果集合
*/
public Book findById(String id) {
try {
dbSource.setSelectCommand("SELECT * FROM book WHERE book_id = @id");
id_pa.setValue(id);
dbSource.addSelectParameter(id_pa);
ResultSet result = dbSource.select();
if (result.next()) {
return Book.toBook(result);
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
/**
* 通过作者名查询
*
* @param concernName 作者名
* @return 结果集合
*/
public Collection findByAuthor(String authorName) {
try {
dbSource.setSelectCommand("SELECT * FROM book WHERE book_author = @author");
author_pa.setValue(authorName);
dbSource.addSelectParameter(author_pa);
ResultSet result = dbSource.select();
LinkedList resultList = new LinkedList();
while (result.next()) {
resultList.add(Book.toBook(result));
}
return resultList;
}
catch (SQLException ex) {
return null;
}
}
/**
* 通过图书名查询
*
* @param name 图书名
* @return 查询结果集
*/
public Collection findByName(String name) {
try {
dbSource.setSelectCommand("SELECT * FROM book WHERE book_name = @name");
name_pa.setValue(name);
dbSource.addSelectParameter(name_pa);
ResultSet result = dbSource.select();
LinkedList resultList = new LinkedList();
while (result.next()) {
resultList.add(Book.toBook(result));
}
return resultList;
}
catch (SQLException ex) {
return null;
}
}
/**
* 通过出版社查询
*
* @param concernName 出版社名
* @return 结果集合
*/
public Collection findByConcern(String concernName) {
try {
dbSource.setSelectCommand("SELECT * FROM book WHERE book_concern = @concern");
concern_pa.setValue(concernName);
dbSource.addSelectParameter(concern_pa);
ResultSet result = dbSource.select();
LinkedList resultList = new LinkedList();
while (result.next()) {
resultList.add(Book.toBook(result));
}
return resultList;
}
catch (SQLException ex) {
return null;
}
}
/**
* 查找所有书籍
*
* @return 返回所有书籍信息
*/
public Collection findAll() {
try {
dbSource.setSelectCommand("SELECT * FROM book");
ResultSet resultSet = dbSource.select();
LinkedList resultList = new LinkedList();
while (resultSet.next()) {
//根据数据库查询结果集,创建对象
Book book = Book.toBook(resultSet);
//更新缓存
resultList.add(book);
}
return resultList;
}
catch (SQLException ex) {
ex.printStackTrace();
return null;
}
}
/**
* 检查数据库中是否有指定的图书信息
* @param book 指定的图书
* @return true 数据库中没有该书的信息
*/
public boolean isExist(Book book){
return isExist(book.getBookId());
}
/**
* 根据索书号检查数据库中是否有指定的图书信息
*
* @param id 索书号
* @return true 数据库中有该书的信息
*/
public boolean isExist(String id){
dbSource.setSelectCommand("SELECT * FROM book WHERE book_id = @id");
dbSource.addSelectParameter(new Parameter("id", id));
ResultSet result = dbSource.select();
try {
if (result.next()) {
return true;
}
return false;
}
catch (SQLException ex) {
ex.printStackTrace();
return false;
}
}
public Collection findByUserId(String id) {
try {
dbSource.setSelectCommand(
"SELECT * FROM book WHERE user = @user");
dbSource.addSelectParameter(new Parameter("user", id));
LinkedList resultList = new LinkedList();
ResultSet result = dbSource.select();
while(result.next()){
resultList.add(Book.toBook(result));
}
return resultList;
}
catch (SQLException ex) {
ex.printStackTrace();
return null;
}
}
// ---------------------------------------------------------------------------------------------
// 实现自 接口 BookEditManager
/**
* 添加新图书.
* <p>
* 比如,当客户端A修改了图书,而客户端B没有进行图书信息的更新而执行了操作,将会出现问题
* @param newBook 新书
*/
public void add(Book newBook) {
//插入到数据库
dbSource.setInsertCommand(inserCommand);
//设置参数
//设置索书号变量
id_pa.setValue(newBook.getBookId());
dbSource.addInsertParameter(id_pa);
//设置图书种类变量
genus_pa.setValue(newBook.getBookGenus());
dbSource.addInsertParameter(genus_pa);
//设置借书用户索书号变量
userId_pa.setValue(newBook.getUserId());
dbSource.addInsertParameter(userId_pa);
//设置书名变量
name_pa.setValue(newBook.getBookName());
dbSource.addInsertParameter(name_pa);
//设置出版社变量
concern_pa.setValue(newBook.getBookConcen());
dbSource.addInsertParameter(concern_pa);
//设置图书作者变量
author_pa.setValue(newBook.getBookAuthor());
dbSource.addInsertParameter(author_pa);
//设置购买日期变量
pd_pa.setValue(newBook.getBookPurchaseDate());
dbSource.addInsertParameter(pd_pa);
//设置价格变量
price_pa.setValue(newBook.getBookPrice());
dbSource.addInsertParameter(price_pa);
//设置图书信息变量
info_pa.setValue(newBook.getBookInfo());
dbSource.addInsertParameter(info_pa);
//设置图片地址变量
image_pa.setValue(newBook.getBookImageAddr());
dbSource.addInsertParameter(image_pa);
//设置库存量变量
count_pa.setValue(newBook.getBookCount());
dbSource.addInsertParameter(count_pa);
dbSource.insert();
}
/**
* 通过索书号删除图书
*
* @param bookId 索书号
*/
public void delete(Book book) {
dbSource.setDeleteCommand("DELETE FROM book WHERE book_id = @id");
id_pa.setValue(book.getBookId());
dbSource.addDeleteParameter(id_pa);
dbSource.delete();
}
/**
* 更新书籍信息
*
* @param book 要更新的书籍对象
*/
public void update(Book book) {
delete(book);
add(book);
}
/**
* 更新主键-索书号,因为主键和一般的属性不一样,所以要特殊处理
*
* @param oldId 修改之前的索书号
* @param newId 修改之后的索书号
*/
public void updateId(String oldId, String newId) {
dbSource.setUpdateCommand(
"UPDATE book SET book_id=@nid WHERE book_id=@oid");
dbSource.addUpdateParameter(new Parameter("nid", newId));
dbSource.addUpdateParameter(new Parameter("oid", oldId));
dbSource.update();
Book book = (Book) findById(oldId);
book.setBookId(newId);
}
public void closeDBSource(){
this.dbSource.close();
}
// ---------------------------------------------------------------------------------------------
// 获得书籍管理器的唯一实例
private static BookDBManager instance = new BookDBManager(AppSetting.connectionStringBean);
/**
* 获得书籍管理器的唯一实例
*
* @return 书籍管理器的唯一实例
*/
public static BookDBManager getInstance(){
return instance;
}
// ---------------------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -