图书管理系统之DAO设计与实现

来源:互联网 发布:unity3d 2d游戏 编辑:程序博客网 时间:2024/05/18 03:55

为什么要进行DAO设计?DAO是java代码与数据库建立连接的纽带,通过DAO可以对数据库进行方便的存储于访问,下面就对本项目中用到的DAO进行逐一讲解。

一、CommonDAO,通用Dao里只有两个主要方法,更新与查询操作,因为对数据库进行操作时除了查询之外都是更新,所以建立两个通用的方法将会对接下来的DAO设计更加方便。两个方法里含有两个参数,一个是sql语句,另一个是可变长的数组,方便对传进的不同参数进行处理。

package pdsu.bbm.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class CommonDao {private String drivername = "com.microsoft.sqlserver.jdbc.SQLServerDriver";private String url = "jdbc:sqlserver://localhost:1433;DatabaseName=library";private String name = "sa";private String password = "123456";private Connection con = null;public CommonDao() {}// 建立连接数据库public void openConnection() {try {Class.forName(drivername);con = DriverManager.getConnection(url, name, password);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}// 查询方法public ResultSet query(String sql, Object... objs) {ResultSet rs = null;try {if (con == null) {openConnection();}PreparedStatement ps = null;ps = con.prepareStatement(sql);if (objs != null) {for (int i = 0; i < objs.length; i++) {ps.setObject(i + 1, objs[i]);}}rs = ps.executeQuery();} catch (SQLException e) {e.printStackTrace();}return rs;}//更新操作public int update(String sql, Object... objs) {int result = 0;PreparedStatement ps = null;try {if (con == null) {openConnection();}ps = con.prepareStatement(sql);for (int i = 0; i < objs.length; i++) {ps.setObject(i + 1, objs[i]);}result = ps.executeUpdate();System.out.println(result + "行受影响");} catch (SQLException e) {e.printStackTrace();}return result;}//关闭连接public void close() {try {con.close();} catch (SQLException e) {e.printStackTrace();} finally {con = null;}}}

二、BookDao.java.主要包含添加图书信息,查询所有图书信息,根据ISBN编号查询,根据图书名模糊查询,根据图书类型查询,根据作者查询,根据出版社查询, 修改图书信息等,查询结果为结果集,使用list来存储书的所有信息。

package pdsu.bbm.dao;import java.sql.ResultSet;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import pdsu.bbm.model.Book;public class BookDao {// 添加图书信息public static int insertBook(Book book) {int result = 0;CommonDao dao = new CommonDao();String sql = "insert into book values(?,?,?,?,?,?,?,?)";try {result = dao.update(sql, book.getISBN(), book.getTypeid(),book.getBookname(),book.getAuthor(), book.getPublish(), book.getPublishdate().toLocaleString(),book.getPublishtime(), book.getUnitprice());} catch (Exception e) {e.printStackTrace();}dao.close();return result;}// 查询所有图书信息public static List<Book> selectBook() {List<Book> list = null;CommonDao dao = new CommonDao();String sql = "select * from book";ResultSet rs = dao.query(sql);list=new ArrayList<Book>();try {while (rs.next()) {Book book = new Book();book.setISBN(rs.getString("ISBN"));book.setTypeid(rs.getString("typeid"));book.setBookname(rs.getString("bookname"));book.setAuthor(rs.getString("author"));book.setPublish(rs.getString("publish"));book.setPublishdate(rs.getDate("publishdate"));book.setPublishtime(rs.getInt("publishtime"));book.setUnitprice(rs.getInt("unitprice"));list.add(book);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}// 根据ISBN编号查询public static Book selectBookByISBN(String ISBN) {Book book = null;ResultSet rs=null;CommonDao dao = new CommonDao();String sql = "select * from book where ISBN=?"; rs = dao.query(sql, ISBN);try {if (rs.next()) {book = new Book();book.setISBN(rs.getString("ISBN"));book.setTypeid(rs.getString("typeid"));book.setBookname(rs.getString("bookname"));book.setAuthor(rs.getString("author"));book.setPublish(rs.getString("publish"));book.setPublishdate(rs.getDate("publishdate"));book.setPublishtime(rs.getInt("publishtime"));book.setUnitprice(rs.getInt("unitprice"));}} catch (SQLException e) {e.printStackTrace();}dao.close();return book;}// 根据图书名模糊public static List<Book> selectBookByName(String name) {List<Book> list = null;CommonDao dao = new CommonDao();String bname = "%" + name + "%";String sql = "select * from book where bookname like ?";ResultSet rs = dao.query(sql, bname);list =new ArrayList<Book>();try {while(rs.next()){Book book = new Book();book.setISBN(rs.getString("ISBN"));book.setTypeid(rs.getString("typeid"));book.setBookname(rs.getString("bookname"));book.setAuthor(rs.getString("author"));book.setPublish(rs.getString("publish"));book.setPublishdate(rs.getDate("publishdate"));book.setPublishtime(rs.getInt("publishtime"));book.setUnitprice(rs.getInt("unitprice"));list.add(book);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}//根据图书类型查询public static List<Book> selectBookByType(String type){List<Book> list = null;CommonDao dao = new CommonDao();String btype = "%" + type + "%";String sql = "select * from book where typeid like ?";ResultSet rs = dao.query(sql, btype);list =new ArrayList<Book>();try {while(rs.next()){Book book = new Book();book.setISBN(rs.getString("ISBN"));book.setTypeid(rs.getString("typeid"));book.setBookname(rs.getString("bookname"));book.setAuthor(rs.getString("author"));book.setPublish(rs.getString("publish"));book.setPublishdate(rs.getDate("publishdate"));book.setPublishtime(rs.getInt("publishtime"));book.setUnitprice(rs.getInt("unitprice"));list.add(book);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}//根据作者查询public static List<Book> selectBookByAuthor(String author){List<Book> list = null;CommonDao dao = new CommonDao();String bauthor = "%" + author + "%";String sql = "select * from book where author like ?";ResultSet rs = dao.query(sql, bauthor);list =new ArrayList<Book>();try {while(rs.next()){Book book = new Book();book.setISBN(rs.getString("ISBN"));book.setTypeid(rs.getString("typeid"));book.setBookname(rs.getString("bookname"));book.setAuthor(rs.getString("author"));book.setPublish(rs.getString("publish"));book.setPublishdate(rs.getDate("publishdate"));book.setPublishtime(rs.getInt("publishtime"));book.setUnitprice(rs.getInt("unitprice"));list.add(book);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}//根据出版社查询public static List<Book> selectBookByPublish(String Publish){List<Book> list = null;CommonDao dao = new CommonDao();String bPublish = "%" + Publish + "%";String sql = "select * from book where publish like ?";ResultSet rs = dao.query(sql, bPublish);list =new ArrayList<Book>();try {while(rs.next()){Book book = new Book();book.setISBN(rs.getString("ISBN"));book.setTypeid(rs.getString("typeid"));book.setBookname(rs.getString("bookname"));book.setAuthor(rs.getString("author"));book.setPublish(rs.getString("publish"));book.setPublishdate(rs.getDate("publishdate"));book.setPublishtime(rs.getInt("publishtime"));book.setUnitprice(rs.getInt("unitprice"));list.add(book);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}//修改图书信息public static int update(Book book){int result=0;SimpleDateFormat sim=new SimpleDateFormat("yyyy-WW-dd");String sql="update book set typeid=?,bookname=?,author=?,publish=?,publishdate=?,publishtime=?,unitprice=?  where ISBN=?";CommonDao dao =new CommonDao();try {result=dao.update(sql,book.getTypeid(),book.getBookname(),book.getAuthor(),book.getPublish(),sim.format(book.getPublishdate()),
book.getPublishtime(),book.getUnitprice(),book.getISBN());} catch (Exception e) {
e.printStackTrace();
}
dao.close();
return result;
}
public static void main(String[] args) {
System.out.println(BookDao.selectBookByISBN("10101010"));
}


}
三、BookTypeDAO.java.包括根据图书类型id查询所有图书类型,查询所有图书类型,查询指定图书类型的信息,根据编号查询图书类型,添加图书信息,修改指定编号的图书
类型,删除指定类型编号的图书类型,根据图书名称查询图书信息,查询指定图书类型的id。
package pdsu.bbm.dao;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import pdsu.bbm.model.BookType;public class BookTypeDao {public static BookType selectBooktypeDao(int id){BookType ci = null;String sql = "select * from booktype where id=?";CommonDao Dao = new CommonDao();ResultSet rs = Dao.query(sql, id);try {if(rs.next()){int ci_id = rs.getInt("id");String ci_type = rs.getString("typename");ci = new BookType(); ci.setId(ci_id);ci.setTypename(ci_type);}} catch (SQLException e) {e.printStackTrace();}return ci;}// 查询所有图书类型信息public static List<BookType> selectBookType() {List<BookType> list = null;CommonDao dao = new CommonDao();String sql = "select * from booktype";ResultSet rs = dao.query(sql);list = new ArrayList<BookType>();try {while (rs.next()) {BookType bt = new BookType();bt.setId(rs.getInt("id"));bt.setTypename(rs.getString("typename"));list.add(bt);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}// 查询指定图书类型的图书信息public static List<BookType> selectBookType(String type) {List<BookType> list = null;CommonDao dao = new CommonDao();String booktype = "%" + type + "%";String sql = "select * from booktype where typename like ?";ResultSet rs = dao.query(sql, booktype);list=new ArrayList<BookType>();try {while (rs.next()) {BookType bt = new BookType();bt.setId(rs.getInt("id")); bt.setTypename(rs.getString("typename"));list.add(bt);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}//public static String selectById(String id){String typename=null;String sql="select * from booktype where id=?";CommonDao dao =new CommonDao();ResultSet rs=dao.query(sql,id);try {while(rs.next()){typename=rs.getString("typename");}} catch (SQLException e) {e.printStackTrace();}dao.close();return typename;}// 添加图书信息public static int insertBookType(Integer id, String typename) {int result = 0;CommonDao dao = new CommonDao();String sql = "insert into booktype(id,typename) values(?,?)";result = dao.update(sql, id, typename);dao.close();return result;}// 修改指定编号的图书类型信息public static int updateBookType(Integer id, String typename) {int result=0;String sql="update  Booktype set typename=? where id=?";CommonDao dao = new CommonDao();result = dao.update(sql,typename,id);return result;}// 删除指定类型编号的图书类型。public static int deleteBookType(Integer id) {int result = 0;CommonDao dao = new CommonDao();String sql = "delete  from booktype where id=?";result = dao.update(sql, id);dao.close();return result;}//根据图书名称查询图书信息public static String  selectByTypename(String name){List<BookType> list=new ArrayList<BookType>();BookType booktype=null;String sql="select * from booktype where typename=?";ResultSet rs=null;CommonDao dao=new CommonDao();rs=dao.query(sql,name);String m=null;try {while(rs.next()){try {m=rs.getString("id");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} catch (SQLException e) {e.printStackTrace();}return m;}// 查询指定图书类型的idpublic static List<BookType> selectIdByTypename(String name) {List<BookType> list = null;CommonDao dao = new CommonDao();String sql = "select * from booktype where typename like ?";ResultSet rs = dao.query(sql, "%" + name + "%");list = new ArrayList<BookType>();try {if (rs.next()) {BookType bt=new BookType();bt.setId(rs.getInt("id"));bt.setTypename(rs.getString("typename"));list.add(bt);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}public static void main(String[] args) {System.out.println(BookTypeDao.selectIdByTypename("计算机类"));}}
四、BorrowBookinfoDAO.java.包含根据读者id查询借的书籍,借阅书籍,归还书籍等。
package pdsu.bbm.dao;import java.sql.Date;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import pdsu.bbm.model.BorrowBook;public class BorrowBookinfoDao {public static List<BorrowBook> selectBorrowByReaderId(String readerid){List<BorrowBook> list = null;String sql = "select * from borrowbook where readerid=? and returndate is null";CommonDao dao = new CommonDao();ResultSet rs = dao.query(sql,readerid);list = new ArrayList<BorrowBook>();try {while(rs.next()){BorrowBook ci = new BorrowBook();ci.setReaderid(rs.getString("readerid"));ci.setISBN(rs.getString("ISBN"));ci.setBorrowdate(rs.getDate("borrowdate"));ci.setReturndate(rs.getDate("returndate"));ci.setFine(rs.getInt("fine"));list.add(ci);}} catch (SQLException e) {e.printStackTrace();}return list;}public static BorrowBook selectBorrowByReaderId1(String readerid){BorrowBook ci = null ;String sql = "select * from borrowbook where readerid=? and returndate is null";CommonDao dao = new CommonDao();ResultSet rs = dao.query(sql,readerid);try {if(rs.next()){ci = new BorrowBook();ci.setReaderid(rs.getString("readerid"));ci.setISBN(rs.getString("ISBN"));ci.setBorrowdate(rs.getDate("borrowdate"));ci.setReturndate(rs.getDate("returndate"));ci.setFine(rs.getInt("fine"));}} catch (SQLException e) {e.printStackTrace();}return ci;} final private static  Object [][] getSelect(List<BorrowBook> list){ String [] a ={"读者编号","图书编号","借书日期","还书日期","罚金"};Object [][] data = new Object[list.size()][5];for(int i = 0;i<list.size();i++){BorrowBook borrowbook = list.get(i);data[i][0] = borrowbook.getReaderid();data[i][1] = borrowbook.getISBN();data[i][2] = borrowbook.getBorrowdate();data[i][3] = borrowbook.getReturndate();data[i][4] = borrowbook.getFine();}return data;}public static int borrowBook(String readerid,String ISBN,String borrowdate){int result = 0;String sql = "insert into borrowbook(readerid,ISBN,borrowdate) values(?,?,?);";CommonDao dao = new CommonDao();result = dao.update(sql, readerid,ISBN,borrowdate);return result;}public static int returnBook(String readerid,String ISBN,Date returndate) {int result = 0;String sql1 = "update borrowbook set returndate=? where readerid=? and ISBN=? and returndate is null ;";CommonDao dao = new CommonDao();result = dao.update(sql1, returndate,readerid,ISBN);return result;}}

五、ReaderDAO.java.包含录入读者信息、查询读者信息、根据id查询读者信息、根据读者姓名模糊查询、根据读者类型查询、根据院系查询,修改读者信息、添加读者。

package pdsu.bbm.dao;import java.net.CookieHandler;import java.sql.Date;import java.sql.ResultSet;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import pdsu.bbm.model.Reader;public class ReaderDao {// 录入读者信息public static int insertReader(Reader reader) {int result = 0;CommonDao dao = new CommonDao();String sql = "insert into reader values(?,?,?,?,?,?,?,?)";try {result = dao.update(sql, reader.getReaderid(), reader.getType(), reader.getName(), reader.getAge(),reader.getSex(), reader.getPhone(), reader.getDept(), reader.getRegDate());} catch (Exception e) {e.printStackTrace();}dao.close();return result;}// 查看读者信息public static List<Reader> selectReader() {List<Reader> list = new ArrayList<Reader>();CommonDao dao = new CommonDao();String sql = "select readerid,type,name,age,sex,phone,dept,regdate,typename,maxborrownum,limit  from reader join readertype on 
reader.type=readertype.id";ResultSet rs = dao.query(sql);try {while (rs.next()) {Reader reader = new Reader();reader.setReaderid(rs.getString("readerid"));reader.setType(rs.getInt("type"));reader.setName(rs.getString("name"));reader.setAge(rs.getInt("age"));reader.setSex(rs.getString("sex"));reader.setPhone(rs.getString("phone"));reader.setDept(rs.getString("dept"));reader.setRegDate(rs.getDate("regdate"));reader.setTypename(rs.getString("typename"));reader.setMaxborrownum(rs.getInt("maxborrownum"));reader.setLimit(rs.getInt("limit"));list.add(reader);}} catch (Exception e) {e.printStackTrace();}dao.close();return list;}public static List<Reader> selectReaderById(String id) {List<Reader> list = new ArrayList<Reader>();CommonDao dao = new CommonDao();String sql = "select readerid,type,name,age,sex,phone,dept,regdate,typename,maxborrownum,limit  from reader join readertype on 
reader.type=readertype.id where readerid='"+ id + "'";ResultSet rs = dao.query(sql);try {while (rs.next()) {Reader reader = new Reader();reader.setReaderid(rs.getString("readerid"));reader.setType(rs.getInt("type"));reader.setName(rs.getString("name").trim());reader.setAge(rs.getInt("age"));reader.setSex(rs.getString("sex"));reader.setPhone(rs.getString("phone"));reader.setDept(rs.getString("dept"));reader.setRegDate(rs.getDate("regdate"));reader.setTypename(rs.getString("typename"));reader.setMaxborrownum(rs.getInt("maxborrownum"));reader.setLimit(rs.getInt("limit"));list.add(reader);}} catch (Exception e) {e.printStackTrace();}dao.close();return list;}public static Reader selectReaderById2(String readerid) {Reader reader = null;String sql = "select * from reader where readerid=? ";CommonDao dao = new CommonDao();ResultSet rs = dao.query(sql, readerid);try {while (rs.next()) {reader = new Reader();reader.setReaderid(rs.getString("readerid"));reader.setType(rs.getInt("type"));reader.setTypename(rs.getString("name"));reader.setAge(rs.getInt("age"));reader.setSex(rs.getString("sex"));reader.setPhone(rs.getString("phone"));reader.setDept(rs.getString("dept"));reader.setRegDate(rs.getDate("regdate"));}} catch (SQLException e) {e.printStackTrace();}return reader;}// 通过读者姓名模糊查询public static List<Reader> selectReaderByName(String name) {List<Reader> list = new ArrayList<Reader>();CommonDao dao = new CommonDao();String sql = "select readerid,type,name,age,sex,phone,dept,regdate,typename,maxborrownum,limit  from reader join readertype on
 reader.type=readertype.id and name like ?";ResultSet rs = dao.query(sql, "%" + name + "%");try {while (rs.next()) {Reader reader = new Reader();reader.setReaderid(rs.getString("readerid"));reader.setType(rs.getInt("type"));reader.setName(rs.getString("name"));reader.setAge(rs.getInt("age"));reader.setSex(rs.getString("sex"));reader.setPhone(rs.getString("phone"));reader.setDept(rs.getString("dept"));reader.setRegDate(rs.getDate("regdate"));reader.setTypename(rs.getString("typename"));reader.setMaxborrownum(rs.getInt("maxborrownum"));reader.setLimit(rs.getInt("limit"));list.add(reader);}} catch (Exception e) {e.printStackTrace();}dao.close();return list;}// 通过读者类型模糊查询public static List<Reader> selectReaderByType(String type) {List<Reader> list = new ArrayList<Reader>();CommonDao dao = new CommonDao();String sql = "select readerid,type,name,age,sex,phone,dept,regdate,typename,maxborrownum,limit from reader join readertype on
 reader.type=readertype.id and readertype.typename like ?";ResultSet rs = dao.query(sql, "%" + type + "%");try {while (rs.next()) {Reader reader = new Reader();reader.setReaderid(rs.getString("readerid"));reader.setType(rs.getInt("type"));reader.setName(rs.getString("name"));reader.setAge(rs.getInt("age"));reader.setSex(rs.getString("sex"));reader.setPhone(rs.getString("phone"));reader.setDept(rs.getString("dept"));reader.setRegDate(rs.getDate("regdate"));reader.setTypename(rs.getString("typename"));reader.setMaxborrownum(rs.getInt("maxborrownum"));reader.setLimit(rs.getInt("limit"));list.add(reader);}} catch (Exception e) {e.printStackTrace();}dao.close();return list;}// 根据院系查询public static List<Reader> selectReaderByDept(String dept) {List<Reader> list = new ArrayList<Reader>();CommonDao dao = new CommonDao();String sql = "select readerid,type,name,age,sex,phone,dept,regdate,typename,maxborrownum,limit  from reader join readertype on
 reader.type=readertype.id and dept like ?";ResultSet rs = dao.query(sql, "%" + dept + "%");try {while (rs.next()) {Reader reader = new Reader();reader.setReaderid(rs.getString("readerid"));reader.setType(rs.getInt("type"));reader.setName(rs.getString("name"));reader.setAge(rs.getInt("age"));reader.setSex(rs.getString("sex"));reader.setPhone(rs.getString("phone"));reader.setDept(rs.getString("dept"));reader.setRegDate(rs.getDate("regdate"));reader.setTypename(rs.getString("typename"));reader.setMaxborrownum(rs.getInt("maxborrownum"));reader.setLimit(rs.getInt("limit"));list.add(reader);}} catch (Exception e) {e.printStackTrace();}dao.close();return list;}// 修改读者信息public static int updateReader(Reader reader) {int typeid = 0, i = 0;CommonDao dao = new CommonDao();try {String sql1 = "select * from readertype where typename=?";ResultSet rs = dao.query(sql1, reader.getTypename());try {while (rs.next()) {typeid = rs.getInt("id");reader.setType(typeid);}} catch (Exception e) {e.printStackTrace();}String sql = "update reader set type=?,name=?,age=?,sex=?,phone=?,dept=?,regdate=? where readerid=?";// System.out.println(sql);// 向数据库添加数据需要若干参数信息,把这些信息加入一个数组中使代码更清楚Object[] objs = new Object[8];objs[0] = reader.getType();objs[1] = reader.getName();objs[2] = reader.getAge();objs[3] = reader.getSex();objs[4] = reader.getPhone();objs[5] = reader.getDept();// 把读者的注册日期转换为字符串类型SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String regDateString = sdf.format(reader.getRegDate());objs[6] = Dao.getDateToString(reader.getRegDate());objs[7] = reader.getReaderid();i = dao.update(sql, objs);} catch (Exception e) {e.printStackTrace();}dao.close();return i;}
//添加读者
public static   Reader insertReader(String id,String name,int age,String sex,String phone,String dept,Date regDate,String typename){
Reader ci = null;
String sql = " insert into reader(readerid,type,name,age,sex,phone,dept,regdate) values(?,?,?,?,?,?,?,?)";
CommonDao dao = new CommonDao();
ResultSet rs = dao.query(sql, id,typename,name,age,sex,phone,dept,regDate);
return ci;

}
public static Reader selectReaderById(String readerid){
Reader reader=null;
String sql="select * from reader where readerid=? ";
CommonDao dao=new CommonDao();
ResultSet rs=dao.query(sql,readerid);   
try {
while (rs.next()) {
reader=new Reader();
reader.setReaderid(rs.getString("readerid"));
reader.setType(rs.getInt("type"));
reader.setTypename(rs.getString("name"));
reader.setAge(rs.getInt("age"));
reader.setSex(rs.getString("sex"));
reader.setPhone(rs.getString("phone"));
reader.setDept(rs.getString("dept"));
reader.setRegDate(rs.getDate("regdate"));
}
} catch (SQLException e) {
e.printStackTrace();
}

return reader;
}public static void main(String[] args) {System.out.println(ReaderDao.selectReaderById("101"));}}

六、ReaderTypeDao.java.包含查询所有读者类型信息、根据读者id查询读者类型、查询指定类型名的读者类型信息、根据类型编号查找、添加读者类型、修改指定编号的读者
类型信息、修改读者的所有信息、删除指定类型编号的读者类型。

package pdsu.bbm.dao;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import pdsu.bbm.model.ReaderType;public class ReaderTypeDao {// 读者类别设置包括查询所有读者类型信息public static List<ReaderType> selectReaderType() {List<ReaderType> list = null;CommonDao dao = new CommonDao();String sql = "select * from readertype";ResultSet rs = dao.query(sql);list = new ArrayList<ReaderType>();try {while (rs.next()) {ReaderType bt = new ReaderType();bt.setId(rs.getInt("id"));bt.setTypename(rs.getString("typename"));bt.setMaxborrownum(rs.getInt("maxborrownum"));bt.setLimit(rs.getInt("limit"));list.add(bt);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}public static ReaderType selectReaderType2(int id){ReaderType ci = null;String sql = "select * from readertype where id=? ";CommonDao dao = new CommonDao();ResultSet rs = dao.query(sql, id);try {if(rs.next()){ci = new ReaderType();ci.setId(rs.getInt("id"));ci.setTypename(rs.getString("typename"));ci.setMaxborrownum(rs.getInt("maxborrownum"));ci.setLimit(rs.getInt("limit"));}} catch (SQLException e) {e.printStackTrace();}return ci;}//查询指定类型名的读者类型信息public static List<ReaderType> selectReaderType(String type){List<ReaderType> list =null;CommonDao dao =new CommonDao();String sql="select * from readertype where typename like ?";ResultSet rs=dao.query(sql,"%"+type+"%");list=new ArrayList<ReaderType>();try {while(rs.next()){ReaderType bt = new ReaderType();bt.setId(rs.getInt("id"));bt.setTypename(rs.getString("typename"));bt.setMaxborrownum(rs.getInt("maxborrownum"));bt.setLimit(rs.getInt("limit"));list.add(bt);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}//根据类型编号查找public static List<ReaderType> selectReaderTypeByNUm(Integer num){List<ReaderType> list =null;CommonDao dao =new CommonDao();String sql="select * from readertype where id=?";ResultSet rs=dao.query(sql,num);list=new ArrayList<ReaderType>();try {while(rs.next()){ReaderType bt = new ReaderType();bt.setId(rs.getInt("id"));bt.setTypename(rs.getString("typename"));bt.setMaxborrownum(rs.getInt("maxborrownum"));bt.setLimit(rs.getInt("limit"));list.add(bt);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}public static int insertReaderType(ReaderType reader){int result=0;String sql="insert into readertype values(?,?,?,?) ";CommonDao dao=new CommonDao();int id=reader.getId();String typeName=reader.getTypename();int maxborrownum=reader.getMaxborrownum();int limit=reader.getLimit();result=dao.update(sql, id,typeName,maxborrownum,limit);dao.close();return result;}//修改指定编号的读者类型信息public static int updateReaderType(Integer id, String typename, Integer num, Integer limit){int result=0;CommonDao dao =new CommonDao();String sql="update readertype set typename=?,maxborrownum=?,limit=?where id=?";result=dao.update(sql, typename,num,limit,id);dao.close();return result;}//修改读者的所有信息public static int updateReaderType(ReaderType reader){int result=0;String sql="update readertype set typename=?,maxborrownum=?,limit=? where id=?";CommonDao dao=new CommonDao();int id=reader.getId();String typeName=reader.getTypename();int maxborrownum=reader.getMaxborrownum();int limit=reader.getLimit();result=dao.update(sql,typeName,maxborrownum,limit,id);dao.close();return result;}//删除指定类型编号的读者类型。public static int deleteReaderType(Integer id){int result=0;CommonDao dao =new CommonDao();String sql="delete  from readertype where id=?";result=dao.update(sql,id);dao.close();return result;}}

七、UsersDao.java.包含检查用户名和密码是否有效、查询用户名是否有效、查询密码是否有效、查询所有用户信息、添加用户、修改指定编号的用户的密码、

修改指定用户的密码、删除指定编号的用户信息。

package pdsu.bbm.dao;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import pdsu.bbm.model.Users;public class UsersDao {//判断用户名和密码是否有效public static Users check(Users users) {Users u = null;CommonDao dao = new CommonDao();String sql = "select * from users where name=? and password=?";String name = users.getName();String password = users.getPassword();ResultSet rs = dao.query(sql, name, password);try {if (rs.next()) {u = new Users();u.setName(rs.getString("name").trim());u.setPassword(rs.getString("password").trim());}} catch (SQLException e) {e.printStackTrace();}return u;}//检查密码public static Users checkPassword(Users users){Users u=null;CommonDao dao=new CommonDao();String sql ="select password from users where name=?";String password=users.getPassword();ResultSet rs=dao.query(sql, users.getName());try {if(rs.next()){u=new Users();u.setPassword(rs.getString("password"));}} catch (SQLException e) {e.printStackTrace();}return u;}//检查名字public static Users checkName(Users users){Users u=null;CommonDao dao =new CommonDao();String sql="select name from users where name=?";ResultSet rs=dao.query(sql, users.getName());try {if(rs.next()){u=new Users();u.setName(rs.getString("name"));}} catch (SQLException e) {e.printStackTrace();}return u;}//查询所有用户信息;public static List<Users> selectUser(){List<Users> list =null;CommonDao dao =new CommonDao();String sql="select * from users";ResultSet rs=dao.query(sql);list =new ArrayList<Users>();try {while(rs.next()){Users u=new Users();u.setId(rs.getInt("id"));u.setName(rs.getString("name").trim());u.setPassword(rs.getString("password").trim());list.add(u);}} catch (SQLException e) {e.printStackTrace();}dao.close();return list;}//添加用户public static int insertUser(Users users){int result=0;CommonDao dao =new CommonDao();String sql="insert into users(name,password) values(?,?)";result=dao.update(sql,users.getName(),users.getPassword());dao.close();return result;}//修改指定编号的用户的密码public static int updateUserPWD(Users users){int result=0;CommonDao dao =new CommonDao();String sql="update users set name=? password=? where id=?";result=dao.update(sql, users.getName(),users.getPassword());dao.close();return result;} //修改指定用户的密码、public static int updateUserPWDByName(Users users){int result=0;CommonDao dao =new CommonDao();String sql="update users set password=? where name=?";result=dao.update(sql,users.getPassword(),users.getName());dao.close();return result;} //删除指定编号的用户信息。public static int deleteUser(Integer id){int result=0;CommonDao dao =new CommonDao();String sql="delete   from users where id=?";try {result=dao.update(sql,id);} catch (Exception e) {e.printStackTrace();}dao.close();return result;}}


                                             
0 0
原创粉丝点击