package bean.db;
import java.io.*;import java.sql.*;
import java.util.*;
import org.apache.struts.action.ActionForm;
import form.addBookForm;
import bean.db.common.dbOperation;//封装对book表的操作
public class bookOPBean extends dbOperation {
private int bookid = 0;
private bookBean book = new bookBean();
private int bookTypeId = 0; // 查询出书籍资料
public ArrayList selectBook(String bookname) {
// 构造SQL语句
String sqlString = null;
if (bookname == null || bookname.trim().length() <= 0)
sqlString = new String("select * from book ");
else
sqlString = new String("select *from book where book_name like '%"
+ bookname + "%'");
sqlString = sqlString + "order by add_time desc";
ArrayList rsArrayList = selectBookBySQL(sqlString);
return rsArrayList;
} // 查询出最近的100本书
public ArrayList selectBook100() {
String sqlString = new String(
"select top 100 * from book order by add_time desc");
ArrayList rsArrayList = selectBookBySQL(sqlString);
return rsArrayList;
} // 查询出特价书籍的前7本,5折用其以下的
public ArrayList selectBookMiniPriceTop7() {
String sqlString = new String(
"select top 7 * from book where price_rebate<=5 order by add_time desc");
ArrayList rsArrayList = selectBookBySQL(sqlString);
return rsArrayList;
} // 查询出特价书籍的前6本,5折用其以下的
public ArrayList selectBookMiniPriceTop6() {
String sqlString = new String(
"select top 6 * from book where price_rebate<=5 order by add_time desc");
ArrayList rsArrayList = selectBookBySQL(sqlString);
return rsArrayList;
} // 查询出所有特价书籍,5折用其以下的
public ArrayList selectBookMiniPriceTop() {
String sqlString = new String(
"select * from book where price_rebate<=5 order by add_time desc");
ArrayList rsArrayList = selectBookBySQL(sqlString);
return rsArrayList;
} // 查询出最近录入的6本新书
public ArrayList selectBookTop6() {
// 构造SQL语句
String sqlString = new String(
"select top 6 * from book order by add_time desc");
// 查询出数据
ArrayList rsArrayList = selectBookBySQL(sqlString);
return rsArrayList;
} // 查询出某个分类的前6本书籍,并按时间排序
public ArrayList selectBookByTypeTop6() {
// 构造SQL语句
String sqlString = new String("select top 6 * from book where type_id="
+ bookTypeId + " order by add_time desc");
// 查询出数据
ArrayList rsArrayList = selectBookBySQL(sqlString);
return rsArrayList;
} // 查询出某个分类的书籍的数据,并按时间排序
public ArrayList selectBookByType() {
// 构造SQL语句
String sqlString = new String("select * from book where type_id="
+ bookTypeId + " order by add_time desc");
// 查询出数据
ArrayList rsArrayList = selectBookBySQL(sqlString);
return rsArrayList;
} // 根据SQL语句查询出书籍,并放入ArrayList
public ArrayList selectBookBySQL(String sqlString) {
ResultSet rs = this.executeQuery(sqlString);
// 将查询出的数据放入ArrayList
ArrayList rsArrayList = new ArrayList();
try {
int i = 0;
while (rs.next()) {
rsArrayList.add(i, dealARecord(rs));
i++;
}
} catch (Exception e) {
e.printStackTrace();
rsArrayList = null;
}
return rsArrayList;
} // 一条记录的处理,私有方法
private bookBean dealARecord(ResultSet rs) {
bookBean book = new bookBean();
try {
book.setBookid(rs.getLong("book_id"));
book.setBookname(rs.getString("book_name"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setPrice(rs.getFloat("price"));
book.setPrice_rebate(rs.getFloat("price_rebate"));
book.setPublish_date(rs.getDate("publisher_date"));
book.setPagecount(rs.getInt("pagecount"));
book.setFormat(rs.getString("format"));
book.setSimple_content(rs.getString("simple_content"));
book.setTypeid(rs.getInt("type_id"));
// 图片处理
InputStream in = rs.getBinaryStream("book_image");
if (in != null) {
try {
int len = 10 * 1024 * 1024;
byte[] P_Buf = new byte[len];
int j;
while ((j = in.read(P_Buf)) != -1) {
byte[] oldByteArray = book.getImageByteArray();
byte[] newByteArray = null;
if (oldByteArray != null) {
newByteArray = new byte[oldByteArray.length + j];
for (int k = 0; k < j; k++)
newByteArray[oldByteArray.length + k] = P_Buf[k];
} else {
newByteArray = new byte[j];
for (int k = 0; k < j; k++)
newByteArray[k] = P_Buf[k];
}
book.setImageByteArray(newByteArray);
}
} catch (NullPointerException e1) {
e1.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return book;
} // 根据ID查询出一本书籍的资料
public bookBean selectBookById(int bookid) {
//构造SQL语句
String sqlString="select * from book where book_id="+bookid;
//查询出数据
ResultSet rs=this.executeQuery(sqlString);
//将查询出的数据放入bookbean中
bookBean book=new bookBean();
try{
if(rs.next())
book=dealARecord(rs);
}catch(Exception e){
e.printStackTrace();
book=null;
}
return book;
}
//新书上架
public int insertBook(ActionForm form){
int i=0;
//从ActionForm对象中得到数据
String bookname=((addBookForm)form).getBookname();
String typeid=((addBookForm)form).getTypeid();
String author=((addBookForm)form).getAuthor();
String publisher=((addBookForm)form).getPublisher();
String price=((addBookForm)form).getPrice();
String price_rebate=((addBookForm)form).getPrice_rebate();
String publishdate=((addBookForm)form).getPublishdate();
String pagecount=((addBookForm)form).getPagecount();
String format=((addBookForm)form).getFormat();
String simple_content=((addBookForm)form).getSimple_content();

//构造SQL语句
String sqlString="insert into book(book_name,author,publisher"
+",price,price_rebate,publish_date,pagecount,format,simple_cntent"
+",type_id) values ('"+bookname+"','"+author+"','"+publisher
+"',"+price+","+price_rebate+",'"+publishdate+"',"+pagecount+
",'"+format+"','"+simple_content+"',"+typeid+")";
//执行SQL语句
i=this.executeUpdate(sqlString);
return i;
}
//删除一本书
public int deleteBook(int bookid){
int i=0;
if(bookid<=0) return i;
String sqlString="delete form book where book_id="+bookid;
i=this.executeUpdate(sqlString);
return i;
}
//更新一本书籍的信息
public int updateBook(ActionForm form,int bookid){
int i=0;
//从ActionForm对象中得到数据
String bookname=((addBookForm)form).getBookname();
String typeid=((addBookForm)form).getTypeid();
String author=((addBookForm)form).getAuthor();
String publisher=((addBookForm)form).getPublisher();
String price=((addBookForm)form).getPrice();
String price_rebate=((addBookForm)form).getPrice_rebate();
String publishdate=((addBookForm)form).getPublishdate();
String pagecount=((addBookForm)form).getPagecount();
String format=((addBookForm)form).getFormat();
String simple_content=((addBookForm)form).getSimple_content();

//构造SQL语句
String sqlString="iupdate book set book_name='"+bookname+"',author='"+
author+"',publisher='"+publisher+"',price="+price+",price_rebate="+
price_rebate+",publish_date='"+publishdate+"',pagecount="+
pagecount+",format='"+format+"',simple_cntent='"+simple_content+
"',type_id="+typeid+" where bookid="+bookid;
//执行SQL语句
i=this.executeUpdate(sqlString);
return i;
}
public int getBookid(){
return bookid;
}
public void setBookid(int bookid){
 this.bookid=bookid;
 this.book=this.selectBookById(bookid);
}
public bookBean getBook(){
return book;
}
public void setBook(bookBean book){
 this.book=book;
}
public int getBookTypeId(){
return bookTypeId;
}
public void setBookTypeId(int bookTypeId){
 this.bookTypeId=bookTypeId;
}
}以上代码红色部分为报错的,我想知道我少什么包,还是少的那个类必须得自己建一个?
SOS啊...周日就得进行答辩了TT

解决方案 »

  1.   

    ActionForm form,你用的struts,怎么连struts包都不知道?
    百度一下struts的相关jar,导入到你的程序就行了。
      

  2.   

    import form.addBookForm;  有个form包里面有个addBookForm.java的类
      

  3.   

    上面的那个类没找到底下就全部错了。
    String bookname=((addBookForm)form).getBookname();
    String typeid=((addBookForm)form).getTypeid();
    String author=((addBookForm)form).getAuthor();
    String publisher=((addBookForm)form).getPublisher();
    String price=((addBookForm)form).getPrice();
    String price_rebate=((addBookForm)form).getPrice_rebate();
    String publishdate=((addBookForm)form).getPublishdate();
    String pagecount=((addBookForm)form).getPagecount();
    String format=((addBookForm)form).getFormat();
    String simple_content=((addBookForm)form).getSimple_content();
      

  4.   

    下载了好多的strusts的jar包...都导进去了...还是木有解决啊..
    您知道是具体哪个jar包么?我再去下一个放里。