BookDAO.java
上传用户:ycdfsj258
上传日期:2022-08-04
资源大小:1094k
文件大小:7k
源码类别:

图形图象

开发平台:

Java

  1. package com.dao;
  2. import com.core.ConnDB;
  3. import java.sql.*;
  4. import java.util.*;
  5. import com.actionForm.BookForm;
  6. public class BookDAO {
  7.     private ConnDB conn=new ConnDB();
  8. //查询数据
  9. public Collection query(String strif){
  10. BookForm bookForm=null;
  11. Collection bookColl=new ArrayList();
  12. String sql="";
  13. if(strif!="all" && strif!=null && strif!=""){
  14.     sql="select * from (select b.*,c.name as bookcaseName,p.pubname as publishing,t.typename from tb_bookinfo b left join tb_bookcase c on b.bookcase=c.id join tb_publishing p on b.ISBN=p.ISBN join tb_booktype t on b.typeid=t.id where b.del=0)  as book where book."+strif+"'";
  15. }else{
  16.     sql="select b.*,c.name as bookcaseName,p.pubname as publishing,t.typename from tb_bookinfo b left join tb_bookcase c on b.bookcase=c.id join tb_publishing p on b.ISBN=p.ISBN join tb_booktype t on b.typeid=t.id where b.del=0";
  17. }
  18. System.out.println("图书查询时的SQL:"+sql);
  19. ResultSet rs=conn.executeQuery(sql);
  20. try {
  21.     while (rs.next()) {
  22.         bookForm=new BookForm();
  23.         bookForm.setBarcode(rs.getString(1));
  24.         bookForm.setBookName(rs.getString(2));
  25.         bookForm.setTypeId(rs.getInt(3));
  26.         bookForm.setAuthor(rs.getString(4));
  27.         bookForm.setTranslator(rs.getString(5));
  28.         bookForm.setIsbn(rs.getString(6));
  29.         bookForm.setPrice(Float.valueOf(rs.getString(7)));  //此处必须进行类型转换
  30.         bookForm.setPage(rs.getInt(8));
  31.         bookForm.setBookcaseid(rs.getInt(9));
  32.         bookForm.setStorage(rs.getInt(10));
  33.         bookForm.setInTime(rs.getString(11));
  34.         bookForm.setOperator(rs.getString(12));
  35.         bookForm.setDel(rs.getInt(13));
  36.         bookForm.setId(Integer.valueOf(rs.getString(14)));
  37.         bookForm.setBookcaseName(rs.getString(15));
  38.         bookForm.setPublishing(rs.getString(16));
  39.         bookForm.setTypeName(rs.getString(17));
  40.         bookColl.add(bookForm);
  41.         System.out.print(bookForm.getTypeName());
  42.     }
  43. } catch (SQLException ex) {
  44. }
  45. conn.close();
  46. return bookColl;
  47. }
  48. //用于修改的查询
  49. public BookForm queryM(BookForm bookForm){
  50. BookForm bookForm1=null;
  51. String sql="select b.*,c.name as bookcaseName,p.pubname as publishing,t.typename from tb_bookinfo b left join tb_bookcase c on b.bookcase=c.id join tb_publishing p on b.ISBN=p.ISBN join tb_booktype t on b.typeid=t.id where b.id="+bookForm.getId()+"";
  52. System.out.println("修改时的SQL:"+sql);
  53. ResultSet rs=conn.executeQuery(sql);
  54. try {
  55.     while (rs.next()) {
  56.         bookForm1=new BookForm();
  57.         bookForm1.setBarcode(rs.getString(1));
  58.         bookForm1.setBookName(rs.getString(2));
  59.         bookForm1.setTypeId(rs.getInt(3));
  60.         bookForm1.setAuthor(rs.getString(4));
  61.         bookForm1.setTranslator(rs.getString(5));
  62.         bookForm1.setIsbn(rs.getString(6));
  63.         bookForm1.setPrice(Float.valueOf(rs.getString(7)));  //此处必须进行类型转换
  64.         bookForm1.setPage(rs.getInt(8));
  65.         bookForm1.setBookcaseid(rs.getInt(9));
  66.         bookForm1.setStorage(rs.getInt(10));
  67.         bookForm1.setInTime(rs.getString(11));
  68.         bookForm1.setOperator(rs.getString(12));
  69.         bookForm1.setDel(rs.getInt(13));
  70.         bookForm1.setId(Integer.valueOf(rs.getString(14)));
  71.         bookForm1.setBookcaseName(rs.getString(15));
  72.         bookForm1.setPublishing(rs.getString(16));
  73.         bookForm1.setTypeName(rs.getString(17));
  74.     }
  75. } catch (SQLException ex) {
  76. }
  77. conn.close();
  78. return bookForm1;
  79. }
  80.     //用于借阅的查询
  81.    public BookForm queryB(String f,String key){
  82.    BookForm bookForm1=null;
  83.    String sql="select b.*,c.name as bookcaseName,p.pubname as publishing,t.typename from tb_bookinfo b left join tb_bookcase c on b.bookcase=c.id join tb_publishing p on b.ISBN=p.ISBN join tb_booktype t on b.typeid=t.id where b."+f+"='"+key+"'";
  84.    System.out.println("查询借阅信息时的SQL:"+sql);
  85.    ResultSet rs=conn.executeQuery(sql);
  86.    try {
  87.        if (rs.next()) {
  88.            bookForm1=new BookForm();
  89.            bookForm1.setBarcode(rs.getString(1));
  90.            bookForm1.setBookName(rs.getString(2));
  91.            bookForm1.setTypeId(rs.getInt(3));
  92.            bookForm1.setAuthor(rs.getString(4));
  93.            bookForm1.setTranslator(rs.getString(5));
  94.            bookForm1.setIsbn(rs.getString(6));
  95.            bookForm1.setPrice(Float.valueOf(rs.getString(7)));  //此处必须进行类型转换
  96.            bookForm1.setPage(rs.getInt(8));
  97.            bookForm1.setBookcaseid(rs.getInt(9));
  98.            bookForm1.setStorage(rs.getInt(10));
  99.            bookForm1.setInTime(rs.getString(11));
  100.            bookForm1.setOperator(rs.getString(12));
  101.            bookForm1.setDel(rs.getInt(13));
  102.            bookForm1.setId(Integer.valueOf(rs.getString(14)));
  103.            bookForm1.setBookcaseName(rs.getString(15));
  104.            bookForm1.setPublishing(rs.getString(16));
  105.            bookForm1.setTypeName(rs.getString(17));
  106.        }
  107.    } catch (SQLException ex) {
  108.    }
  109.    conn.close();
  110.    return bookForm1;
  111.    }
  112. //添加数据
  113. public int insert(BookForm bookForm){
  114. String sql1="SELECT * FROM tb_bookinfo WHERE barcode='"+bookForm.getBarcode()+"' or bookname='"+bookForm.getBookName()+"'";
  115. ResultSet rs = conn.executeQuery(sql1);
  116. String sql = "";
  117. int falg = 0;
  118. try {
  119. if (rs.next()) {
  120.     falg = 2;
  121. } else {
  122.     sql ="Insert into tb_bookinfo (barcode,bookname,typeid,author,translator,isbn,price,page,bookcase,storage,inTime,operator) values('"+bookForm.getBarcode()+"','"+bookForm.getBookName()+"',"+bookForm.getTypeId()+",'"+bookForm.getAuthor()+"','"+bookForm.getTranslator()+"','"+bookForm.getIsbn()+"',"+bookForm.getPrice()+","+bookForm.getPage()+","+bookForm.getBookcaseid()+","+bookForm.getStorage()+",'"+bookForm.getInTime()+"','"+bookForm.getOperator()+"')";
  123.     falg = conn.executeUpdate(sql);
  124.     System.out.println("添加图书的SQL:" + sql);
  125.     conn.close();
  126. }
  127. } catch (SQLException ex) {
  128. falg = 0;
  129. }
  130. System.out.println("falg:"+falg);
  131. return falg;
  132. }
  133. //修改数据
  134. public int update(BookForm bookForm){
  135. String sql="Update tb_bookinfo set typeid="+bookForm.getTypeId()+",author='"+bookForm.getAuthor()+"',translator='"+bookForm.getTranslator()+"',isbn='"+bookForm.getIsbn()+"',price="+bookForm.getPrice()+",page="+bookForm.getPage()+",bookcase="+bookForm.getBookcaseid()+",storage="+bookForm.getStorage()+" where id="+bookForm.getId()+"";
  136. int falg=conn.executeUpdate(sql);
  137. System.out.println("修改数据时的SQL:"+sql);
  138. conn.close();
  139. return falg;
  140. }
  141. //删除数据
  142. public int delete(BookForm bookForm){
  143. String sql="UPDATE tb_bookinfo SET del=1 where id="+bookForm.getId()+"";
  144. int falg=conn.executeUpdate(sql);
  145. System.out.println("删除时的SQL:"+sql);
  146. return falg;
  147. }
  148. }