跟着网上的视频学的,还有个别地方写的不够好,主要学习dao模式,这里其中的BaseUtil类不错
DVD.java
1 package com.dvd.entiy; 2 3 public class DVD { 4 private int id; 5 private String dname; 6 private int dcount; 7 private int status; 8 9 public DVD() { 10 11 } 12 13 public DVD(String dname, int dcount, int status) { 14 super(); 15 this.dname = dname; 16 this.dcount = dcount; 17 this.status = status; 18 } 19 20 public DVD(int id, String dname, int dcount, int status) { 21 super(); 22 this.id = id; 23 this.dname = dname; 24 this.dcount = dcount; 25 this.status = status; 26 } 27 28 public int getId() { 29 return id; 30 } 31 32 public void setId(int id) { 33 this.id = id; 34 } 35 36 public String getDname() { 37 return dname; 38 } 39 40 public void setDname(String duname) { 41 this.dname = dname; 42 } 43 44 public int getDcount() { 45 return dcount; 46 } 47 48 public void setDcount(int dcount) { 49 this.dcount = dcount; 50 } 51 52 public int getStatus() { 53 return status; 54 } 55 56 public void setStatus(int status) { 57 this.status = status; 58 } 59 60 }
User.java
1 package com.dvd.entiy; 2 3 public class User { 4 private int id; 5 private String uname; 6 private String upass; 7 private int type; 8 9 public User() { 10 11 } 12 13 public User(String uname, String upass, int type) { 14 super(); 15 this.uname = uname; 16 this.upass = upass; 17 this.type = type; 18 } 19 20 public User(int id, String uname, String upass, int type) { 21 super(); 22 this.id = id; 23 this.uname = uname; 24 this.upass = upass; 25 this.type = type; 26 } 27 28 public int getId() { 29 return id; 30 } 31 32 public void setId(int id) { 33 this.id = id; 34 } 35 36 public String getUname() { 37 return uname; 38 } 39 40 public void setUname(String uname) { 41 this.uname = uname; 42 } 43 44 public String getUpass() { 45 return upass; 46 } 47 48 public void setUpass(String upass) { 49 this.upass = upass; 50 } 51 52 public int getType() { 53 return type; 54 } 55 56 public void setType(int type) { 57 this.type = type; 58 } 59 60 }
Record.java
1 package com.dvd.entiy; 2 3 public class Record { 4 private int id; 5 private int uid; 6 private int did; 7 private String lendTime; 8 private String returnTime; 9 10 public Record() { 11 12 } 13 14 public Record(int uid, int did, String lendTime, String returnTime) { 15 super(); 16 this.uid = uid; 17 this.did = did; 18 this.lendTime = lendTime; 19 this.returnTime = returnTime; 20 } 21 22 public Record(int id, int uid, int did, String lendTime, String returnTime) { 23 super(); 24 this.id = id; 25 this.uid = uid; 26 this.did = did; 27 this.lendTime = lendTime; 28 this.returnTime = returnTime; 29 } 30 31 public int getId() { 32 return id; 33 } 34 35 public void setId(int id) { 36 this.id = id; 37 } 38 39 public int getUid() { 40 return uid; 41 } 42 43 public void setUid(int uid) { 44 this.uid = uid; 45 } 46 47 public int getDid() { 48 return did; 49 } 50 51 public void setDid(int did) { 52 this.did = did; 53 } 54 55 public String getLendTime() { 56 return lendTime; 57 } 58 59 public void setLendTime(String lendTime) { 60 this.lendTime = lendTime; 61 } 62 63 public String getReturnTime() { 64 return returnTime; 65 } 66 67 public void setReturnTime(String returnTime) { 68 this.returnTime = returnTime; 69 } 70 71 }
Record2.java
1 package com.dvd.entiy; 2 3 public class Record2 { 4 private int id;// 记录id 5 private int did;// 影碟id 6 private String uname;// 用户名 7 private String dname;// 影碟名字 8 private String lendTime;// 借出时间 9 private String returnTime;// 归还时间 10 11 public Record2() { 12 13 } 14 15 public Record2(String uname, String dname, String lendTime, String returnTime) { 16 super(); 17 this.uname = uname; 18 this.dname = dname; 19 this.lendTime = lendTime; 20 this.returnTime = returnTime; 21 } 22 23 public Record2(int id, String uname, String dname, String lendTime, String returnTime) { 24 super(); 25 this.id = id; 26 this.uname = uname; 27 this.dname = dname; 28 this.lendTime = lendTime; 29 this.returnTime = returnTime; 30 } 31 32 public int getId() { 33 return id; 34 } 35 36 public void setId(int id) { 37 this.id = id; 38 } 39 40 public int getDid() { 41 return did; 42 } 43 44 public void setDid(int did) { 45 this.did = did; 46 } 47 48 public String getUname() { 49 return uname; 50 } 51 52 public void setUname(String uname) { 53 this.uname = uname; 54 } 55 56 public String getDname() { 57 return dname; 58 } 59 60 public void setDname(String dname) { 61 this.dname = dname; 62 } 63 64 public String getLendTime() { 65 return lendTime; 66 } 67 68 public void setLendTime(String lendTime) { 69 this.lendTime = lendTime; 70 } 71 72 public String getReturnTime() { 73 return returnTime; 74 } 75 76 public void setReturnTime(String returnTime) { 77 this.returnTime = returnTime; 78 } 79 80 }
DVDDao.java
1 package com.dvd.dao; 2 3 import java.util.List; 4 5 import com.dvd.entiy.DVD; 6 7 public interface DVDDao { 8 public boolean saveDVD(DVD dvd);// 添加DVD 9 10 public boolean updateDVD(DVD dvd);// 更新DVD 11 12 public boolean delDVD(int did);// 删除指定的DVD 13 14 public List<DVD> queryDVDs();// 查询所有的DVD 15 16 public List<DVD> queryDVDByName(String name);// 查找指定名字的DVD 17 18 public List<DVD> querySortByLimit(int index, int number);// 查找指定起始位,指定个数的DVD 19 20 public DVD queryDVDById(int did);// 根据dvd编号来查询DVD 21 22 public List<DVD> queryDVDByStatus(int status);// 根据状态查询 DVD 23 24 }
RecordDao.java
1 package com.dvd.dao; 2 3 import java.util.List; 4 5 import com.dvd.entiy.Record; 6 import com.dvd.entiy.Record2; 7 8 public interface RecordDao { 9 public Record queryRecordById(int rid);// 查看指定id的DVD借还记录 10 11 public boolean saveRecord(Record record);// 保存record记录 12 13 public boolean updateRecord(Record record);// 更新Record记录 14 15 public List<Record2> queryAllRecord();// 查询所有的DVD借还记录 16 17 public List<Record2> queryRecordByUname(String uname);// 查看指定用户的dvd借还记录 18 19 public List<Record2> queryRecordByDname(String dname);// 查看指定dvd的借还记录 20 21 public List<Record2> queryUserRecordByReturnTime(boolean flag, String uname);// 查看用户归还 22 // 记录(已归还 23 // ,未归还) 24 }
UserDao.java
1 package com.dvd.dao; 2 3 import com.dvd.entiy.User; 4 5 public interface UserDao { 6 public boolean saveUser(User user);// 添加用户 7 8 public boolean delUser(int id);// 删除用户 9 10 public boolean updateUser(User user);// 更新用户 11 12 public User queryUser(User user);// 查询用户 13 }
BaseDao.java
1 package com.dvd.dao.impl; 2 3 import java.lang.reflect.Field; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.ResultSetMetaData; 9 import java.sql.SQLException; 10 import java.util.ArrayList; 11 import java.util.List; 12 13 public class BaseDao { 14 private static final String DRIVER = "com.mysql.jdbc.Driver"; 15 private static final String URL = "jdbc:mysql://localhost:3306/dvddb"; 16 17 /** 18 * 与数据库建立连接 19 * 20 * @return 数据库连接对象 21 */ 22 public Connection getConn() { 23 Connection conn = null; 24 try { 25 Class.forName(DRIVER); 26 } catch (ClassNotFoundException e) { 27 // TODO 自动生成的 catch 块 28 e.printStackTrace(); 29 } 30 try { 31 conn = DriverManager.getConnection(URL, "root", "123456"); 32 } catch (SQLException e) { 33 // TODO 自动生成的 catch 块 34 e.printStackTrace(); 35 } 36 return conn; 37 } 38 39 /** 40 * 释放相应的资源 41 * 42 * @param rs 43 * @param pstmt 44 * @param conn 45 */ 46 public void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn) { 47 try { 48 if (rs != null) { 49 rs.close(); 50 } 51 if (pstmt != null) { 52 pstmt.close(); 53 } 54 if (conn != null) { 55 conn.close(); 56 } 57 } catch (SQLException e) { 58 // TODO 自动生成的 catch 块 59 e.printStackTrace(); 60 } 61 } 62 63 /** 64 * 此方法可以完成增删改查 65 * 66 * @param sql 67 * @param params 68 * @return 69 */ 70 public boolean operUpdate(String sql, List<Object> params) { 71 int res = 0;// 影响的行数 72 Connection conn = null; 73 PreparedStatement pstmt = null; 74 ResultSet rs = null; 75 try { 76 conn = getConn(); 77 pstmt = conn.prepareStatement(sql);// 装在sql语句 78 if (params != null) { 79 // 假如有?在执行之前把?点位符替换掉 80 for (int i = 0; i < params.size(); i++) { 81 pstmt.setObject(i + 1, params.get(i)); 82 } 83 } 84 res = pstmt.executeUpdate(); 85 } catch (SQLException e) { 86 // TODO 自动生成的 catch 块 87 e.printStackTrace(); 88 } finally { 89 closeAll(rs, pstmt, conn); 90 } 91 return res > 0 ? true : false; 92 } 93 94 /** 95 * 使用泛型方法和反射机制进行封装 96 * 97 * @param sql 98 * @param params 99 * @param cls 100 * @return 101 * @throws IllegalAccessException 102 * @throws InstantiationException 103 * @throws SecurityException 104 * @throws NoSuchFieldException 105 */ 106 107 public <T> List<T> operQuery(String sql, List<Object> params, Class<T> cls) 108 throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException { 109 Connection conn = null; 110 PreparedStatement pstmt = null; 111 ResultSet rs = null; 112 List<T> data = new ArrayList<T>(); 113 try { 114 conn = getConn(); 115 pstmt = conn.prepareStatement(sql);// 装在sql语句 116 if (params != null) { 117 // 假如有?在执行之前把?点位符替换掉 118 for (int i = 0; i < params.size(); i++) { 119 pstmt.setObject(i + 1, params.get(i)); 120 } 121 } 122 rs = pstmt.executeQuery(); 123 // 把查询出来的记录封装成对应 的实体类对象 124 ResultSetMetaData rsd = rs.getMetaData();// 得到记录集元数据对象 125 // 通过此对象可以得到表的结构 ,列名,列的个数 ,列的数据类型 126 while (rs.next()) { 127 T m = cls.newInstance(); 128 for (int i = 0; i < rsd.getColumnCount(); i++) { 129 String col_name = rsd.getColumnName(i + 1);// 获得列名 130 Object value = rs.getObject(col_name);// 获得对应的值 131 Field field = cls.getDeclaredField(col_name); 132 field.setAccessible(true);// 给私有属性设置可访问权 133 field.set(m, value);// 给对象私有属性赋值 134 135 } 136 data.add(m); 137 } 138 } catch (SQLException e) { 139 // TODO 自动生成的 catch 块 140 e.printStackTrace(); 141 } finally { 142 closeAll(rs, pstmt, conn); 143 } 144 return data; 145 } 146 }
DVDDaoImpl.java
1 package com.dvd.dao.impl; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.dvd.dao.DVDDao; 7 import com.dvd.entiy.DVD; 8 9 public class DVDDaoImpl extends BaseDao implements DVDDao { 10 11 @Override 12 public boolean saveDVD(DVD dvd) { 13 String sql = "insert into dvds(dname,dcount,status) values(?,?,?)"; 14 List<Object> params = new ArrayList<Object>(); 15 params.add(dvd.getDname()); 16 params.add(dvd.getDcount()); 17 params.add(dvd.getStatus()); 18 return this.operUpdate(sql, params); 19 20 } 21 22 @Override 23 public boolean updateDVD(DVD dvd) { 24 String sql = "update dvds set dname=?,dcount=?,status=? where id=?"; 25 List<Object> params = new ArrayList<Object>(); 26 params.add(dvd.getDname()); 27 params.add(dvd.getDcount()); 28 params.add(dvd.getStatus()); 29 params.add(dvd.getId()); 30 return this.operUpdate(sql, params); 31 } 32 33 @Override 34 public boolean delDVD(int did) { 35 String sql = "delete from dvds where id=?"; 36 List<Object> params = new ArrayList<Object>(); 37 params.add(did); 38 return this.operUpdate(sql, params); 39 } 40 41 @Override 42 public List<DVD> queryDVDs() { 43 String sql = "select id,dname,dcount,status from dvds"; 44 List<DVD> dList = null; 45 try { 46 dList = this.operQuery(sql, null, DVD.class); 47 } catch (Exception e) { 48 // TODO 自动生成的 catch 块 49 e.printStackTrace(); 50 } 51 return dList; 52 } 53 54 @Override 55 public List<DVD> queryDVDByName(String dname) { 56 String sql = "select id,dname,dcount,status from dvds where dname=?"; 57 List<DVD> dList = null; 58 List<Object> params = new ArrayList<Object>(); 59 params.add(dname); 60 try { 61 dList = this.operQuery(sql, params, DVD.class); 62 } catch (Exception e) { 63 // TODO 自动生成的 catch 块 64 e.printStackTrace(); 65 } 66 return dList; 67 } 68 69 /** 70 * 查询热门dvd就是借出次数前五位的dvd 71 * 72 * @param index 73 * @param number 74 * @return 75 */ 76 @Override 77 public List<DVD> querySortByLimit(int index, int number) { 78 String sql = "select id,dname,dcount,status from dvds order by dcount desc limit" + index + "," + number; 79 List<DVD> dList = null; 80 try { 81 dList = this.operQuery(sql, null, DVD.class); 82 } catch (Exception e) { 83 // TODO 自动生成的 catch 块 84 e.printStackTrace(); 85 } 86 return dList; 87 } 88 89 @Override 90 public DVD queryDVDById(int did) { 91 String sql = "select id,dname,dcount,status from dvds where id=?"; 92 List<DVD> dList = null; 93 List<Object> params = new ArrayList<Object>(); 94 params.add(did); 95 try { 96 dList = this.operQuery(sql, params, DVD.class); 97 } catch (Exception e) { 98 // TODO 自动生成的 catch 块 99 e.printStackTrace(); 100 } 101 if (dList.size() > 0) { 102 return dList.get(0); 103 } 104 return null; 105 } 106 107 @Override 108 public List<DVD> queryDVDByStatus(int status) { 109 String sql = "select id,dname,dcount,status from dvds where status=?"; 110 List<DVD> dList = null; 111 List<Object> params = new ArrayList<Object>(); 112 params.add(status); 113 try { 114 dList = this.operQuery(sql, params, DVD.class); 115 } catch (Exception e) { 116 // TODO 自动生成的 catch 块 117 e.printStackTrace(); 118 } 119 return dList; 120 } 121 122 }
RecordDaoImp.java
1 package com.dvd.dao.impl; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.dvd.dao.RecordDao; 7 import com.dvd.entiy.Record; 8 import com.dvd.entiy.Record2; 9 10 public class RecordDaoImpl extends BaseDao implements RecordDao { 11 12 @Override 13 public Record queryRecordById(int rid) { 14 List<Record> rList = null; 15 String sql = "select id,uid,did,lendTime,returnTime from records where did=? order by id desc"; 16 List<Object> params = new ArrayList<Object>(); 17 params.add(rid); 18 try { 19 rList = this.operQuery(sql, params, Record.class); 20 } catch (Exception e) { 21 // TODO 自动生成的 catch 块 22 e.printStackTrace(); 23 } 24 if (rList.size() > 0) { 25 return rList.get(0); 26 } 27 return null; 28 } 29 30 @Override 31 public boolean saveRecord(Record record) { 32 String sql = "insert into records(uid,did,lendTime,returnTime) values(?,?,?,?)"; 33 List<Object> params = new ArrayList<Object>(); 34 params.add(record.getUid()); 35 params.add(record.getDid()); 36 params.add(record.getLendTime()); 37 params.add(record.getReturnTime()); 38 39 return this.operUpdate(sql, params); 40 } 41 42 @Override 43 public boolean updateRecord(Record record) { 44 String sql = "update records set uid=?,did=?,lendTime=?,returnTime=? where id=?"; 45 List<Object> params = new ArrayList<Object>(); 46 params.add(record.getUid()); 47 params.add(record.getDid()); 48 params.add(record.getLendTime()); 49 params.add(record.getReturnTime()); 50 params.add(record.getId()); 51 return this.operUpdate(sql, params); 52 } 53 54 @Override 55 public List<Record2> queryAllRecord() { 56 List<Record2> data = null; 57 String sql = "select r.id,d.id as did,u.uname,d.dname,r.lentTime,r.returnTime from users u,dvds d,records r where u.id=r.uid and d.id =r.did"; 58 try { 59 data = this.operQuery(sql, null, Record2.class); 60 } catch (Exception e) { 61 // TODO 自动生成的 catch 块 62 e.printStackTrace(); 63 } 64 return data; 65 } 66 67 @Override 68 public List<Record2> queryRecordByUname(String uname) { 69 List<Record2> data = null; 70 String sql = "select r.id,d.id as did,u.uname,d.dname,r.lendTime,r.returnTime from" 71 + " users u,dvds d,records r where u.id=r.uid and d.id =r.did and uname=?"; 72 List<Object> params = new ArrayList<Object>(); 73 params.add(uname); 74 try { 75 data = this.operQuery(sql, params, Record2.class); 76 } catch (Exception e) { 77 // TODO 自动生成的 catch 块 78 e.printStackTrace(); 79 } 80 return data; 81 } 82 83 @Override 84 public List<Record2> queryRecordByDname(String dname) { 85 List<Record2> data = null; 86 String sql = "select r.id,d.id as did,u.uname,d.dname,r.lendTime,r.returnTime from users u," 87 + "dvds d,records r where u.id=r.uid and d.id =r.did and dname=?"; 88 List<Object> params = new ArrayList<Object>(); 89 params.add(dname); 90 try { 91 data = this.operQuery(sql, params, Record2.class); 92 } catch (Exception e) { 93 // TODO 自动生成的 catch 块 94 e.printStackTrace(); 95 } 96 return data; 97 } 98 99 /** 100 * // 查看用户归还记录(已归还 ,未归还) 101 */ 102 @Override 103 public List<Record2> queryUserRecordByReturnTime(boolean flag, String uname) { 104 List<Record2> rList = null; 105 String sql = null; 106 if (flag) { 107 sql = "select r.id,d.id as did,u.uname,d.dname,r.lentTime,r.returnTime from " 108 + "uses u,dvds d,records r where u.id=r.uid and d.id =r.did and returnTime is not null and uname=?"; 109 } else { 110 sql = "select r.id,d.id as did,u.uname,d.dname,r.lentTime,r.returnTime from " 111 + "uses u,dvds d,records r where u.id=r.uid and d.id =r.did and returnTime is null and uname=?"; 112 } 113 List<Object> params = new ArrayList<Object>(); 114 params.add(uname); 115 try { 116 rList = this.operQuery(sql, params, Record2.class); 117 } catch (Exception e) { 118 e.printStackTrace(); 119 } 120 return rList; 121 } 122 123 }
UserDaoImpl.java
1 package com.dvd.dao.impl; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.dvd.dao.UserDao; 7 import com.dvd.entiy.User; 8 9 public class UserDaoImpl extends BaseDao implements UserDao { 10 11 @Override 12 public boolean saveUser(User user) { 13 String sql = "insert into users(uname,upass,type) values(?,?,?)"; 14 List<Object> params = new ArrayList<Object>(); 15 params.add(user.getUname()); 16 params.add(user.getUpass()); 17 params.add(user.getType()); 18 return this.operUpdate(sql, params); 19 } 20 21 @Override 22 public boolean delUser(int id) { 23 String sql = "delete from users where id=?"; 24 List<Object> params = new ArrayList<Object>(); 25 params.add(id); 26 return this.operUpdate(sql, params); 27 } 28 29 @Override 30 public boolean updateUser(User user) { 31 String sql = "updata users set uname=?,upass=?,type=? where id=?"; 32 List<Object> params = new ArrayList<Object>(); 33 params.add(user.getUname()); 34 params.add(user.getUpass()); 35 params.add(user.getType()); 36 params.add(user.getId()); 37 return this.operUpdate(sql, params); 38 } 39 40 @Override 41 public User queryUser(User user) { 42 List<User> uList = null; 43 String sql = "select id,uname,upass,type from users where uname=? and upass=? and type=?"; 44 List<Object> params = new ArrayList<Object>(); 45 params.add(user.getUname()); 46 params.add(user.getUpass()); 47 params.add(user.getType()); 48 try { 49 uList = this.operQuery(sql, params, User.class); 50 } catch (Exception e) { 51 } 52 53 if (uList.size() > 0) { 54 return uList.get(0); 55 } 56 return null; 57 } 58 59 }
DVDBiz.java
1 package com.dvd.biz; 2 3 import java.util.List; 4 5 import com.dvd.entiy.DVD; 6 7 public interface DVDBiz { 8 public boolean addDVD(DVD dvd);// 添加dvd 9 10 public boolean delDVD(int did);// 删除dvd 11 12 public boolean modifyDVD(DVD dvd);// 修改dvd 13 14 public List<DVD> queryAllDVDs();// 查询所有的dvd信息 15 16 public List<DVD> ranking_top_five();// 查看前5张最爱欢迎的dvd(热门dvd) 17 18 public List<DVD> queryDVDByName(String dname);// 根据DVD的名字来查询 19 20 public DVD queryDVDById(int did);// 根据id来查询 DVD 21 22 public int lendDVD(int did, int uid);// 按dvd编号和用户编号来租DVD 23 24 public int returnDVD(int rid);// 还DVD的功能 25 26 public List<DVD> canLendDVD();// 可借DVD 27 28 public List<DVD> hasLendedDvD();// 不可DVD 29 30 }
RecordBiz.java
1 package com.dvd.biz; 2 3 import java.util.List; 4 5 import com.dvd.entiy.Record2; 6 7 public interface RecordBiz { 8 public List<Record2> queryUserRecords(String uname);// 查看指定用户的租赁记录 9 10 public List<Record2> queryDVDRecords(String dname);// 查看指定DVD的租赁记录 11 12 public List<Record2> queryHasReturnRecords(String uname);// 查看指定用户已归还 的记录 13 14 public List<Record2> queryNoReturnRecords(String uname);// 查看指定用户未归还 的记录 15 16 public List<Record2> queryAllRecords();// 查看所有的记录 17 }
UserBiz.java
package com.dvd.biz; import com.dvd.entiy.User; public interface UserBiz { // 用户登录,返回的就是登录用户的信息(对象) public User login(User user); // 注册用户 public int registerUser(User user); }
DVDBizImpl.java
1 package com.dvd.biz.Impl; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 import java.util.List; 6 7 import com.dvd.biz.DVDBiz; 8 import com.dvd.dao.DVDDao; 9 import com.dvd.dao.RecordDao; 10 import com.dvd.dao.impl.DVDDaoImpl; 11 import com.dvd.dao.impl.RecordDaoImpl; 12 import com.dvd.entiy.DVD; 13 import com.dvd.entiy.Record; 14 15 public class DVDBizImpl implements DVDBiz { 16 private DVDDao dvdDao = null; 17 private RecordDao recordDao = null; 18 19 public DVDBizImpl() { 20 dvdDao = new DVDDaoImpl(); 21 recordDao = new RecordDaoImpl(); 22 } 23 24 @Override 25 public boolean addDVD(DVD dvd) { 26 27 return dvdDao.saveDVD(dvd); 28 } 29 30 @Override 31 public boolean delDVD(int did) { 32 33 return dvdDao.delDVD(did); 34 } 35 36 @Override 37 public boolean modifyDVD(DVD dvd) { 38 return dvdDao.updateDVD(dvd); 39 } 40 41 @Override 42 public List<DVD> queryAllDVDs() { 43 return dvdDao.queryDVDs(); 44 } 45 46 @Override 47 public List<DVD> ranking_top_five() { 48 return dvdDao.querySortByLimit(0, 5); 49 } 50 51 @Override 52 public List<DVD> queryDVDByName(String dname) { 53 54 return dvdDao.queryDVDByName(dname); 55 } 56 57 @Override 58 public DVD queryDVDById(int did) { 59 return dvdDao.queryDVDById(did); 60 } 61 62 @Override 63 public int lendDVD(int did, int uid) { 64 DVD dvd = dvdDao.queryDVDById(did);// 查询 65 if (dvd == null) { 66 return 0;// 没有找到要借的dvd 67 } else { 68 if (dvd.getStatus() == 0) { 69 return 1;// 不可借。已经借出 70 } else { 71 72 dvd.setStatus(0);// 更新状态代表已经借出 73 dvd.setDcount(dvd.getDcount() + 1); 74 // System.out.println(dvd.getId()); 75 //// System.out.println(dvd.getDname()); 76 // System.out.println(dvd.getDcount()); 77 // System.out.println(dvd.getStatus()); 78 boolean flag1 = dvdDao.updateDVD(dvd);// 更新DVD 79 // System.out.println("更新dvdflag1=" + flag1); 80 Record record = new Record(uid, did, new SimpleDateFormat("yyyy-MM-dd").format(new Date()), null); 81 boolean flag2 = recordDao.saveRecord(record);// 插入租赁记录; 82 // System.out.println("更新插入租赁记录;flag2=" + flag2); 83 if (flag1 && flag2) { 84 return 2;// 借出成功 85 } else { 86 // dvd.setStatus(1); 87 // dvdDao.updateDVD(dvd); 88 return 3;// 借出失败 89 } 90 } 91 } 92 } 93 94 @Override 95 public int returnDVD(int rid) { 96 Record record = recordDao.queryRecordById(rid); 97 // System.out.println(record); 98 if (record == null) { 99 return 1;// 输入不正确 100 } else if (record.getReturnTime() != null) { 101 102 return 2;// dvd已经归还 103 } else { 104 record.setReturnTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date())); 105 boolean flag1 = recordDao.updateRecord(record);// 更新记录 106 // System.out.println("更新flag= " + flag1); 107 DVD dvd = dvdDao.queryDVDById(record.getDid());// 打到对应的dvd 108 dvd.setStatus(1);// 可借状态 109 boolean flag2 = dvdDao.updateDVD(dvd); 110 // System.out.println("更新flag2= " + flag2); 111 if (flag1 && flag2) { 112 return 3;// 归还成功 113 } else { 114 dvd.setStatus(0);// 未还状态 115 dvdDao.updateDVD(dvd); 116 return 4;// 归还失败 117 } 118 } 119 } 120 121 @Override 122 public List<DVD> canLendDVD() { 123 return dvdDao.queryDVDByStatus(1); 124 } 125 126 @Override 127 public List<DVD> hasLendedDvD() { 128 return dvdDao.queryDVDByStatus(0); 129 } 130 131 }
RecordBizImpl.java
1 package com.dvd.biz.Impl; 2 3 import java.util.List; 4 5 import com.dvd.biz.RecordBiz; 6 import com.dvd.dao.RecordDao; 7 import com.dvd.dao.impl.RecordDaoImpl; 8 import com.dvd.entiy.Record2; 9 10 public class RecordBizImpl implements RecordBiz { 11 private RecordDao recordDao = null; 12 13 public RecordBizImpl() { 14 recordDao = new RecordDaoImpl(); 15 } 16 17 @Override 18 public List<Record2> queryUserRecords(String uname) { 19 return recordDao.queryRecordByUname(uname); 20 } 21 22 @Override 23 public List<Record2> queryDVDRecords(String dname) { 24 return recordDao.queryRecordByDname(dname); 25 } 26 27 @Override 28 public List<Record2> queryHasReturnRecords(String uname) { 29 return recordDao.queryUserRecordByReturnTime(true, uname); 30 } 31 32 @Override 33 public List<Record2> queryNoReturnRecords(String uname) { 34 // TODO 自动生成的方法存根 35 return recordDao.queryUserRecordByReturnTime(false, uname); 36 } 37 38 @Override 39 public List<Record2> queryAllRecords() { 40 // TODO 自动生成的方法存根 41 return recordDao.queryAllRecord(); 42 } 43 44 }
UserBizImpl.java
1 package com.dvd.biz.Impl; 2 3 import com.dvd.biz.UserBiz; 4 import com.dvd.dao.UserDao; 5 import com.dvd.dao.impl.UserDaoImpl; 6 import com.dvd.entiy.User; 7 8 public class UserBizImpl implements UserBiz { 9 private UserDao userDao = null; 10 11 public UserBizImpl() { 12 userDao = new UserDaoImpl(); 13 } 14 15 @Override 16 public User login(User user) { 17 return userDao.queryUser(user); 18 } 19 20 @Override 21 public int registerUser(User user) { 22 if (userDao.queryUser(user) != null) { 23 return 1;// 此用户名已经存在 24 } else { 25 boolean res = userDao.saveUser(user); 26 if (res) { 27 return 2;// 注册成功 28 } else { 29 return 3;// 注册失败 30 } 31 } 32 } 33 34 }
AdminDVDOperatorView.java
1 package com.dvd.view; 2 3 import java.awt.BorderLayout; 4 import java.awt.GridLayout; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.awt.event.ItemEvent; 8 import java.awt.event.ItemListener; 9 import java.awt.event.MouseAdapter; 10 import java.awt.event.MouseEvent; 11 import java.util.ArrayList; 12 import java.util.List; 13 14 import javax.swing.BorderFactory; 15 import javax.swing.JButton; 16 import javax.swing.JComboBox; 17 import javax.swing.JInternalFrame; 18 import javax.swing.JLabel; 19 import javax.swing.JOptionPane; 20 import javax.swing.JPanel; 21 import javax.swing.JScrollPane; 22 import javax.swing.JTable; 23 import javax.swing.JTextField; 24 import javax.swing.event.TableModelListener; 25 import javax.swing.table.TableModel; 26 27 import com.dvd.Util.DVDUtil; 28 import com.dvd.biz.DVDBiz; 29 import com.dvd.biz.Impl.DVDBizImpl; 30 import com.dvd.entiy.DVD; 31 32 public class AdminDVDOperatorView extends JInternalFrame { 33 34 private static final long serialVersionUID = -3558209667843105955L; 35 private DVDBiz dvdBiz = null; 36 private List<DVD> dvdList = null; 37 private DVDInfoTableModel infoTableModel = null; 38 39 private JPanel paneltable = null; 40 private JScrollPane jScrollPanetable = null;// 用来保存Jtable的一个面板 41 private JTable table = null;// 声明JTable 42 private JPanel panelmanage = null; 43 private JPanel manage = null;// 声明信息 44 45 private JPanel panelbutton = null;// 按钮面板 46 private JTextField tf_search = null; 47 private JButton btn_search = null; 48 private JButton btn_add = null; 49 private JButton btn_updata = null; 50 private JButton btn_del = null; 51 private JButton btn_exit = null; 52 private JComboBox<String> cb_type = null; 53 private JLabel lb_type = null; 54 55 private JLabel lb_name = null; 56 private JLabel lb_count = null; 57 private JLabel lb_status = null; 58 private JTextField tf_name = null; 59 private JTextField tf_count = null; 60 private JComboBox<String> cb_status = null; 61 62 public AdminDVDOperatorView() { 63 dvdBiz = new DVDBizImpl(); 64 init(); 65 registerListener(); 66 } 67 68 private void init() { 69 this.setTitle("DVD租赁记录查询"); 70 this.setSize(525, 450); 71 this.setIconifiable(true);// 窗体可最小化 72 this.setClosable(true);// 窗体可被关闭 73 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 74 // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 75 this.setLayout(new BorderLayout()); 76 dvdList = new ArrayList<DVD>(); 77 78 lb_name = new JLabel("DVD名字", JLabel.CENTER); 79 lb_count = new JLabel("借出次数", JLabel.CENTER); 80 lb_status = new JLabel("DVD状态", JLabel.CENTER); 81 tf_name = new JTextField(8); 82 tf_count = new JTextField(8); 83 cb_status = new JComboBox<String>(new String[] { "已借", "可借" }); 84 paneltable = new JPanel(new BorderLayout()); 85 panelmanage = new JPanel(); 86 manage = new JPanel(); 87 88 table = new JTable(); 89 90 // 让JTable绑定数据模型呈现数据 91 92 refreshTable(dvdList); 93 94 manage.add(lb_name); 95 manage.add(tf_name); 96 manage.add(lb_count); 97 manage.add(tf_count); 98 manage.add(lb_status); 99 manage.add(cb_status); 100 101 jScrollPanetable = new JScrollPane();// 创建面板 102 // 给面板设置边框 103 paneltable 104 .setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(null, null), "DVD记录查询 ")); 105 jScrollPanetable.setViewportView(table); 106 paneltable.add(jScrollPanetable); 107 panelmanage.add(manage); 108 paneltable.add(panelmanage, BorderLayout.SOUTH); 109 this.add(paneltable, BorderLayout.CENTER); 110 // 给面板设置边框 111 panelmanage.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(null, null))); 112 113 panelbutton = new JPanel(new GridLayout(9, 1, 5, 10)); 114 // 给面板设置边框 115 panelbutton.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(null, null), "查询条件")); 116 this.add(panelbutton, BorderLayout.EAST); 117 lb_type = new JLabel("查询类型"); 118 panelbutton.add(lb_type); 119 cb_type = new JComboBox<String>(new String[] { "所有DVD", "DVD编号", "DVD名字" }); 120 panelbutton.add(cb_type); 121 122 tf_search = new JTextField(); 123 panelbutton.add(tf_search); 124 125 btn_search = new JButton("查询"); 126 panelbutton.add(btn_search); 127 tf_search.setEditable(false);// 默认不可用 128 129 btn_add = new JButton("添加DVD"); 130 panelbutton.add(btn_add); 131 132 btn_updata = new JButton("更新DVD"); 133 panelbutton.add(btn_updata); 134 btn_updata.setEnabled(false);// 默认不可用 135 136 btn_del = new JButton("删除DVD"); 137 panelbutton.add(btn_del); 138 btn_del.setEnabled(false);// 默认不可用 139 140 panelbutton.add(new JLabel()); 141 this.getRootPane().setDefaultButton(btn_search);// 设置默认获取的焦点 142 143 btn_exit = new JButton("退出窗口"); 144 panelbutton.add(btn_exit); 145 146 this.setVisible(true); 147 } 148 149 private void registerListener() { 150 151 btn_exit.addActionListener(new ActionListener() { 152 153 @Override 154 public void actionPerformed(ActionEvent e) { 155 int flag = JOptionPane.showInternalConfirmDialog(AdminDVDOperatorView.this, "是否确定退出?", "确认信息", 156 JOptionPane.YES_NO_OPTION); 157 if (flag == JOptionPane.YES_OPTION) { 158 AdminDVDOperatorView.this.dispose(); 159 } 160 } 161 }); 162 163 btn_del.addActionListener(new ActionListener() { 164 165 @Override 166 public void actionPerformed(ActionEvent e) { 167 int row = table.getSelectedRow();// 取得选中的那行 168 int did = (Integer) table.getValueAt(row, 0); 169 int flag = JOptionPane.showInternalConfirmDialog(AdminDVDOperatorView.this, "是否确定删除DVD?", "确认信息", 170 JOptionPane.YES_NO_OPTION); 171 if (flag == JOptionPane.YES_OPTION) { 172 boolean res = dvdBiz.delDVD(did); 173 if (res) { 174 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "删除成功!"); 175 return; 176 } else { 177 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "删除失败,请联系管理员!!"); 178 return; 179 } 180 } 181 } 182 }); 183 184 btn_updata.addActionListener(new ActionListener() { 185 186 @Override 187 public void actionPerformed(ActionEvent e) { 188 String dname = tf_name.getText().trim(); 189 String dcount = tf_count.getText().trim(); 190 int status = cb_status.getSelectedIndex(); 191 if (dname.equals("")) { 192 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "DVD名字不能为空"); 193 return; 194 } else if (dcount.equals("")) { 195 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "借出次数不能为空"); 196 return; 197 } 198 if (!DVDUtil.isNumber(dcount)) { 199 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "DVD借出次数只能是数字"); 200 return; 201 } 202 int flag = JOptionPane.showInternalConfirmDialog(AdminDVDOperatorView.this, "是否确定更新DVD?", "确认信息", 203 JOptionPane.YES_NO_OPTION); 204 205 if (flag == JOptionPane.YES_OPTION) { 206 int row = table.getSelectedRow(); 207 boolean res = dvdBiz 208 .modifyDVD(new DVD((Integer) table.getValueAt(row, 0), dname, new Integer(dcount), status)); 209 System.out.println(res); 210 if (res) { 211 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "更新成功!"); 212 return; 213 } else { 214 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "更新失败,请联系管理员!"); 215 return; 216 } 217 } 218 } 219 }); 220 221 table.addMouseListener(new MouseAdapter() { 222 @Override 223 public void mouseClicked(MouseEvent e) { 224 /// 假设选中一行,更新删除按钮可用 225 if (table.getSelectedRow() != -1) { 226 btn_del.setEnabled(true); 227 btn_updata.setEnabled(true); 228 229 } 230 int row = table.getSelectedRow();// 得到你所选中那行的下标 231 String dname = table.getValueAt(row, 1).toString(); 232 String dcount = table.getValueAt(row, 2).toString(); 233 String status = table.getValueAt(row, 3).toString(); 234 tf_name.setText(dname); 235 tf_count.setText(dcount); 236 cb_status.setSelectedItem(status); 237 } 238 }); 239 240 cb_type.addItemListener(new ItemListener() { 241 242 @Override 243 public void itemStateChanged(ItemEvent e) { 244 String item = e.getItem().toString(); 245 tf_search.setText(""); 246 if (item.equals("所有DVD")) { 247 tf_search.setEditable(false); 248 } else { 249 tf_search.setEditable(true); 250 } 251 } 252 }); 253 254 btn_add.addActionListener(new ActionListener() { 255 256 @Override 257 public void actionPerformed(ActionEvent e) { 258 String dname = tf_name.getText().trim(); 259 String dcount = tf_count.getText().trim(); 260 int status = cb_status.getSelectedIndex();// 0代表已经借出,1代表可借 261 if (dname.equals("")) { 262 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "DVD名字不能为空"); 263 return; 264 } else if (dcount.equals("")) { 265 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "借出次数不能为空"); 266 return; 267 } 268 if (!DVDUtil.isNumber(dcount)) { 269 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "DVD借出次数只能是数字"); 270 return; 271 } 272 int flag = JOptionPane.showInternalConfirmDialog(AdminDVDOperatorView.this, "是否确定添加DVD?", "确认信息", 273 JOptionPane.YES_NO_OPTION); 274 275 if (flag == JOptionPane.YES_OPTION) { 276 boolean res = dvdBiz.addDVD(new DVD(dname, new Integer(dcount), status)); 277 if (res) { 278 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "添加成功!"); 279 return; 280 } else { 281 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "添加失败,请联系管理员!!"); 282 return; 283 } 284 } 285 } 286 }); 287 288 btn_search.addActionListener(new ActionListener() { 289 290 @Override 291 public void actionPerformed(ActionEvent e) { 292 293 int index = cb_type.getSelectedIndex(); 294 String content = tf_search.getText().trim(); 295 if (index != 0 && content.equals("")) { 296 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "查询的内容不能为空"); 297 return; 298 } 299 // 先清除数据 ,防止数据 的累加 300 if (dvdList != null) { 301 dvdList.clear(); 302 } 303 if (index == 0) { 304 dvdList = dvdBiz.queryAllDVDs(); 305 } else if (index == 1) { 306 tf_search.setEnabled(true);// 输入框可用 307 if (DVDUtil.isNumber(content)) { 308 DVD dvd = dvdBiz.queryDVDById(Integer.parseInt(content)); 309 if (dvd != null) { 310 dvdList.add(dvd); 311 } 312 } else { 313 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "输入的编号只能是数字"); 314 return; 315 } 316 } else { 317 dvdList = dvdBiz.queryDVDByName(content); 318 } 319 refreshTable(dvdList); 320 btn_del.setEnabled(false); 321 btn_updata.setEnabled(false); 322 if (dvdList.size() == 0) { 323 JOptionPane.showInternalMessageDialog(AdminDVDOperatorView.this, "没有你要查询的记录!"); 324 return; 325 } 326 } 327 }); 328 } 329 330 private class DVDInfoTableModel implements TableModel { 331 private List<DVD> dvdsList = null; 332 333 private DVDInfoTableModel(List<DVD> dvdList) { 334 this.dvdsList = dvdList; 335 } 336 337 @Override 338 public void addTableModelListener(TableModelListener l) { 339 340 } 341 342 // JTable 列的数据类型 343 public Class<?> getColumnClass(int columnIndex) { 344 345 return String.class; 346 } 347 348 // JTable数据 的列数 349 public int getColumnCount() { 350 351 return 4; 352 } 353 354 // 设置 JTable数据 的列名 355 public String getColumnName(int columnIndex) { 356 if (columnIndex == 0) { 357 return "影碟ID"; 358 } else if (columnIndex == 1) { 359 return "影碟名字"; 360 } else if (columnIndex == 2) { 361 return "影碟借出次数"; 362 } else if (columnIndex == 3) { 363 return "影碟状态"; 364 } else { 365 return "出错"; 366 } 367 } 368 369 // JTable显示 的行数 370 public int getRowCount() { 371 372 return dvdsList.size(); 373 } 374 375 // 获取JTable中指定行指定单元格的数据 376 public Object getValueAt(int rowIndex, int columnIndex) { 377 DVD dvd = dvdsList.get(rowIndex); 378 if (columnIndex == 0) { 379 return dvd.getId(); 380 } else if (columnIndex == 1) { 381 return dvd.getDname(); 382 } else if (columnIndex == 2) { 383 return dvd.getDcount(); 384 } else if (columnIndex == 3) { 385 return "" + (dvd.getStatus() == 1 ? "可借" : "已借"); 386 } else { 387 return "出错"; 388 } 389 } 390 391 // 设置单元格是否可以编辑 392 public boolean isCellEditable(int rowIndex, int columnIndex) { 393 394 return false; 395 } 396 397 @Override 398 public void removeTableModelListener(TableModelListener l) { 399 400 } 401 402 @Override 403 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 404 405 } 406 407 } 408 409 // 刷新Jtable并显示数据 410 private void refreshTable(List<DVD> dvdList) { 411 infoTableModel = new DVDInfoTableModel(dvdList); 412 table.setModel(infoTableModel); 413 } 414 }
AdminMainView.java
1 package com.dvd.view; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.EventQueue; 6 import java.awt.Font; 7 import java.awt.GridLayout; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10 11 import javax.swing.BorderFactory; 12 import javax.swing.ImageIcon; 13 import javax.swing.JButton; 14 import javax.swing.JDesktopPane; 15 import javax.swing.JFrame; 16 import javax.swing.JLabel; 17 import javax.swing.JOptionPane; 18 import javax.swing.JPanel; 19 20 import com.dvd.entiy.User; 21 22 public class AdminMainView extends JFrame { 23 private static final long serialVersionUID = -7828161836358813128L; 24 25 private JPanel panel_main = null; 26 private JPanel panel_wel = null; 27 private JDesktopPane desktopPane = null; 28 private JPanel panel_btn = null; 29 30 private JButton btn_query_rent_dvd = null; 31 private JButton btn_dvd_record = null; 32 private JButton btn_exit = null; 33 34 private JLabel lb_welcome = null; 35 private JLabel desk_img = null; 36 private User user = null; 37 38 public AdminMainView(User user) { 39 this.user = user; 40 init(); 41 registerListener(); 42 43 } 44 45 private void init() { 46 47 panel_main = new JPanel(new BorderLayout()); 48 panel_wel = new JPanel(); 49 desktopPane = new JDesktopPane(); 50 panel_btn = new JPanel(new GridLayout(7, 1, 0, 35)); 51 52 btn_query_rent_dvd = new JButton("管理员DVD查询操作"); 53 btn_dvd_record = new JButton("DVD租赁记录查询"); 54 btn_exit = new JButton("退出窗口"); 55 /* 56 * 快捷功能区 57 */ 58 59 panel_btn.setBorder(BorderFactory.createTitledBorder(BorderFactory.createRaisedBevelBorder(), "快捷功能区")); 60 panel_btn.add(new JLabel()); 61 panel_btn.add(new JLabel()); 62 panel_btn.add(btn_query_rent_dvd); 63 panel_btn.add(btn_dvd_record); 64 panel_btn.add(btn_exit); 65 panel_btn.add(new JLabel()); 66 panel_btn.add(new JLabel()); 67 68 /* 69 * 欢迎区 70 */ 71 lb_welcome = new JLabel("欢 迎 " + user.getUname() + " 使 用 影 碟 租 赁 管 理 系 统"); 72 lb_welcome.setFont(new Font("宋体", Font.BOLD, 23)); 73 lb_welcome.setForeground(Color.BLUE); 74 panel_wel.add(lb_welcome); 75 EventQueue.invokeLater(new Runnable() { 76 77 @Override 78 public void run() { 79 new Thread(new DynaminThread()).start(); 80 81 } 82 }); 83 /* 84 * 主面板 85 */ 86 ImageIcon image = new ImageIcon("src/images/main.png"); 87 desk_img = new JLabel(image); 88 desk_img.setBounds(0, 0, image.getIconWidth(), image.getIconHeight()); 89 desktopPane.add(desk_img, new Integer(Integer.MIN_VALUE)); 90 91 panel_main.add(panel_wel, BorderLayout.NORTH); 92 panel_main.add(panel_btn, BorderLayout.EAST); 93 panel_main.add(desktopPane, BorderLayout.CENTER); 94 95 this.getContentPane().add(panel_main); 96 this.setTitle("管理员操作主面板"); 97 // this.setSize(1000, 650); 98 this.pack(); 99 this.setResizable(false); 100 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 101 this.setLocationRelativeTo(null); 102 this.setVisible(true); 103 } 104 105 private void registerListener() { 106 107 /* 108 * 管理员DVD查询操作 109 */ 110 btn_query_rent_dvd.addActionListener(new ActionListener() { 111 112 @Override 113 public void actionPerformed(ActionEvent e) { 114 AdminDVDOperatorView qdv = new AdminDVDOperatorView(); 115 desktopPane.add(qdv);// 把指定的视图添加到时桌面容器中去 116 qdv.toFront();// 视图显示 的前面 117 118 } 119 }); 120 121 /* 122 * DVD租赁记录查询 123 */ 124 btn_dvd_record.addActionListener(new ActionListener() { 125 126 @Override 127 public void actionPerformed(ActionEvent e) { 128 AdminQueryDVDRecord qdv = new AdminQueryDVDRecord(); 129 desktopPane.add(qdv);// 把指定的视图添加到时桌面容器中去 130 qdv.toFront();// 视图显示 的前面 131 132 } 133 }); 134 135 /* 136 * 退出窗口 137 */ 138 btn_exit.addActionListener(new ActionListener() { 139 140 @Override 141 public void actionPerformed(ActionEvent e) { 142 int flag = JOptionPane.showConfirmDialog(AdminMainView.this, "是否确定退出窗口", "确认信息", 143 JOptionPane.YES_NO_OPTION); 144 if (flag == JOptionPane.YES_OPTION) { 145 System.exit(0);// 关闭窗体,并释放全部资源。 146 // AdminMainView.this.dispose();// 关闭窗体,释放一部分资源。 147 } 148 } 149 }); 150 151 } 152 153 /** 154 * 这是一个线程类,专门用来进行对欢迎Label标签进行移动 155 * 156 * @author LZC 157 * 158 */ 159 private class DynaminThread implements Runnable { 160 161 @Override 162 public void run() { 163 while (true) { 164 for (int i = 780; i > -650; i--) { 165 try { 166 Thread.sleep(10); 167 } catch (InterruptedException e) { 168 e.printStackTrace(); 169 } 170 lb_welcome.setLocation(i, 5); 171 } 172 } 173 174 } 175 } 176 177 }
AdminQueryDVDRecord.java
1 package com.dvd.view; 2 3 import java.awt.BorderLayout; 4 import java.awt.GridLayout; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import javax.swing.BorderFactory; 11 import javax.swing.JButton; 12 import javax.swing.JComboBox; 13 import javax.swing.JInternalFrame; 14 import javax.swing.JLabel; 15 import javax.swing.JOptionPane; 16 import javax.swing.JPanel; 17 import javax.swing.JScrollPane; 18 import javax.swing.JTable; 19 import javax.swing.JTextField; 20 import javax.swing.event.TableModelListener; 21 import javax.swing.table.TableModel; 22 23 import com.dvd.biz.RecordBiz; 24 import com.dvd.biz.Impl.RecordBizImpl; 25 import com.dvd.entiy.Record2; 26 27 public class AdminQueryDVDRecord extends JInternalFrame { 28 29 private static final long serialVersionUID = -3558209667843105955L; 30 private JScrollPane jScrollPanetable = null;// 用来保存Jtable的一个面板 31 private JTable table = null;// 声明JTable 32 33 private JPanel panelbutton = null;// 按钮面板 34 private JTextField tf_insert = null; 35 private JButton btn_search = null; 36 // private JButton btn_rent = null; 37 private JButton btn_exit = null; 38 private JComboBox<String> cb_type = null; 39 private JLabel lb_type = null; 40 41 private List<Record2> record2List = null; 42 private DVDInfoTableModel infoTableModel = null; 43 RecordBiz recordBiz = null; 44 45 public AdminQueryDVDRecord() { 46 47 init(); 48 registerLister(); 49 50 } 51 52 private void init() { 53 this.setTitle("DVD租赁记录查询"); 54 this.setSize(525, 450); 55 this.setIconifiable(true);// 窗体可最小化 56 this.setClosable(true);// 窗体可被关闭 57 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 58 this.setLayout(new BorderLayout()); 59 60 recordBiz = new RecordBizImpl(); 61 62 record2List = new ArrayList<Record2>(); 63 table = new JTable(); 64 jScrollPanetable = new JScrollPane();// 创建面板 65 // 给面板设置边框 66 jScrollPanetable 67 .setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(null, null), "DVD记录查询 ")); 68 jScrollPanetable.setViewportView(table); 69 this.add(jScrollPanetable, BorderLayout.CENTER); 70 71 panelbutton = new JPanel(new GridLayout(9, 1, 10, 10)); 72 // 给面板设置边框 73 panelbutton.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(null, null), "查询条件")); 74 this.add(panelbutton, BorderLayout.EAST); 75 refreshTable(record2List); 76 lb_type = new JLabel("查询类型"); 77 panelbutton.add(lb_type); 78 cb_type = new JComboBox<String>(new String[] { "指定用户租赁记录", "指定DVD租赁记录" }); 79 panelbutton.add(cb_type); 80 81 tf_insert = new JTextField(); 82 panelbutton.add(tf_insert); 83 84 btn_search = new JButton("查询"); 85 panelbutton.add(btn_search); 86 87 panelbutton.add(new JLabel()); 88 panelbutton.add(new JLabel()); 89 panelbutton.add(new JLabel()); 90 panelbutton.add(new JLabel()); 91 92 btn_exit = new JButton("退出窗口"); 93 panelbutton.add(btn_exit); 94 this.getRootPane().setDefaultButton(btn_search);// 默认查询为获得焦点的按钮 95 this.setVisible(true); 96 } 97 98 private void registerLister() { 99 btn_search.addActionListener(new ActionListener() { 100 101 @Override 102 public void actionPerformed(ActionEvent e) { 103 String content = tf_insert.getText().trim(); 104 int index = cb_type.getSelectedIndex(); 105 if (content.equals("")) { 106 JOptionPane.showInternalMessageDialog(AdminQueryDVDRecord.this, "查询内容不能为空"); 107 return; 108 } 109 // record2List = recordBiz.queryHasReturnRecords(content); 110 // 先清除数据 ,防止数据 的累加 111 if (record2List != null) { 112 record2List.clear(); 113 } 114 List<Record2> record22List = new ArrayList<Record2>(); 115 if (index == 0) { 116 record22List = recordBiz.queryUserRecords(content); 117 } else { 118 record22List = recordBiz.queryDVDRecords(content); 119 } 120 refreshTable(record22List); 121 if (record22List.size() == 0) { 122 JOptionPane.showInternalMessageDialog(AdminQueryDVDRecord.this, "没有你要查询的记录!"); 123 return; 124 } 125 126 } 127 }); 128 btn_exit.addActionListener(new ActionListener() { 129 130 @Override 131 public void actionPerformed(ActionEvent arg0) { 132 int flag = JOptionPane.showInternalConfirmDialog(AdminQueryDVDRecord.this, "是否确定退出?", "确认信息", 133 JOptionPane.YES_NO_OPTION); 134 if (flag == JOptionPane.YES_OPTION) { 135 AdminQueryDVDRecord.this.dispose(); 136 } 137 138 } 139 }); 140 141 } 142 143 private class DVDInfoTableModel implements TableModel { 144 private List<Record2> record2List = null; 145 146 private DVDInfoTableModel(List<Record2> record2List) { 147 this.record2List = record2List; 148 } 149 150 @Override 151 public void addTableModelListener(TableModelListener l) { 152 // TODO 自动生成的方法存根 153 154 } 155 156 // JTable 列的数据类型 157 public Class<?> getColumnClass(int columnIndex) { 158 // TODO 自动生成的方法存根 159 return String.class; 160 } 161 162 // JTable数据 的列数 163 public int getColumnCount() { 164 // TODO 自动生成的方法存根 165 return 6; 166 } 167 168 @Override 169 public String getColumnName(int columnIndex) { 170 if (columnIndex == 0) { 171 return "记录ID"; 172 } else if (columnIndex == 1) { 173 return "影碟ID号"; 174 } else if (columnIndex == 2) { 175 return "用户名"; 176 } else if (columnIndex == 3) { 177 return "影碟名字"; 178 } else if (columnIndex == 4) { 179 return "租赁时间"; 180 } else if (columnIndex == 5) { 181 return "归还时间"; 182 } else { 183 return "出错"; 184 } 185 } 186 187 // JTable显示 的行数 188 public int getRowCount() { 189 // TODO 自动生成的方法存根 190 return record2List.size(); 191 } 192 193 // 获取JTable中指定行指定单元格的数据 194 public Object getValueAt(int rowIndex, int columnIndex) { 195 Record2 record = record2List.get(rowIndex); 196 if (columnIndex == 0) { 197 return record.getId(); 198 } else if (columnIndex == 1) { 199 return record.getDid(); 200 } else if (columnIndex == 2) { 201 return record.getUname(); 202 } else if (columnIndex == 3) { 203 return record.getDname(); 204 } else if (columnIndex == 4) { 205 return record.getLendTime(); 206 } else if (columnIndex == 5) { 207 return record.getReturnTime(); 208 } else { 209 return "出错"; 210 } 211 } 212 213 @Override 214 public boolean isCellEditable(int rowIndex, int columnIndex) { 215 // TODO 自动生成的方法存根 216 return false; 217 } 218 219 @Override 220 public void removeTableModelListener(TableModelListener l) { 221 // TODO 自动生成的方法存根 222 223 } 224 225 @Override 226 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 227 // TODO 自动生成的方法存根 228 229 } 230 231 } 232 233 private void refreshTable(List<Record2> record2List) { 234 infoTableModel = new DVDInfoTableModel(record2List); 235 table.setModel(infoTableModel); 236 } 237 238 }
LoginView.java
1 package com.dvd.view; 2 3 import java.awt.GridLayout; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 import javax.swing.ImageIcon; 8 import javax.swing.JButton; 9 import javax.swing.JComboBox; 10 import javax.swing.JFrame; 11 import javax.swing.JLabel; 12 import javax.swing.JOptionPane; 13 import javax.swing.JPanel; 14 import javax.swing.JPasswordField; 15 import javax.swing.JTextField; 16 17 import com.dvd.biz.UserBiz; 18 import com.dvd.biz.Impl.UserBizImpl; 19 import com.dvd.entiy.User; 20 21 public class LoginView extends JFrame { 22 23 private static final long serialVersionUID = 7893951512570101893L; 24 25 private UserBiz userBiz = null; 26 27 private JPanel panel_main = null; 28 private JPanel panel_left = null; 29 private JPanel panel_right = null; 30 31 private JLabel lb_img = null; 32 private JLabel lb_uname = null; 33 private JLabel lb_upass = null; 34 private JLabel lb_type = null; 35 36 private JTextField tf_uname = null; 37 private JPasswordField pf_upass = null; 38 private JComboBox<String> cb_type = null; 39 40 private JButton btn_login = null; 41 private JButton btn_register = null; 42 43 public LoginView() { 44 userBiz = new UserBizImpl(); 45 init(); 46 registerListener(); 47 } 48 49 private void init() { 50 this.setSize(320, 220); 51 this.setResizable(false); 52 this.setLocationRelativeTo(null);// 窗口在中间打开 53 this.setTitle("登录窗口"); 54 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 55 panel_main = new JPanel(new GridLayout(1, 2)); 56 panel_left = new JPanel(); 57 panel_right = new JPanel(new GridLayout(4, 2, 10, 0)); 58 59 lb_img = new JLabel(new ImageIcon(ClassLoader.getSystemResource("images/login.png"))); 60 lb_uname = new JLabel("用 户:", JLabel.CENTER); 61 lb_upass = new JLabel("密 码:", JLabel.CENTER); 62 lb_type = new JLabel("类 型:", JLabel.CENTER); 63 64 tf_uname = new JTextField(8); 65 pf_upass = new JPasswordField(8); 66 cb_type = new JComboBox<String>(new String[] { "普通用户", "管理员" }); 67 68 btn_login = new JButton("登录"); 69 btn_register = new JButton("注册"); 70 this.getRootPane().setDefaultButton(btn_login);// 默认登录为获得焦点的按钮 71 72 panel_left.add(lb_img); 73 panel_right.add(lb_uname); 74 panel_right.add(tf_uname); 75 panel_right.add(lb_upass); 76 panel_right.add(pf_upass); 77 panel_right.add(lb_type); 78 panel_right.add(cb_type); 79 panel_right.add(btn_login); 80 panel_right.add(btn_register); 81 82 panel_main.add(panel_left); 83 panel_main.add(panel_right); 84 85 this.getContentPane().add(panel_main); 86 this.pack(); 87 this.setVisible(true); 88 89 } 90 91 private void registerListener() { 92 btn_login.addActionListener(new ActionListener() { 93 94 @Override 95 public void actionPerformed(ActionEvent arg0) { 96 String uname = tf_uname.getText().trim(); 97 String upass = new String(pf_upass.getPassword()); 98 int type = cb_type.getSelectedIndex() + 1; 99 if (uname.equals("")) { 100 JOptionPane.showMessageDialog(LoginView.this, "用户名不能为空"); 101 return; 102 } else if (upass.equals("")) { 103 JOptionPane.showMessageDialog(LoginView.this, "密码不能为空"); 104 return; 105 } 106 User user = new User(uname, upass, type); 107 user = userBiz.login(user); 108 if (user != null) { 109 if (user.getType() == 1) { 110 new UserMainView(user); 111 } else { 112 new AdminMainView(user); 113 } 114 LoginView.this.dispose();// 打开新的窗口,旧的窗口自动关闭 115 } else { 116 JOptionPane.showMessageDialog(LoginView.this, "用户名或密码出错"); 117 return; 118 } 119 } 120 }); 121 122 btn_register.addActionListener(new ActionListener() { 123 124 @Override 125 public void actionPerformed(ActionEvent arg0) { 126 new UserRegisterView();// 弹出注册视图 127 128 } 129 }); 130 131 } 132 133 }
UserMainView.java
1 package com.dvd.view; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.EventQueue; 6 import java.awt.Font; 7 import java.awt.GridLayout; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10 11 import javax.swing.BorderFactory; 12 import javax.swing.ImageIcon; 13 import javax.swing.JButton; 14 import javax.swing.JDesktopPane; 15 import javax.swing.JFrame; 16 import javax.swing.JLabel; 17 import javax.swing.JOptionPane; 18 import javax.swing.JPanel; 19 20 import com.dvd.entiy.User; 21 22 public class UserMainView extends JFrame { 23 private static final long serialVersionUID = -7828161836358813128L; 24 25 private JPanel panel_main = null; 26 private JPanel panel_wel = null; 27 private JDesktopPane desktopPane = null; 28 private JPanel panel_btn = null; 29 30 private JButton btn_query_rent_dvd = null; 31 private JButton btn_dvd_record = null; 32 private JButton btn_exit = null; 33 34 private JLabel lb_welcome = null; 35 private JLabel desk_img = null; 36 private User user = null; 37 38 public UserMainView(User user) { 39 this.user = user; 40 init(); 41 registerListener(this.user); 42 43 } 44 45 private void init() { 46 47 panel_main = new JPanel(new BorderLayout()); 48 panel_wel = new JPanel(); 49 desktopPane = new JDesktopPane(); 50 panel_btn = new JPanel(new GridLayout(7, 1, 0, 35)); 51 52 btn_query_rent_dvd = new JButton("DVD查询租赁操作"); 53 btn_dvd_record = new JButton("DVD租赁记录查询"); 54 btn_exit = new JButton("退出窗口"); 55 /* 56 * 快捷功能区 57 */ 58 59 panel_btn.setBorder(BorderFactory.createTitledBorder(BorderFactory.createRaisedBevelBorder(), "快捷功能区")); 60 panel_btn.add(new JLabel()); 61 panel_btn.add(new JLabel()); 62 panel_btn.add(btn_query_rent_dvd); 63 panel_btn.add(btn_dvd_record); 64 panel_btn.add(btn_exit); 65 panel_btn.add(new JLabel()); 66 panel_btn.add(new JLabel()); 67 68 /* 69 * 欢迎区 70 */ 71 lb_welcome = new JLabel("欢 迎 " + user.getUname() + " 使 用 影 碟 租 赁 管 理 系 统"); 72 lb_welcome.setFont(new Font("宋体", Font.BOLD, 23)); 73 lb_welcome.setForeground(Color.BLUE); 74 panel_wel.add(lb_welcome); 75 EventQueue.invokeLater(new Runnable() { 76 77 @Override 78 public void run() { 79 new Thread(new DynaminThread()).start(); 80 81 } 82 }); 83 /* 84 * 主面板 85 */ 86 ImageIcon image = new ImageIcon("src/images/main.png"); 87 desk_img = new JLabel(image); 88 desk_img.setBounds(0, 0, image.getIconWidth(), image.getIconHeight()); 89 desktopPane.add(desk_img, new Integer(Integer.MIN_VALUE)); 90 91 panel_main.add(panel_wel, BorderLayout.NORTH); 92 panel_main.add(panel_btn, BorderLayout.EAST); 93 panel_main.add(desktopPane, BorderLayout.CENTER); 94 95 this.getContentPane().add(panel_main); 96 this.setTitle("用户操作主面板"); 97 // this.setSize(1000, 650); 98 this.pack(); 99 this.setResizable(false); 100 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 101 this.setLocationRelativeTo(null); 102 this.setVisible(true); 103 } 104 105 private void registerListener(User user) { 106 btn_query_rent_dvd.addActionListener(new ActionListener() { 107 108 @Override 109 public void actionPerformed(ActionEvent e) { 110 UserQueryDVDRecordView qdv = new UserQueryDVDRecordView(user); 111 112 desktopPane.add(qdv);// 把指定的视图添加到时桌面容器中去 113 qdv.toFront();// 视图显示 的前面 114 115 } 116 }); 117 btn_dvd_record.addActionListener(new ActionListener() { 118 119 @Override 120 public void actionPerformed(ActionEvent e) { 121 UserQueryRentDVDView qdv = new UserQueryRentDVDView(); 122 desktopPane.add(qdv);// 把指定的视图添加到时桌面容器中去 123 qdv.toFront();// 视图显示 的前面 124 125 } 126 }); 127 btn_exit.addActionListener(new ActionListener() { 128 129 @Override 130 public void actionPerformed(ActionEvent e) { 131 int flag = JOptionPane.showConfirmDialog(UserMainView.this, "是否确定退出窗口", "确认信息", 132 JOptionPane.YES_NO_OPTION); 133 if (flag == JOptionPane.YES_OPTION) { 134 System.exit(0);// 关闭窗体,并释放全部资源。 135 // AdminMainView.this.dispose();// 关闭窗体,释放一部分资源。 136 } 137 } 138 }); 139 } 140 141 /** 142 * 这是一个线程类,专门用来进行对欢迎Label标签进行移动 143 * 144 * @author LZC 145 * 146 */ 147 private class DynaminThread implements Runnable { 148 149 @Override 150 public void run() { 151 while (true) { 152 for (int i = 780; i > -650; i--) { 153 try { 154 Thread.sleep(10); 155 } catch (InterruptedException e) { 156 e.printStackTrace(); 157 } 158 lb_welcome.setLocation(i, 5); 159 } 160 } 161 162 } 163 } 164 165 }
UserQueryDVDRecordView.java
1 package com.dvd.view; 2 3 import java.awt.BorderLayout; 4 import java.awt.GridLayout; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.awt.event.MouseAdapter; 8 import java.awt.event.MouseEvent; 9 import java.util.ArrayList; 10 import java.util.List; 11 12 import javax.swing.BorderFactory; 13 import javax.swing.JButton; 14 import javax.swing.JComboBox; 15 import javax.swing.JInternalFrame; 16 import javax.swing.JLabel; 17 import javax.swing.JOptionPane; 18 import javax.swing.JPanel; 19 import javax.swing.JScrollPane; 20 import javax.swing.JTable; 21 import javax.swing.event.TableModelListener; 22 import javax.swing.table.TableModel; 23 24 import com.dvd.biz.DVDBiz; 25 import com.dvd.biz.Impl.DVDBizImpl; 26 import com.dvd.entiy.DVD; 27 import com.dvd.entiy.User; 28 29 public class UserQueryDVDRecordView extends JInternalFrame { 30 31 private static final long serialVersionUID = -3558209667843105955L; 32 private JScrollPane jScrollPane = null;// 用来保存Jtable的一个面板 33 private JTable table = null;// 声明JTable 34 35 private JPanel panelbutton = null;// 按钮面板 36 private JButton btn_search = null; 37 private JButton btn_rent = null; 38 private JButton btn_exit = null; 39 private JComboBox<String> cb_type = null; 40 private JLabel lb_type = null; 41 42 private DVDInfoTableModel infoTableModel = null; 43 DVDBiz dvdBiz = null; 44 List<DVD> dvdList = null; 45 User user = null; 46 47 public UserQueryDVDRecordView(User user) { 48 this.user = user; 49 dvdBiz = new DVDBizImpl(); 50 init(); 51 registerListener(this.user); 52 } 53 54 private void init() { 55 this.setTitle("DVD信息查询"); 56 this.setSize(500, 450); 57 this.setIconifiable(true);// 窗体可最小化 58 this.setClosable(true);// 窗体可关闭 59 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 60 this.setLayout(new BorderLayout()); 61 62 dvdList = new ArrayList<DVD>(); 63 64 table = new JTable(); 65 66 jScrollPane = new JScrollPane();// 创建面板 67 // 给面板设置边框 68 jScrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(null, null), "查询信息")); 69 jScrollPane.setViewportView(table); 70 ; 71 this.add(jScrollPane, BorderLayout.CENTER); 72 73 panelbutton = new JPanel(new GridLayout(7, 1, 10, 30)); 74 // 给面板设置边框 75 panelbutton.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(null, null), "查询条件")); 76 this.add(panelbutton, BorderLayout.EAST); 77 78 refreshTable(dvdList); 79 80 lb_type = new JLabel("查询类型"); 81 panelbutton.add(lb_type); 82 cb_type = new JComboBox<String>(new String[] { "全部DVD", "可借DVD" }); 83 panelbutton.add(cb_type); 84 btn_search = new JButton("查询"); 85 panelbutton.add(btn_search); 86 87 btn_rent = new JButton("租DVD"); 88 panelbutton.add(btn_rent); 89 btn_rent.setEnabled(false);// 默认不可用 90 91 panelbutton.add(new JLabel()); 92 93 panelbutton.add(new JLabel()); 94 95 btn_exit = new JButton("退出窗口"); 96 panelbutton.add(btn_exit); 97 98 this.setVisible(true); 99 } 100 101 private void registerListener(User user) { 102 btn_rent.addActionListener(new ActionListener() { 103 104 @Override 105 public void actionPerformed(ActionEvent arg0) { 106 int row = table.getSelectedRow();// 得到你所选中那行的下标 107 int did = (int) table.getValueAt(row, 0); 108 // System.out.println("id编号" + did); 109 int flag = dvdBiz.lendDVD(did, user.getId()); 110 // System.out.println(user.getId()); 111 // System.out.println("flag=" + flag); 112 if (flag == 1) { 113 JOptionPane.showInternalMessageDialog(UserQueryDVDRecordView.this, "不可借,已经借出"); 114 return; 115 } else if (flag == 2) { 116 JOptionPane.showInternalMessageDialog(UserQueryDVDRecordView.this, "成功租到DVD"); 117 return; 118 } else if (flag == 3) { 119 JOptionPane.showInternalMessageDialog(UserQueryDVDRecordView.this, "租DVD失败"); 120 return; 121 } 122 } 123 }); 124 125 table.addMouseListener(new MouseAdapter() { 126 @Override 127 public void mouseClicked(MouseEvent e) { 128 /// 假设选中一行,租DVD按钮可用 129 btn_rent.setEnabled(true); 130 int row = table.getSelectedRow();// 得到你所选中那行的下标 131 132 } 133 }); 134 /* 135 * cb_type.addItemListener(new ItemListener() { 136 * 137 * @Override public void itemStateChanged(ItemEvent e) { String item = 138 * e.getItem().toString(); if (item.equals("可借DVD")) { 139 * btn_rent.setEnabled(true); } 140 * 141 * } }); 142 */ 143 btn_search.addActionListener(new ActionListener() { 144 145 @Override 146 public void actionPerformed(ActionEvent e) { 147 int index = cb_type.getSelectedIndex(); 148 List<DVD> dvdList = new ArrayList<DVD>(); 149 if (index == 0) { 150 dvdList = dvdBiz.queryAllDVDs(); 151 } else { 152 dvdList = dvdBiz.canLendDVD(); 153 } 154 refreshTable(dvdList); 155 if (dvdList.size() == 0) { 156 JOptionPane.showInternalMessageDialog(UserQueryDVDRecordView.this, "没有你要查询的记录!"); 157 return; 158 } 159 160 } 161 }); 162 163 btn_exit.addActionListener(new ActionListener() { 164 165 @Override 166 public void actionPerformed(ActionEvent e) { 167 int flag = JOptionPane.showInternalConfirmDialog(UserQueryDVDRecordView.this, "是否确定退出?", "确认信息", 168 JOptionPane.YES_NO_OPTION); 169 if (flag == JOptionPane.YES_OPTION) { 170 UserQueryDVDRecordView.this.dispose(); 171 } 172 } 173 }); 174 175 } 176 177 private class DVDInfoTableModel implements TableModel { 178 private List<DVD> dvdsList = null; 179 180 private DVDInfoTableModel(List<DVD> dvdList) { 181 this.dvdsList = dvdList; 182 } 183 184 @Override 185 public void addTableModelListener(TableModelListener l) { 186 // TODO 自动生成的方法存根 187 188 } 189 190 @Override 191 public Class<?> getColumnClass(int columnIndex) { 192 // TODO 自动生成的方法存根 193 return String.class; 194 } 195 196 @Override 197 public int getColumnCount() { 198 // TODO 自动生成的方法存根 199 return 4; 200 } 201 202 @Override 203 public String getColumnName(int columnIndex) { 204 if (columnIndex == 0) { 205 return "影碟ID"; 206 } else if (columnIndex == 1) { 207 return "影碟名字"; 208 } else if (columnIndex == 2) { 209 return "影碟借出次数"; 210 } else if (columnIndex == 3) { 211 return "影碟状态"; 212 } else { 213 return "出错"; 214 } 215 } 216 217 @Override 218 public int getRowCount() { 219 // TODO 自动生成的方法存根 220 return dvdsList.size(); 221 } 222 223 @Override 224 public Object getValueAt(int rowIndex, int columnIndex) { 225 DVD dvd = dvdsList.get(rowIndex); 226 if (columnIndex == 0) { 227 return dvd.getId(); 228 } else if (columnIndex == 1) { 229 return dvd.getDname(); 230 } else if (columnIndex == 2) { 231 return dvd.getDcount(); 232 } else if (columnIndex == 3) { 233 return "" + (dvd.getStatus() == 1 ? "可借" : "已借"); 234 } else { 235 return "出错"; 236 } 237 } 238 239 @Override 240 public boolean isCellEditable(int rowIndex, int columnIndex) { 241 // TODO 自动生成的方法存根 242 return false; 243 } 244 245 @Override 246 public void removeTableModelListener(TableModelListener l) { 247 // TODO 自动生成的方法存根 248 249 } 250 251 @Override 252 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 253 // TODO 自动生成的方法存根 254 255 } 256 257 } 258 259 // 刷新Jtable并显示数据 260 private void refreshTable(List<DVD> dvdList) { 261 infoTableModel = new DVDInfoTableModel(dvdList); 262 table.setModel(infoTableModel); 263 } 264 }
UserQueryRentDVDView.java
1 package com.dvd.view; 2 3 import java.awt.BorderLayout; 4 import java.awt.GridLayout; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.awt.event.MouseAdapter; 8 import java.awt.event.MouseEvent; 9 import java.util.ArrayList; 10 import java.util.List; 11 12 import javax.swing.BorderFactory; 13 import javax.swing.JButton; 14 import javax.swing.JComboBox; 15 import javax.swing.JInternalFrame; 16 import javax.swing.JLabel; 17 import javax.swing.JOptionPane; 18 import javax.swing.JPanel; 19 import javax.swing.JScrollPane; 20 import javax.swing.JTable; 21 import javax.swing.event.TableModelListener; 22 import javax.swing.table.TableModel; 23 24 import com.dvd.biz.DVDBiz; 25 import com.dvd.biz.Impl.DVDBizImpl; 26 import com.dvd.entiy.DVD; 27 28 public class UserQueryRentDVDView extends JInternalFrame { 29 30 private static final long serialVersionUID = -3558209667843105955L; 31 private JScrollPane jScrollPane = null;// 用来保存Jtable的一个面板 32 private JTable table = null;// 声明JTable 33 34 private JPanel panelbutton = null;// 按钮面板 35 private JButton btn_search = null; 36 private JButton btn_return = null; 37 private JButton btn_exit = null; 38 private JComboBox<String> cb_type = null; 39 private JLabel lb_type = null; 40 private DVDInfoTableModel infoTableModel = null; 41 DVDBiz dvdBiz = null; 42 43 public UserQueryRentDVDView() { 44 dvdBiz = new DVDBizImpl(); 45 init(); 46 registerListener(); 47 } 48 49 private void init() { 50 this.setTitle("DVD租赁记录查询"); 51 this.setSize(500, 450); 52 this.setIconifiable(true);// 窗体可最小化 53 this.setClosable(true);// 窗体可被关闭 54 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 55 this.setLayout(new BorderLayout()); 56 57 table = new JTable(); 58 jScrollPane = new JScrollPane();// 创建面板 59 // 给面板设置边框 60 jScrollPane 61 .setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(null, null), "本人租赁记录查询 ")); 62 jScrollPane.setViewportView(table); 63 this.add(jScrollPane, BorderLayout.CENTER); 64 65 panelbutton = new JPanel(new GridLayout(7, 1, 10, 30)); 66 // 给面板设置边框 67 panelbutton.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(null, null), "查询条件")); 68 this.add(panelbutton, BorderLayout.EAST); 69 lb_type = new JLabel("查询类型"); 70 panelbutton.add(lb_type); 71 cb_type = new JComboBox<String>(new String[] { "全部DVD", "未还DVD" }); 72 panelbutton.add(cb_type); 73 74 btn_search = new JButton("查询"); 75 panelbutton.add(btn_search); 76 77 btn_return = new JButton("还DVD"); 78 panelbutton.add(btn_return); 79 btn_return.setEnabled(false);// 默认不可用 80 81 panelbutton.add(new JLabel()); 82 83 panelbutton.add(new JLabel()); 84 85 btn_exit = new JButton("退出窗口"); 86 panelbutton.add(btn_exit); 87 88 this.setVisible(true); 89 } 90 91 private void registerListener() { 92 btn_return.addActionListener(new ActionListener() { 93 94 @Override 95 public void actionPerformed(ActionEvent e) { 96 int row = table.getSelectedRow();// 得到你所选中那行的下标 97 int rid = (int) table.getValueAt(row, 0); 98 // System.out.println(rid); 99 int flag = dvdBiz.returnDVD(rid); 100 System.out.println(flag); 101 if (flag == 1) { 102 JOptionPane.showInternalMessageDialog(UserQueryRentDVDView.this, "DVD已经归还"); 103 return; 104 } else if (flag == 2) { 105 JOptionPane.showInternalMessageDialog(UserQueryRentDVDView.this, "DVD已经归还"); 106 return; 107 } else if (flag == 3) { 108 JOptionPane.showInternalMessageDialog(UserQueryRentDVDView.this, "归还成功"); 109 return; 110 } else if (flag == 4) { 111 JOptionPane.showInternalMessageDialog(UserQueryRentDVDView.this, "归还失败"); 112 return; 113 } 114 } 115 }); 116 table.addMouseListener(new MouseAdapter() { 117 @Override 118 public void mouseClicked(MouseEvent e) { 119 /// 假设选中一行,还DVD按钮可用 120 btn_return.setEnabled(true); 121 122 } 123 }); 124 125 btn_search.addActionListener(new ActionListener() { 126 127 @Override 128 public void actionPerformed(ActionEvent e) { 129 int index = cb_type.getSelectedIndex(); 130 List<DVD> dvdList = new ArrayList<DVD>(); 131 if (index == 0) { 132 dvdList = dvdBiz.queryAllDVDs(); 133 } else { 134 dvdList = dvdBiz.hasLendedDvD(); 135 } 136 refreshTable(dvdList); 137 if (dvdList.size() == 0) { 138 JOptionPane.showInternalMessageDialog(UserQueryRentDVDView.this, "没有你要查询的记录!"); 139 return; 140 } 141 142 } 143 }); 144 btn_exit.addActionListener(new ActionListener() { 145 146 @Override 147 public void actionPerformed(ActionEvent e) { 148 int flag = JOptionPane.showInternalConfirmDialog(UserQueryRentDVDView.this, "是否确定退出?", "确认信息", 149 JOptionPane.YES_NO_OPTION); 150 if (flag == JOptionPane.YES_OPTION) { 151 UserQueryRentDVDView.this.dispose(); 152 } 153 } 154 }); 155 } 156 157 private class DVDInfoTableModel implements TableModel { 158 private List<DVD> dvdsList = null; 159 160 private DVDInfoTableModel(List<DVD> dvdList) { 161 this.dvdsList = dvdList; 162 } 163 164 @Override 165 public void addTableModelListener(TableModelListener l) { 166 // TODO 自动生成的方法存根 167 168 } 169 170 @Override 171 public Class<?> getColumnClass(int columnIndex) { 172 // TODO 自动生成的方法存根 173 return String.class; 174 } 175 176 @Override 177 public int getColumnCount() { 178 // TODO 自动生成的方法存根 179 return 4; 180 } 181 182 @Override 183 public String getColumnName(int columnIndex) { 184 if (columnIndex == 0) { 185 return "影碟ID"; 186 } else if (columnIndex == 1) { 187 return "影碟名字"; 188 } else if (columnIndex == 2) { 189 return "影碟借出次数"; 190 } else if (columnIndex == 3) { 191 return "影碟状态"; 192 } else { 193 return "出错"; 194 } 195 } 196 197 @Override 198 public int getRowCount() { 199 // TODO 自动生成的方法存根 200 return dvdsList.size(); 201 } 202 203 @Override 204 public Object getValueAt(int rowIndex, int columnIndex) { 205 DVD dvd = dvdsList.get(rowIndex); 206 if (columnIndex == 0) { 207 return dvd.getId(); 208 } else if (columnIndex == 1) { 209 return dvd.getDname(); 210 } else if (columnIndex == 2) { 211 return dvd.getDcount(); 212 } else if (columnIndex == 3) { 213 return "" + (dvd.getStatus() == 1 ? "可借" : "已借"); 214 } else { 215 return "出错"; 216 } 217 } 218 219 @Override 220 public boolean isCellEditable(int rowIndex, int columnIndex) { 221 // TODO 自动生成的方法存根 222 return false; 223 } 224 225 @Override 226 public void removeTableModelListener(TableModelListener l) { 227 // TODO 自动生成的方法存根 228 229 } 230 231 @Override 232 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 233 // TODO 自动生成的方法存根 234 235 } 236 237 } 238 239 // 刷新Jtable并显示数据 240 private void refreshTable(List<DVD> dvdList) { 241 infoTableModel = new DVDInfoTableModel(dvdList); 242 table.setModel(infoTableModel); 243 } 244 245 } 246 // UserQueryRentDVDView
UserRegisterView.java
1 package com.dvd.view; 2 3 import java.awt.Font; 4 import java.awt.GridLayout; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 8 import javax.swing.JButton; 9 import javax.swing.JFrame; 10 import javax.swing.JLabel; 11 import javax.swing.JOptionPane; 12 import javax.swing.JPanel; 13 import javax.swing.JPasswordField; 14 import javax.swing.JTextField; 15 16 import com.dvd.biz.UserBiz; 17 import com.dvd.biz.Impl.UserBizImpl; 18 import com.dvd.dao.UserDao; 19 import com.dvd.dao.impl.UserDaoImpl; 20 import com.dvd.entiy.User; 21 22 public class UserRegisterView extends JFrame { 23 private static final long serialVersionUID = 7893951512570101893L; 24 private JPanel panel_main = null; 25 private JPanel panel01 = null; 26 private JPanel panel02 = null; 27 private JPanel panel03 = null; 28 private JPanel panel04 = null; 29 private JPanel panel05 = null; 30 31 private JLabel lb_name = null; 32 private JLabel lb_init_upass = null; 33 private JLabel lb_confirm_upass = null; 34 35 private JTextField tf_name = null; 36 private JPasswordField pf_init_upass = null; 37 private JPasswordField pf_confirm_upass = null; 38 39 private JButton btn_confirm = null; 40 private JButton btn_back = null; 41 42 UserDao userDao = null; 43 UserBiz userBiz = null; 44 45 public UserRegisterView() { 46 userDao = new UserDaoImpl(); 47 userBiz = new UserBizImpl(); 48 init(); 49 registerListener(); 50 } 51 52 private void init() { 53 this.setSize(450, 260); 54 this.setResizable(false); 55 this.setLocationRelativeTo(null);// 窗口在中间打开 56 this.setTitle("用户注册窗口"); 57 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 58 panel_main = new JPanel(new GridLayout(5, 1)); 59 JPanel panel01 = new JPanel(); 60 JPanel panel02 = new JPanel(); 61 JPanel panel03 = new JPanel(); 62 JPanel panel04 = new JPanel(); 63 JPanel panel05 = new JPanel(); 64 65 lb_name = new JLabel("用户名"); 66 lb_name.setFont(new Font("宋体", Font.BOLD, 15)); 67 lb_init_upass = new JLabel("初始化密码"); 68 lb_init_upass.setFont(new Font("宋体", Font.BOLD, 15)); 69 lb_confirm_upass = new JLabel("确认密码"); 70 lb_confirm_upass.setFont(new Font("宋体", Font.BOLD, 15)); 71 72 tf_name = new JTextField(15); 73 pf_init_upass = new JPasswordField(15); 74 pf_confirm_upass = new JPasswordField(15); 75 76 btn_confirm = new JButton("确认提交"); 77 btn_back = new JButton("退出"); 78 79 panel02.add(lb_name); 80 panel02.add(tf_name); 81 panel03.add(lb_init_upass); 82 panel03.add(pf_init_upass); 83 panel04.add(lb_confirm_upass); 84 panel04.add(pf_confirm_upass); 85 panel05.add(btn_confirm); 86 panel05.add(btn_back); 87 88 panel_main.add(panel01); 89 panel_main.add(panel02); 90 panel_main.add(panel03); 91 panel_main.add(panel04); 92 panel_main.add(panel05); 93 94 this.getContentPane().add(panel_main); 95 96 this.getRootPane().setDefaultButton(btn_confirm);// 默认获得焦点的按钮 97 this.setVisible(true); 98 99 } 100 101 private void registerListener() { 102 // 退出按钮 103 btn_back.addActionListener(new ActionListener() { 104 105 @Override 106 public void actionPerformed(ActionEvent e) { 107 // int flag = 108 // JOptionPane.showInternalConfirmDialog(UserRegisterView.this, 109 // "是否退出注册?", "退出信息", 110 // JOptionPane.YES_NO_OPTION); 111 int flag = JOptionPane.showConfirmDialog(UserRegisterView.this, "是否退出注册?", "确认信息", 112 JOptionPane.YES_NO_OPTION); 113 if (flag == JOptionPane.YES_OPTION) { 114 UserRegisterView.this.dispose();// 窗口自动关闭 115 } 116 } 117 }); 118 // 注册按钮 119 btn_confirm.addActionListener(new ActionListener() { 120 121 @Override 122 public void actionPerformed(ActionEvent e) { 123 String uname = tf_name.getText(); 124 String init_upass = new String(pf_init_upass.getPassword()); 125 String upass = new String(pf_confirm_upass.getPassword()); 126 if (uname.equals("")) { 127 JOptionPane.showMessageDialog(UserRegisterView.this, "用户名不能为空"); 128 return; 129 } else if (init_upass.equals("")) { 130 JOptionPane.showMessageDialog(UserRegisterView.this, "初始化密码不能为空"); 131 return; 132 } else if (upass.equals("")) { 133 JOptionPane.showMessageDialog(UserRegisterView.this, "确认密码不能为空"); 134 return; 135 } 136 User user = new User(uname, upass, 1); 137 138 int flag = userBiz.registerUser(user); 139 if (flag == 1) { 140 JOptionPane.showMessageDialog(UserRegisterView.this, "用户名已存在"); 141 return; 142 } else if (flag == 2) { 143 JOptionPane.showMessageDialog(UserRegisterView.this, "注册成功"); 144 return; 145 } else if (flag == 3) { 146 JOptionPane.showMessageDialog(UserRegisterView.this, "注册失败"); 147 return; 148 } 149 } 150 }); 151 152 } 153 154 }
TestLogin.java
1 package com.dvd.text; 2 3 import com.dvd.view.LoginView; 4 5 public class TestLogin { 6 public static void main(String[] args) { 7 new LoginView(); 8 } 9 }
看了那么多代码,来看看运行怎么样
最后就是红色这里还没完善,有待更新