📄 bookcontrollerbean.java~1~
字号:
package bookstore.ejb;
import bookstore.util.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.ejb.*;
public class BookControllerBean implements SessionBean
{
SessionContext sessionContext;
private String bookID;
private BookHome bookHome;
private Book book;
private Connection con;
/*************************** removal methods*****************************/
public String createBook(BookDetails bookValue) throws Exception {
// makes a new Book and enters it into db,
// returns BookId
System.out.println("BookControllerBean createBook");
try
{
book = bookHome.create(bookValue);
}
catch (Exception ex)
{
throw new EJBException
("createBook: " + ex.getMessage());
}
return bookID;
}// createBook
public void removeBook(String bookId) throws NoSuchEntityException {
//抛出RunTimeException的子类,有利于事务管理
// removes Book from db
System.out.println("BookControllerBean removeBook");
if (isBookExist(bookId) == false)
throw new NoSuchEntityException(bookId);
try {
book.remove();//Book已在isBookExist中获得
}catch (Exception ex)
{
throw new EJBException
("removeBook: " + ex.getMessage());
}
} // removeBook
public void updateBook(BookDetails bookValue) throws NoSuchEntityException {
System.out.println("BookControllerBean updateBook");
if (isBookExist(bookValue.getBookID()) == false)
throw new NoSuchEntityException(bookValue.getBookID());
book.setName(bookValue.getName());
book.setAuthor(bookValue.getAuthor());
book.setTranslator(bookValue.getTranslator());
book.setPageNum(bookValue.getPageNum());
book.setSize(bookValue.getSize());
book.setPublisher(bookValue.getPublisher());
book.setPublishDate(bookValue.getPublishDate());
book.setStockNum(bookValue.getStockNum());
book.setSaleNum(bookValue.getSaleNum());
book.setPreMonthSaleNum(bookValue.getPreMonthSaleNum());
book.setMonthSaleNum(bookValue.getMonthSaleNum());
book.setSort(bookValue.getSort());
book.setRank(bookValue.getRank());
book.setPrice(bookValue.getPrice());
book.setImage(bookValue.getImage());
}//updateBook
//简单查询,即通过书名模糊匹配
public ArrayList getBooksByName(String name) throws ObjectNotFoundException
{
System.out.println("BookControllerBean getBooksByName");
Collection books;
try
{
String condition="name like "+"'%"+name+"%'";
books =bookHome.findByCondition(condition);
if (books.isEmpty())
throw new ObjectNotFoundException();
} catch (Exception ex) {
throw new ObjectNotFoundException();
}
ArrayList bookValues = new ArrayList();
Iterator i = books.iterator();
while (i.hasNext()) {
Book book = (Book)i.next();
BookDetails bookValue=book.getDetails();
bookValues.add(bookValue);
}
return bookValues;
}//getBookByName
//快速查询,即通过类别来查询
public ArrayList getBooksBySort(String Sort) throws ObjectNotFoundException
{
System.out.println("BookControllerBean getBooksBySort");
Collection books;
try
{
String condition="Sort ="+"'"+Sort+"'";
books =bookHome.findByCondition(condition);
if (books.isEmpty())
throw new ObjectNotFoundException();
} catch (Exception ex) {
throw new ObjectNotFoundException();
}
ArrayList bookValues = new ArrayList();
Iterator i = books.iterator();
while (i.hasNext()) {
Book book = (Book)i.next();
BookDetails bookValue=book.getDetails();
bookValues.add(bookValue);
}
return bookValues;
}//getBookBySort
public ArrayList getBooksByConditions(BookDetails bookValue) throws ObjectNotFoundException
{
System.out.println("BookControllerBean getBooksByConditions");
Collection books;
try
{
String condition="";
boolean isAdd=false;
//书名
if(bookValue.getName().length() != 0)
{
condition=condition+"name="+"'"+bookValue.getName()+"'";
isAdd=true;
}
//作者
if(bookValue.getName().length() != 0)
{
if(isAdd)
{
condition=condition+" and author="+"'"+bookValue.getAuthor()+"'";
}
else
{
condition=condition+"author="+"'"+bookValue.getAuthor()+"'";
isAdd=true;
}
}
//译者
if(bookValue.getTranslator().length()!=0)
{
if(isAdd)
{
condition=condition+" and translator="+"'"+bookValue.getTranslator()+"'";
}
else
{
condition=condition+"translator="+"'"+bookValue.getTranslator()+"'";
isAdd=true;
}
}
//出版社
if(bookValue.getPublisher().length()!=0)
{
if(isAdd)
{
condition=condition+" and Publisher="+"'"+bookValue.getPublisher()+"'";
}
else
{
condition=condition+"Publisher="+"'"+bookValue.getPublisher()+"'";
isAdd=true;
}
}
//丛书
if(bookValue.getSort().length()!=0)
{
if(isAdd)
{
condition=condition+" and Sort="+"'"+bookValue.getSort()+"'";
}
else
{
condition=condition+"Sort="+"'"+bookValue.getSort()+"'";
isAdd=true;
}
}
//出版日期
if(bookValue.getPublishDate()!=null)
{
if(isAdd)
{
condition=condition+" and PublishDate="+"'"+DateHelper.format(bookValue.getPublishDate(),"yyyy-MM-dd HH:mm:ss")+"'";
}
else
{
condition=condition+"PublishDate="+"'"+DateHelper.format(bookValue.getPublishDate(),"yyyy-MM-dd HH:mm:ss")+"'";
isAdd=true;
}
}
books =bookHome.findByCondition(condition);
if (books.isEmpty())
throw new ObjectNotFoundException();
} catch (Exception ex) {
throw new ObjectNotFoundException();
}
ArrayList bookValues = new ArrayList();
Iterator i = books.iterator();
while (i.hasNext()) {
Book book = (Book)i.next();
BookDetails bookV=book.getDetails();
bookValues.add(bookV);
}
return bookValues;
}//getBookByConditions
public ArrayList getHotBooks() throws ObjectNotFoundException
{
System.out.println("BookControllerBean getHotBooks");
Collection books;
try
{
//热卖书的条件:上月销售数量在15以上
String condition="preMonthSaleNum >= 15";
books =bookHome.findByCondition(condition);
if (books.isEmpty())
throw new ObjectNotFoundException();
} catch (Exception ex) {
throw new ObjectNotFoundException();
}
ArrayList bookValues = new ArrayList();
Iterator i = books.iterator();
while (i.hasNext()) {
Book book = (Book)i.next();
BookDetails bookValue=book.getDetails();
bookValues.add(bookValue);
}
return bookValues;
}//getHotBooks
public ArrayList getRecommendedBooks() throws ObjectNotFoundException
{
System.out.println("BookControllerBean getRecommendedBooks");
Collection books;
try
{
//推荐书的条件:上月销售数量在8以上,评定等级在3以上
//rank: 1 一般 2 较好 3 好 4 特好
String condition="preMonthSaleNum >= 8 and rank>=3";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -