code090.java
1package pack02; 2 3import java.sql.Connection; 4import java.sql.DriverManager; 5import java.sql.PreparedStatement; 6import java.sql.ResultSet; 7import java.sql.SQLException; 8import java.sql.Statement; 9import java.util.ArrayList; 10import java.util.List; 11 12//ProductDao�࣬���滻ΪBookDao�࣬��ע�� 13public class code090 14{ 15 public Connection getConnection() 16 { 17 Connection conn = null; 18 19 try 20 { 21 Class.forName("com.mysql.jdbc.Driver"); 22 String url = "jdbc:mysql://localhost:3306/test"; 23 String user = "root"; 24 String pwd = "mysql123"; 25 conn = DriverManager.getConnection(url,user,pwd); 26 } 27 catch (ClassNotFoundException e) 28 { 29 e.printStackTrace(); 30 } 31 catch (SQLException e) 32 { 33 e.printStackTrace(); 34 } 35 36 return conn; 37 } 38 39 /** 40 * 41 * @param page ҳ�� 42 * @return book�༯�� 43 */ 44 public List<code089> find(int page) 45 { 46 List<code089> list1 = new ArrayList<code089>(); 47 Connection conn = this.getConnection(); 48 String sql = "select * from tb_books order by id desc limit ?,?"; //mysql���ݿ�ķ�ҳ 49 50 try 51 { 52 PreparedStatement param = conn.prepareStatement(sql); 53 param.setInt(1, (page - 1) * code089.PAGE_SIZE); //mysql�ӵڼ�����¼��ʼ�� 54 param.setInt(2, code089.PAGE_SIZE); //���ѡȡ���� 55 ResultSet rs = param.executeQuery(); 56 57 while (rs.next()) 58 { 59 code089 book = new code089(); 60 book.setId(rs.getInt("id")); 61 book.setName(rs.getString("name")); 62 book.setPrice(rs.getDouble("price")); 63 book.setBookCount(rs.getInt("bookCount")); 64 book.setAuthor(rs.getString("author")); 65 list1.add(book); 66 } 67 rs.close(); 68 param.close(); 69 conn.close(); 70 } catch (SQLException e) 71 { 72 e.printStackTrace(); 73 } 74 return list1; 75 } 76 77 /** 78 * 79 * @return ���ر���ܼ�¼�� 80 */ 81 public int findCount() 82 { 83 int count = 0; 84 Connection conn = this.getConnection(); 85 String sql = "select count(*) from tb_books"; 86 87 try 88 { 89 Statement stmt = conn.createStatement(); 90 ResultSet rs = stmt.executeQuery(sql); 91 if (rs.next()) 92 { 93 count = rs.getInt(1); 94 } 95 rs.close(); 96 conn.close(); 97 } catch (SQLException e) 98 { 99 e.printStackTrace(); 100 } 101 return count; 102 } 103} 104