最近做的图书馆管理系统,其包装的封装类是BaseDao
1.图书的封装类:添加、删除、修改、查找
1 package com.jerech.dao; 2 3 import java.security.interfaces.RSAKey; 4 import java.sql.Date; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import javax.persistence.Id; 11 12 import com.jerech.entity.Book; 13 import com.jerech.entity.Member; 14 15 public class BookInfoDao extends baseDAO { 16 // 添加图书 17 public void bookReg(Book book) { 18 StringBuilder sql = new StringBuilder( 19 "insert into bookInfo(bookId,bookName,author,price,bookDate,amount)"); 20 sql.append("values(?,?,?,?,?,?)"); 21 String execsql = sql.toString(); 22 // 将util包下的Date转成SQL下的Date 23 java.sql.Date bornDate = new java.sql.Date(book.getBookDate().getTime()); 24 25 Object[] params = { book.getBookNum(), book.getBookName(), 26 book.getAuthor(), book.getPrice(), bornDate, book.getAmount() }; 27 super.execUpdate(execsql, params); 28 } 29 30 // 获取图书 31 public ListgetAllBook() { 32 String sql = "select * from bookInfo"; 33 ResultSet rs = super.execQuery(sql, null); 34 List booklist = new ArrayList (); 35 36 try { 37 while (rs.next()) { 38 Book book = new Book(); 39 book.setBookNum(rs.getString("bookId")); 40 book.setBookName(rs.getString("bookName")); 41 book.setAuthor(rs.getString("author")); 42 book.setPrice(rs.getFloat("price")); 43 book.setBookDate(rs.getDate("bookDate")); 44 book.setAmount(rs.getInt("amount")); 45 booklist.add(book); 46 } 47 } catch (SQLException e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } finally { 51 super.closeAll(); 52 } 53 return booklist; 54 } 55 56 // 删除图书 57 public void bookDel(String id) { 58 String sql = "delete from bookInfo where bookid=?"; 59 Object[] params = { id }; 60 super.execUpdate(sql, params); 61 } 62 63 // 按名称查找 64 public Book findBookByName(String name) { 65 String sql = "select * from bookInfo where bookName=?"; 66 Object[] params = { name }; 67 ResultSet rs = super.execQuery(sql, params); 68 try { 69 while (rs.next()) { 70 Book book = new Book(); 71 book.setBookNum(rs.getString("bookId")); 72 book.setBookName(rs.getString("bookName")); 73 book.setAuthor(rs.getString("author")); 74 book.setPrice(rs.getFloat("price")); 75 book.setBookDate(rs.getDate("bookDate")); 76 book.setAmount(rs.getInt("amount")); 77 return book; 78 } 79 } catch (SQLException e) { 80 // TODO Auto-generated catch block 81 e.printStackTrace(); 82 } finally { 83 super.closeAll(); 84 } 85 return null; 86 } 87 88 // 按编号查找 89 public Book findBookById(int id) { 90 String sql = "select * from bookInfo where bookId=?"; 91 Object[] params = { id }; 92 ResultSet rs = super.execQuery(sql, params); 93 try { 94 while (rs.next()) { 95 Book book = new Book(); 96 book.setBookNum(rs.getString("bookId")); 97 book.setBookName(rs.getString("bookName")); 98 book.setAuthor(rs.getString("author")); 99 book.setPrice(rs.getFloat("price")); 100 book.setBookDate(rs.getDate("bookDate")); 101 book.setAmount(rs.getInt("amount")); 102 return book; 103 } 104 } catch (SQLException e) { 105 // TODO Auto-generated catch block 106 e.printStackTrace(); 107 } finally { 108 super.closeAll(); 109 } 110 return null; 111 } 112 113 // 修改图书 114 public void bookUpdate(Book book) { 115 String sql = "update bookInfo set bookName=?,author=?,price=?,bookDate=?,amount=? where bookId=? "; 116 java.sql.Date bornDate = new java.sql.Date(book.getBookDate().getTime()); 117 118 Object[] params = { book.getBookName(), book.getAuthor(), 119 book.getPrice(), bornDate, book.getAmount(), book.getBookNum() }; 120 super.execUpdate(sql, params); 121 122 } 123 }
2.用户的登陆、注册、修改、删除
1 package com.jerech.dao; 2 3 import java.sql.Date; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import com.jerech.entity.Member; 10 import com.jerech.entity.MemberBase; 11 12 public class UserInfoDao extends baseDAO { 13 // 用户登录 14 public boolean userLogin(String userName, String pwd) { 15 String sql = "select userName,pwd from userInfo where userName=? and pwd=?"; 16 ResultSet rs = super.execQuery(sql, new Object[] { userName, pwd }); 17 try { 18 if (rs.next()) { 19 return true; 20 } 21 } catch (SQLException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } finally { 25 super.closeAll(); 26 } 27 return false; 28 } 29 30 // 注册用户 31 public void userReg(Member member) { 32 StringBuilder sql = new StringBuilder( 33 "insert into userInfo(userName,pwd,email,sex,birthday,phoneNumber)"); 34 sql.append("values(?,?,?,?,?,?)"); 35 String execsql = sql.toString(); 36 // 将util包下的Date转成SQL下的Date 37 java.sql.Date bornDate = new java.sql.Date(member.getBirthday() 38 .getTime()); 39 40 Object[] params = { member.getMemberName(), member.getPwd(), 41 member.getEmail(), member.getSex(), bornDate, 42 member.getPhoneNumber() }; 43 super.execUpdate(execsql, params); 44 } 45 46 // 获取用户 47 public ListgetAllUser() { 48 String sql = "select * from userInfo"; 49 ResultSet rs = super.execQuery(sql, null); 50 List userlist = new ArrayList (); 51 52 try { 53 while (rs.next()) { 54 Member member = new Member(); 55 member.setMemberId(String.valueOf(rs.getInt("uid"))); 56 member.setMemberName(rs.getString("userName")); 57 member.setPwd(rs.getString("pwd")); 58 member.setBirthday(rs.getDate("birthday")); 59 member.setEmail(rs.getString("email")); 60 member.setSex(rs.getString("sex")); 61 member.setPhoneNumber(rs.getString("phoneNumber")); 62 63 userlist.add(member); 64 } 65 } catch (SQLException e) { 66 // TODO Auto-generated catch block 67 e.printStackTrace(); 68 } finally { 69 super.closeAll(); 70 } 71 return userlist; 72 } 73 74 // 删除用户 75 public void userDel(String id) { 76 String sql = "delete from userInfo where uid=?"; 77 Object[] params = { id }; 78 super.execUpdate(sql, params); 79 } 80 81 // 查找用户 82 public Member findMemberById(String uid) { 83 String sql = "select * from userinfo where uid=?"; 84 Object[] params = { uid }; 85 ResultSet rs = super.execQuery(sql, params); 86 try { 87 if (rs.next()) { 88 float store = 1f; 89 Member member = new Member(rs.getString("userName"), 90 rs.getString("uid"), rs.getString("pwd"), 91 rs.getDate("birthday"), rs.getString("sex"), 92 rs.getString("phoneNumber"), rs.getString("email"), 93 rs.getFloat("money")); 94 95 return member; 96 } 97 } catch (SQLException e) { 98 // TODO Auto-generated catch block 99 e.printStackTrace(); 100 } 101 return null; 102 } 103 104 // 更新用户 105 public void userUpdate(Member member) { 106 String sql = "update userInfo set userName=?,email=?,sex=?,birthday=?,phoneNumber=? where uid=? "; 107 java.sql.Date addDate = new java.sql.Date(member.getBirthday() 108 .getTime()); 109 int uid = Integer.parseInt(member.getMemberId()); 110 Object[] params = { member.getMemberName(), member.getEmail(), 111 member.getSex(), addDate, member.getPhoneNumber(), uid }; 112 super.execUpdate(sql, params); 113 } 114 }