1 package com.java1234.dao;
2
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5
6 import com.java1234.model.Book;
7 import com.java1234.model.BookType;
8 import com.java1234.util.StringUtil;
9 import com.mysql.jdbc.Connection;
10 import com.mysql.jdbc.PreparedStatement;
11
12 /**
13 * 图书Dao类
14 * @author H_Pioneer
15 *
16 */
17 public class BookDao {
18
19 /**
20 * 图书添加
21 * @param con
22 * @param book
23 * @return
24 * @throws Exception
25 */
26 public int add(Connection con,Book book)throws Exception{
27 String sql="insert into t_book values(null,?,?,?,?,?,?)";
28 PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql);
29 pstmt.setString(1, book.getBookName());
30 pstmt.setString(2, book.getAuthor());
31 pstmt.setString(3, book.getSex());
32 pstmt.setFloat(4, book.getPrice());
33 pstmt.setInt(5, book.getBookTypeId());
34 pstmt.setString(6, book.getBookDesc());
35 return pstmt.executeUpdate();
36 }
37
38 /**
39 * 图书信息查询
40 * @param con
41 * @param book
42 * @return
43 * @throws Exception
44 */
45 public ResultSet list(Connection con,Book book)throws Exception{
46 StringBuffer sb=new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
47 if(StringUtil.isNotEmpty(book.getBookName())){
48 sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
49 }
50 if(StringUtil.isNotEmpty(book.getAuthor())){
51 sb.append(" and b.author like '%"+book.getAuthor()+"%'");
52 }
53 if(book.getBookTypeId()!=null && book.getBookTypeId()!=-1){
54 sb.append(" and b.bookTypeId="+book.getBookTypeId());
55 }
56 PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sb.toString());
57 return pstmt.executeQuery();
58 }
59
60 /**
61 * 图书信息删除
62 * @param con
63 * @param id
64 * @return
65 * @throws SQLException
66 */
67 public int delete(Connection con,String id)throws Exception{
68 String sql="delete from t_book where id=?";
69 PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql);
70 pstmt.setString(1, id);
71 return pstmt.executeUpdate();
72 }
73
74 /**
75 * 图书信息修改
76 * @param con
77 * @param book
78 * @return
79 * @throws Exception
80 */
81 public int update(Connection con,Book book)throws Exception{
82 String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookDesc=?,bookTypeId=? where id=?";
83 PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql);
84
85
86 pstmt.setString(1, book.getBookName());
87 pstmt.setString(2, book.getAuthor());
88 pstmt.setString(3, book.getSex());
89 pstmt.setFloat(4, book.getPrice());
90 pstmt.setString(5, book.getBookDesc());
91 pstmt.setInt(6, book.getBookTypeId());
92 pstmt.setInt(7, book.getId());
93 return pstmt.executeUpdate();
94 }
95
96 /**
97 *
98 * @param con
99 * @param bookTypeId
100 * @return
101 * @throws Exception
102 */
103 public boolean existBookByBookTypeId(Connection con,String bookTypeId)throws Exception{
104 String sql="select * from t_book where bookTypeId=?";
105 PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql);
106 pstmt.setString(1, bookTypeId);
107 ResultSet rs = pstmt.executeQuery();
108 String string = new String();
109 return rs.next();
110 }
111 }