package PublicModule;
public class DaoFactory {
public static IUserDao getIUserDaoInsanse() {
return new UserDaoImpl();
}
public static IBookDao getIBookDaoDAOInsanse() {
return new BookDaoImpl();
}
}
User.java
[Java] 纯文本查看复制代码
package PublicModule;
/**
* 用户实体
* @author Administrator
*
*/
public class User {
private String username; // 用户名
private String password; // 密码
private String rid; //读者编号
private String btime; //借阅时间
private String id; //书号
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String rid) {
super();
// TODO Auto-generated constructor stub
this.rid=rid;
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public User(String id,String rid,String btime) {
super();
this.id=id;
this.rid=rid;
this.btime=btime;
// TODO Auto-generated constructor stub
}
public String getUserName() {
return username;
}
public void setUserName(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getrid() {
return rid;
}
public void setrid(String rid) {
this.rid = rid;
}
public String getbtime() {
return btime;
}
public void sebtime(String btime) {
this.btime = btime;
}
public String getid() {
return id;
}
public void setid(String id) {
this.id = id;
}
}
IUserDao.java
[Java] 纯文本查看复制代码
package PublicModule;
import java.sql.Connection;
import java.sql.ResultSet;
public interface IUserDao {
//登录验证
public User loginAdmin(Connection con,User user) throws Exception;
public User loginUser(Connection con,User user) throws Exception;
//存在查询
public User CheckExist(Connection con,User user) throws Exception;
//读者信息查询
public ResultSet Readlist(Connection con,User user)throws Exception;
//借阅信息查询
public ResultSet borrowlist(Connection con,User user)throws Exception;
//添加借书信息
public int addborrow(Connection con,User user)throws Exception;
//添加还书时间
public int updateback(Connection con,User user)throws Exception;
}
UserDaoImpl.java
[Java] 纯文本查看复制代码
package PublicModule;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDaoImpl implements IUserDao {
/**
* 登录验证
* @param con
* @param user
* @return
* @throws Exception
*/
public User loginAdmin(Connection con,User user) throws Exception{
User resultUser=null;
String sql="select * from user where username=? and password=? and is_admin='1'";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
resultUser=new User();
resultUser.setUserName(rs.getString("username"));
resultUser.setPassword(rs.getString("password"));
}
return resultUser;
}
public User loginUser(Connection con,User user) throws Exception{
User resultUser=null;
String sql="select * from user where username=? and password=? and is_admin='0'";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
resultUser=new User();
resultUser.setUserName(rs.getString("username"));
resultUser.setPassword(rs.getString("password"));
}
return resultUser;
}
//存在查询
public User CheckExist(Connection con,User user) throws Exception{
User resultUser=null;
String sql="select * from user where username=? ";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
resultUser=new User();
resultUser.setUserName(rs.getString("username"));
}
return resultUser;
}
//读者信息查询
public ResultSet Readlist(Connection con, User user) throws Exception {
// TODO Auto-generated method stub
StringBuffer sb=new StringBuffer("select * from reader where 1=1");
if(user.getrid()!=null){
sb.append(" and id='"+user.getrid()+"'");
}
PreparedStatement pstmt=con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
//借阅信息查询
public ResultSet borrowlist(Connection con, User user) throws Exception {
// TODO Auto-generated method stub
StringBuffer sb=new StringBuffer("select * from borrow where 1=1");
if(user.getrid()!=null){
sb.append(" and reader_id='"+user.getrid()+"' and if_back='0' ");
}
PreparedStatement pstmt=con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
//借阅添加
@Override
public int addborrow(Connection con, User user) throws Exception {
// TODO Auto-generated method stub
String sql="insert into borrow values(null,?,?,?,null,'0')";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, user.getid());
pstmt.setString(2, user.getrid());
pstmt.setString(3, user.getbtime());
return pstmt.executeUpdate();
}
//修改已还
public int updateback(Connection con,User user) throws Exception {
// TODO Auto-generated method stub
String sql="update borrow set back_date=?,if_back='1' where book_id=? and reader_id=? ";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, user.getbtime());
pstmt.setString(2, user.getid());
pstmt.setString(3, user.getrid());
return pstmt.executeUpdate();
}
}
Book.java
[Java] 纯文本查看复制代码
package PublicModule;
public class Book {
private String id; //图书编号
private String bookname; //图书名称
private String booktype; //图书类型
private String author; //作者
private String translator; //译者
private String publisher; //出版社
private String publishe_time; //出版时间
private String price; //价格
private String stock; //库存
private String readname;//读者姓名
private String readtype;//读者类型
private String sex;//性别
private String max_num;//最大借阅数量
private String days_num;//最大节约天数
//注册用户
private String username; // 用户名
private String password; // 密码
private String is_admin; //权限
private String rid;//读者编号
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(String username,String readname,String readtype,String sex,String max_num,String days_num) {
super();
// TODO Auto-generated constructor stub
this.username=username;
this.readname=readname;
this.readtype=readtype;
this.sex=sex;
this.max_num=max_num;
this.days_num=days_num;
}
public Book(String username,String password) {
super();
// TODO Auto-generated constructor stub
this.username=username;
this.password=password;
}
public Book(String username,String password,String is_admin) {
super();
// TODO Auto-generated constructor stub
this.username=username;
this.password=password;
this.is_admin=is_admin;
}
public Book(String id) {
super();
this.id=id;
}
public Book(String bookname, String author, String publisher,String publishe_time) {
super();
this.bookname=bookname;
this.author=author;
this.publisher=publisher;
this.publishe_time=publishe_time;
}
public Book(String id,String bookname,String booktype,String author,String translator,String publisher,String publishe_time,String price,String stock) {
super();
this.id=id;
this.bookname=bookname;
this.booktype=booktype;
this.author=author;
this.translator=translator;
this.publisher=publisher;
this.publishe_time=publishe_time;
this.price=price;
this.stock=stock;
}
public String getid() {
return id;
}
public void setid(String id) {
this.id = id;
}
public String getbookname() {
return bookname;
}
public void setbookname(String bookname) {
this.bookname = bookname;
}
public String getbooktype(){
return booktype;
}
public void setbooktype(String booktype) {
this.booktype = booktype;
}
public String getauthor() {
return author;
}
public void setauthor(String author) {
this.author = author;
}
public String gettranslator() {
return translator;
}
public void settranslator(String translator) {
this.translator = translator;
}
public String getpublisher() {
return publisher;
}
public void setpublisher(String publisher) {
this.publisher = publisher;
}
public String getpublishe_time() {
return publishe_time;
}
public void setpublishe_time(String publishe_time) {
this.publishe_time = publishe_time;
}
public String getprice() {
return price;
}
public void setprice(String price) {
this.price = price;
}
public String getstock() {
return stock;
}
public void setstock(String stock) {
this.stock = stock;
}
public String getusername() {
return username;
}
public void setusername(String username) {
this.username = username;
}
public String getpassword() {
return password;
}
public void setpassword(String password) {
this.password = password;
}
public String getis_admin() {
return is_admin;
}
public void setis_admin(String is_admin) {
this.is_admin = is_admin;
}
public String getreadname() {
return readname;
}
public void setreadname(String readname) {
this.readname = readname;
}
public String getreadtype() {
return readtype;
}
public void setreadtype(String readtype) {
this.readtype = readtype;
}
public String getsex() {
return sex;
}
public void setsex(String sex) {
this.sex = sex;
}
public String getmax_num() {
return max_num;
}
public void setmax_num(String max_num) {
this.max_num = max_num;
}
public String getdays_num() {
return days_num;
}
public void setdays_num(String days_num) {
this.days_num = days_num;
}
public String getrid() {
return rid;
}
public void setrid(String rid) {
this.rid = rid;
}
}
IBookDao.java
[Java] 纯文本查看复制代码
package PublicModule;
import java.sql.Connection;
import java.sql.ResultSet;
public interface IBookDao {
//图书添加
public int add(Connection con,Book book)throws Exception;
//按图书编号图书所有信息查询
public ResultSet list(Connection con,Book book)throws Exception;
//图书信息删除
public int delete(Connection con,String id)throws Exception;
//图书信息修改
public int update(Connection con,Book book)throws Exception;
//注册用户
public int RegistUser(Connection con,Book book)throws Exception;
//修改密码
public int updatePassword(Connection con,Book book)throws Exception;
//按条件查询所有图书
public ResultSet list1(Connection con,Book book)throws Exception;
//添加读者
public int AddReader(Connection con,Book book)throws Exception;
}
IBookDao.java
[Java] 纯文本查看复制代码
package PublicModule;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class BookDaoImpl implements IBookDao {
//图书添加
@Override
public int add(Connection con, Book book) throws Exception {
// TODO Auto-generated method stub
String sql="insert into book values(?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, book.getid());
pstmt.setString(2, book.getbookname());
pstmt.setString(3, book.getbooktype());
pstmt.setString(4, book.getauthor());
pstmt.setString(5, book.gettranslator());
pstmt.setString(6, book.getpublisher());
pstmt.setString(7, book.getpublishe_time());
pstmt.setString(8, book.getprice());
pstmt.setString(9, book.getstock());
return pstmt.executeUpdate();
}
//注册用户
public int RegistUser(Connection con, Book book) throws Exception {
// TODO Auto-generated method stub
String sql="insert into user values(null,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, book.getusername());
pstmt.setString(2, book.getpassword());
pstmt.setString(3, book.getis_admin());
return pstmt.executeUpdate();
}
//添加读者信息
public int AddReader(Connection con, Book book) throws Exception {
// TODO Auto-generated method stub
String sql="insert into reader values(?,?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, book.getusername());
pstmt.setString(2, book.getreadname());
pstmt.setString(3, book.getreadtype());
pstmt.setString(4, book.getsex());
pstmt.setString(5, book.getmax_num());
pstmt.setString(6, book.getdays_num());
return pstmt.executeUpdate();
}
//修改密码
public int updatePassword(Connection con, Book book) throws Exception {
// TODO Auto-generated method stub
String sql="update user set password=? where username=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, book.getpassword());
pstmt.setString(2, book.getusername());
return pstmt.executeUpdate();
}
//图书信息查询(所有)
@Override
public ResultSet list(Connection con, Book book) throws Exception {
// TODO Auto-generated method stub
StringBuffer sb=new StringBuffer("select * from book where 1=1");
if(book.getid()!=null){
sb.append(" and id='"+book.getid()+"'");
}
PreparedStatement pstmt=con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
//按条件查询所有图书信息
public ResultSet list1(Connection con, Book book) throws Exception {
// TODO Auto-generated method stub
StringBuffer sb=new StringBuffer("select * from book where 1=1");
if(book.getbookname()!=null){
sb.append(" and bookname like '"+book.getbookname()+"'");
}
if(book.getauthor()!=null){
sb.append(" and author like '"+book.getauthor()+"'");
}
if(book.getpublisher()!=null){
sb.append(" and publisher = '"+book.getpublisher()+"'");
}
if(book.getpublishe_time()!=null){
sb.append(" and publish_time = '"+book.getpublishe_time()+"'");
}
PreparedStatement pstmt=con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
//图书信息删除
@Override
public int delete(Connection con, String id) throws Exception {
// TODO Auto-generated method stub
String sql="delete from book where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
//图书信息修改
@Override
public int update(Connection con, Book book) throws Exception {
// TODO Auto-generated method stub
String sql="update book set bookname=?,booktype=?,author=?,translator=?,publisher=?,publish_time=?,price=?,stock=? where id=? ";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, book.getbookname());
pstmt.setString(2, book.getbooktype());
pstmt.setString(3, book.getauthor());
pstmt.setString(4, book.gettranslator());
pstmt.setString(5, book.getpublisher());
pstmt.setString(6, book.getpublishe_time());
pstmt.setString(7, book.getprice());
pstmt.setString(8, book.getstock());
pstmt.setString(9, book.getid());
return pstmt.executeUpdate();
}
}