工程目录:
Java包目录
1.com.qdu.entity
用来保存该工程中的所有实体类,包括以下几个
- Books:书籍实体类
- Manager:图书馆管理员类
- Record:借书记录表的实体类
- User:图书馆用户类
Books.java
package com.qdu.entity;
public class Books {
private String bookName;//不可以更改
private String bookClass;//不可以更改
private String position;
private String writer;//不可以更改
private int quantity;
public Books(String bookName, String bookClass, String position, String writer, Integer quantity) {
super();
this.bookName = bookName;
this.bookClass = bookClass;
this.position = position;
this.writer = writer;
this.quantity = quantity;
}
public Books() {
super();
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookClass() {
return bookClass;
}
public void setBookClass(String bookClass) {
this.bookClass = bookClass;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
}
Manager.java
package com.qdu.entity;
public class Manager {
private String mName;
private String mId;
private String mPass;
public Manager(String mName, String mId, String mPass) {
super();
this.mName = mName;
this.mId = mId;
this.mPass = mPass;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public String getmId() {
return mId;
}
public void setmId(String mId) {
this.mId = mId;
}
public String getmPass() {
return mPass;
}
public void setmPass(String mPass) {
this.mPass = mPass;
}
}
Record.java
package com.qdu.entity;
public class Record {
private String bookName;
private String userName;//谁借的(还的)
private String userId;
private String type;//借阅类型:借书是(初始时)false 如果已经还了那就是true
private String date;//借(或还)的日期
public Record() {
}
public Record(String bookName, String userName, String userId, String type, String date) {
super();
this.bookName = bookName;
this.userName = userName;
this.userId = userId;
this.type = type;
this.date = date;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
User.java
package com.qdu.entity;
public class User {
private String userName;
private String uId;
private String userPass;
private String sex;
private String phoneNum;
private String eMail;
public User() {
super();
}
public User(String userName, String uId, String userPass, String sex, String phoneNum, String eMail) {
super();
this.userName = userName;
this.uId = uId;
this.userPass = userPass;
this.sex = sex;
this.phoneNum = phoneNum;
this.eMail = eMail;
}
public User(String uId, String userPass) {
super();
this.uId = uId;
this.userPass = userPass;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getuId() {
return uId;
}
public void setuId(String uId) {
this.uId = uId;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String geteMail() {
return eMail;
}
public void seteMail(String eMail) {
this.eMail = eMail;
}
}
2.com.qdu.dao.impl
Dao: Data Access Object-数据访问对象。为了封装对数据库表的操作,我们会专门构建Dao组件(Dao接口和Dao实现类)。用于封装对指定数据表的操作,封装在Dao里。实际的业务很复杂,通常会先定义Dao接口,规定对一个表的操作有哪些。比如,StudentDao封装对student表的所有数据库操作;ProductDao封装对product表的所有数据库操作。如果有接口和对应的实现类,实现类我们会放在dao.impl包里。这里为了简便我们把dao和daoimpl合并为了一个daoimpl包。
在Mysql数据库中,我们有四个表:Books表,Manager表,Record表,User表,因此这里有四个Daoimpl类。
其中BaseDaoimpl类用来实现这四个表共有的操作(insert、delete、update)
BaseDaoimpl.java
package com.qdu.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Objects;
import com.qdu.util.DatabaseUtil;
public class BaseDaoimpl{
//仅用于执行insert delete update
public int executeUpdate(String sql,Object... params)
{
Connection con = null;
PreparedStatement ps = null;
int rows = 0; //受影响行数
con = DatabaseUtil.getConnection();//getConnection()获取一个数据库连接
try {
ps = con.prepareStatement(sql);
for(int i=0;i<params.length;i++) {
ps.setObject(i+1, params[i]);//从1开始填参数
}
rows = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(null, ps, con);
}
return rows;
}
}
BooksDaoimpl.java
package com.qdu.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.qdu.entity.Books;
import com.qdu.util.DatabaseUtil;
public class BooksDaoImpl extends BaseDaoimpl{
//插入一本新书
//书名 作者 书类 数量 位置
public int insert(Books book)
{
return executeUpdate("insert into books values(?,?,?,?,?)",
book.getBookName(),book.getWriter(),book.getBookClass(),
book.getQuantity(),book.getPosition());
}
//通过书名删掉一本书
public int deleteByname(String name)
{
String sqlString = "delete from books where Bname=?";
return executeUpdate(sqlString, name);
}
//修改图书数量,根据书名
public int updateBooknum(String name,int newNum)
{
String sqlString = "update books set Bquantity=? where Bname=?";
return executeUpdate(sqlString, newNum,name);
}
//修改位置,就这俩可以改,根据书名
public int updateBookpos(String name,String newPos)
{
String sqlString = "update books set Bposition=? where Bname=?";
return executeUpdate(sqlString, newPos,name);
}
//下面是不用executeUpdate的
//根据书名获取一本书的信息
//表格:书名 作者 书类 数量 位置
//构造函数:(bookName, bookClass, position, writer, quantity)
public Books getAbookByname(String nme) {
Books bk = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
con = DatabaseUtil.getConnection();
String sql = "select * from books where Bname = ?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, nme);
rs = ps.executeQuery();
//这里的相关参数容易出错
while (rs.next()) {
bk = new Books(rs.getString(1), rs.getString(3), rs.getString(5), rs.getString(2), rs.getInt(4));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(rs, ps, con);
}
return bk;
}
//根据姓名关键字或者类别关键字返回书籍列表
public List<Books> getBooksBynamekey(String nme){
List<Books> booklist = new ArrayList<>();
Connection con = DatabaseUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from books where Bname like ?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, "%"+nme+"%");
rs=ps.executeQuery();
while (rs.next()) {
//这个地方也有隐患
Books bk = new Books(rs.getString(1), rs.getString(3), rs.getString(5), rs.getString(2), rs.getInt(4));
booklist.add(bk);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(rs, ps, con);
}
return booklist;
}
public List<Books> getBooksByclasskey(String cls){
List<Books> booklist = new ArrayList<>();
Connection con = DatabaseUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from books where Bclass like ?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, "%"+cls+"%");
rs=ps.executeQuery();
while (rs.next()) {
//这个地方也有隐患
Books bk = new Books(rs.getString(1), rs.getString(3), rs.getString(5), rs.getString(2), rs.getInt(4));
booklist.add(bk);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(rs, ps, con);
}
return booklist;
}
//
public List<Books> getBooksBypos(String pos){
List<Books> booklist = new ArrayList<>();
Connection con = DatabaseUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from books where Bposition=?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, pos);
rs=ps.executeQuery();
while (rs.next()) {
//这个地方也有隐患
Books bk = new Books(rs.getString(1), rs.getString(3), rs.getString(5), rs.getString(2), rs.getInt(4));
booklist.add(bk);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(rs, ps, con);
}
return booklist;
}
//根据作者姓名关键字返回书籍列表
public List<Books> getByWriternamekey(String nme){
List<Books> booklist = new ArrayList<>();
Connection con = DatabaseUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from books where Bwriter like ?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, "%"+nme+"%");
rs=ps.executeQuery();
while (rs.next()) {
//这个地方也有隐患
Books bk = new Books(rs.getString(1), rs.getString(3), rs.getString(5), rs.getString(2), rs.getInt(4));
booklist.add(bk);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(rs, ps, con);
}
return booklist;
}
}
ManagerDaoimpl.java
package com.qdu.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.qdu.entity.Books;
import com.qdu.entity.Manager;
import com.qdu.util.DatabaseUtil;
public class ManagerDaoimpl extends BaseDaoimpl{
//构函:public Manager(String mName, String mId, String mPass)
//表Managers:Mname Mid Mpass
//添加一个新的管理员
public int addNew(String nme,String id,String pass) {
return executeUpdate("insert into Managers values (?,?,?)",nme,id,pass);
}
//获取一个管理员
public Manager getOneByid(String id)
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Manager m = null;
con = DatabaseUtil.getConnection();
String sql = "select * from Managers where Mid=?";
try {
ps = con.prepareStatement(sql);
ps.setString(1, id);
//执行查询,获得结果集
rs = ps.executeQuery();
while(rs.next()) {
//读取各列数据,传入Product的构造函数,设置为Product对象各个属性的值
//将查询的一行记录封装成单个的产品对象
m=new Manager(rs.getString("Mname"),rs.getString("Mid"),rs.getString("Mpass"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(rs, ps, con);
}
return m;
}
//更新一个管理员的密码
public int updatePass(String pass,String id)
{
String sql = "update Managers set Mpass = ? where Mid = ?";
return executeUpdate(sql, pass,id);
}
//获取所有管理员
public List<Manager> getAllmanagers(){
List<Manager> mlist = new ArrayList<>();
Connection con = DatabaseUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from managers";
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while (rs.next()) {
Manager m = new Manager(rs.getString(1),rs.getString(2),rs.getString(3));
mlist.add(m);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(rs, ps, con);
}
return mlist;
}
//删除一个管理员:
public int deleteManager(String id,String pass)
{
String sql = "delete from managers where Mid = ?";
return executeUpdate(sql, id);
}
}
RecordDaoimpl.java
package com.qdu.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.qdu.util.DatabaseUtil;
import com.qdu.entity.Books;
import com.qdu.entity.Record;
public class RecordDaoimpl extends BaseDaoimpl{
//构造函数:bookName, userName, userId,type(boolean), date
//表(Record):bookName, userName, userId,type(boolean), date
//新建一条记录
public int insertRecord(String bookName, String userName, String userId, String type, String date)
{
String sql = "insert into Record values(?,?,?,?,?)";
return executeUpdate(sql,bookName,userName,userId,type,date);
}
//删除一条记录
public int deleteRecord(String bookName,String userId,String type)
{
String sql = "delete from Record where bookName=? AND userId=? AND type=?";
return executeUpdate(sql, bookName,userId,type);
}
//根据用户id获取记录
public List<Record> getRecordByUserId(String id) {
List<Record> recordList = new ArrayList<>();
Connection con=null;
ResultSet res=null;
PreparedStatement ps=null;
con = DatabaseUtil.getConnection();
String sql = "select * from Record where userId = ?";
con=DatabaseUtil.getConnection();
try {
ps=con.prepareStatement(sql);
ps.setString(1, id);
res=ps.executeQuery();
while (res.next()) {
Record r = new Record(res.getString(1),res.getString(2),res.getString(3),res.getString(4),res.getString(5));
recordList.add(r);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DatabaseUtil.close(res, ps, con);
}
return recordList;
}
//根据用户名字获取一堆记录
public List<Record> getRecordByUserName(String name) {
List<Record> recordList = new ArrayList<>();
Connection con=null;
ResultSet res=null;
PreparedStatement ps=null;
con = DatabaseUtil.getConnection();
String sql = "select * from Record where userName = ?";
con=DatabaseUtil.getConnection();
try {
ps=con.prepareStatement(sql);
ps.setString(1, name);
res=ps.executeQuery();
while (res.next()) {
Record r = new Record(res.getString(1),res.getString(2),res.getString(3),res.getString(4),res.getString(5));
recordList.add(r);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DatabaseUtil.close(res, ps, con);
}
return recordList;
}
//根据书名获取一堆记录
public List<Record> getByBookName(String name)
{
List<Record> recordList = new ArrayList<>();
Connection con=null;
ResultSet res=null;
PreparedStatement ps=null;
con = DatabaseUtil.getConnection();
String sql = "select * from Record where bookName = ?";
con=DatabaseUtil.getConnection();
try {
ps=con.prepareStatement(sql);
ps.setString(1, name);
res=ps.executeQuery();
while (res.next()) {
//这个地方也有隐患
Record r = new Record(res.getString(1),res.getString(2),res.getString(3),res.getString(4),res.getString(5));
recordList.add(r);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DatabaseUtil.close(res, ps, con);
}
return recordList;
}
//根据借阅日期查询一堆记录
public List<Record> getBydate(String date)
{
List<Record> recordList = new ArrayList<>();
Connection con=null;
ResultSet res=null;
PreparedStatement ps=null;
con = DatabaseUtil.getConnection();
String sql = "select * from Record where date = ?";
con=DatabaseUtil.getConnection();
try {
ps=con.prepareStatement(sql);
ps.setString(1, date);
res=ps.executeQuery();
while (res.next()) {
//这个地方也有隐患
Record r = new Record(res.getString(1),res.getString(2),res.getString(3),res.getString(4),res.getString(5));
recordList.add(r);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DatabaseUtil.close(res, ps, con);
}
return recordList;
}
//根据用户名 书名 借阅类型查询唯一的记录
public Record getOneByAllkey(String bookname,String uid,String type)
{
Record rc = null;
Connection con=null;
ResultSet res=null;
PreparedStatement ps=null;
con = DatabaseUtil.getConnection();
String sql = "select * from Record where bookname=? AND userId = ? AND type=?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, bookname);
ps.setString(2, uid);
ps.setString(3, type);
res=ps.executeQuery();
rc = new Record(res.getString(1),res.getString(2),res.getString(3),res.getString(4),res.getString(5));
} catch (SQLException e) {
e.printStackTrace();
}
return rc;
}
}
UserDaoimpl.java
package com.qdu.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.qdu.entity.Books;
import com.qdu.entity.User;
import com.qdu.util.DatabaseUtil;
public class UserDaoimpl extends BaseDaoimpl{
//用户的功能
//构函:String userName,String uId,String userPass,String sex,String phoneNum,String eMail
//表Users:Uid Uname Upass Usex Uphone Umail
//添加一个新用户
public int addNew(String userName,String uId,String userPass,String sex,String phoneNum,String eMail)
{
String sql = "insert into Users values(?,?,?,?,?,?)";
return executeUpdate(sql,uId,userName,userPass,sex,phoneNum,eMail);
}
//删除一个用户
public int deleteById(String id)
{
String sql = "delete from Users where Uid=?";
return executeUpdate(sql, id);
}
//更改一个用户的密码
public int updatePass(String id,String newPass)
{
String sql = "update Users set Upass=? where Uid=?";
return executeUpdate(sql, newPass,id);
}
//更改一个用户的信息
public int update(User u)
{
String sql = "update Users set Uname=?, Usex=?, Uphone=?, Umail=? where Uid=?";
return executeUpdate(sql, u.getUserName(),u.getSex(),u.getPhoneNum(),u.geteMail(),u.getuId());
}
//获取一个用户
public User getOneByid(String id)
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
User u=null;
con = DatabaseUtil.getConnection();
String sql = "select * from Users where Uid = ?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, id);
rs = ps.executeQuery();
//这里的相关参数容易出错
//构:userName,uId,userPass,sex,phoneNum,eMail
//表:Uid Uname Upass Usex Uphone Umail
while(rs.next()) {
u = new User(
rs.getString(2),
rs.getString(1),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getString(6)
);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DatabaseUtil.close(rs, ps, con);
}
return u;
}
}
3.com.qdu.services
Service包含业务功能逻辑,Service通过调用Dao层的方法实现功能。Dao层非常纯粹,它只负责和数据库打交道,执行表的增伤改查操作。
因为图书馆服务中有用户和管理员两种人可以操作图书,所以对应这些人有两个service类
这个包里都是纯粹的接口
ManagerService.java
package com.qdu.services;
import java.util.List;
import com.qdu.entity.Record;
import com.qdu.entity.Books;
import com.qdu.entity.Manager;
public interface ManagerService {
//登录功能
public boolean Login(String id,String pass);
//修改密码
public int updatePass(String id,String newpass,String oldpass);
//添加其他管理员账户
public int addNew(String mName, String mId, String mPass);
//获取所有管理员
public List<Manager> getAllManagers();
//删除管理员
public int deleteManager(String id,String pass);
//添加图书
public int addNewBook(String bookName, String bookClass, String position, String writer, Integer quantity);
//修改图书位置
public int updataPos(String name,String newPos);
//修改图书数量
public int updataNum(String name,int newnum);
//根据书名查询图书
public Books searchBooks(String name);
//根据书名关键字查询一堆书
public List<Books> searchBynamekey(String namekey);
//根据类名关键字查询一堆书
public List<Books> searchByclasskey(String classkey);
//根据位置查询一堆书
public List<Books> searchBypos(String pos);
//删除指定图书
public int deleteBook(String name);
//删除一条记录
public int deleteRecord(String userId,String bookName,String type);
//根据用户ID查询借阅记录
public List<Record> getRecordByUid(String id);
//根据用户ID查询未还图书记录
public List<Record> getRecordByUid_false(String id);
//根据书名查询借阅记录
public List<Record> getRecordByBookname(String name);
//查看图书在馆数量
public int getNumofBook(String name);
}
UserService.java
package com.qdu.services;
import java.util.List;
import com.qdu.entity.Books;
import com.qdu.entity.User;
import com.qdu.entity.Record;
//import jdk.vm.ci.code.Register;
public interface UserService {
//登录功能:
public int Login(String id,String pass);
//注册功能:
public int Register(String userName, String uId, String userPass, String sex, String phoneNum, String eMail);
//修改密码:
public int UpdatePass(String id,String newPass,String oldPass);
//修改个人信息:
public int UpdateInfo(User newone);
//根据作者信息查询一堆:
public List<Books> getBywriter(String nme);
//根据书类关键字查询一堆:
public List<Books> getByclskey(String cls);
//根据书名返回一本:
public Books getOneByname(String name);
//查询自己的借还书记录
public List<Record> searchMyrecords(String id);
}
4.com.qdu.services.impl
service包的实现类
ManagerServiceimpl.java
package com.qdu.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.qdu.dao.impl.BooksDaoImpl;
import com.qdu.dao.impl.ManagerDaoimpl;
import com.qdu.dao.impl.RecordDaoimpl;
import com.qdu.entity.Books;
import com.qdu.entity.Manager;
import com.qdu.entity.Record;
import com.qdu.services.ManagerService;
public class ManagerServiceimpl implements ManagerService{
ManagerDaoimpl managerDaoimpl = new ManagerDaoimpl();
BooksDaoImpl booksDaoImpl = new BooksDaoImpl();
RecordDaoimpl recordDaoimpl = new RecordDaoimpl();
@Override
public boolean Login(String id, String pass) {
Manager m = managerDaoimpl.getOneByid(id);
if(m==null)
{
System.out.println("Not found");
return false;
}
else if(!m.getmPass().equals(pass)){
System.out.println("pass error");
return false;
}else {
return true;
}
}
@Override
public int updatePass(String id, String newpass, String oldpass) {
Manager m = managerDaoimpl.getOneByid(id);
if(m==null)
{
System.out.println("sorry we didnt find this manager");
return -1;
}else if(!m.getmPass().equals(oldpass))
{
System.out.println("old pass error");
return -2;
}else {
return managerDaoimpl.updatePass(oldpass, id);
}
}
@Override
public int addNew(String mName, String mId, String mPass) {
Manager m = managerDaoimpl.getOneByid(mId);
if(m==null)
{
return managerDaoimpl.addNew(mName, mId, mPass);
}else {
System.out.println("用户id已存在");
return -1;
}
}
@Override
public int addNewBook(String bookName, String bookClass, String position, String writer, Integer quantity) {
Books b=booksDaoImpl.getAbookByname(bookName);
if(b==null)
{
Books newb = new Books(bookName, bookClass, position, writer, quantity);
return booksDaoImpl.insert(newb);
}else {
System.out.println("this bookname has been inserted");
return -1;
}
}
@Override
public int updataPos(String name, String newPos) {
Books b = booksDaoImpl.getAbookByname(name);
if(b==null)
{
System.out.println("UNdiscover this book");
return -1;
}else {
return booksDaoImpl.updateBookpos(name, newPos);
}
}
@Override
public int updataNum(String name, int newnum) {
Books b = booksDaoImpl.getAbookByname(name);
if(b==null)
{
System.out.println("UNdiscover this book");
return -1;
}else {
return booksDaoImpl.updateBooknum(name, newnum);
}
}
@Override
public Books searchBooks(String name) {
Books b = booksDaoImpl.getAbookByname(name);
if(b==null)
{
System.out.println("not found");
return null;
}else {
return b;
}
}
@Override
public List<Books> searchBynamekey(String namekey) {
return booksDaoImpl.getBooksBynamekey(namekey);
}
@Override
public List<Books> searchByclasskey(String classkey) {
return booksDaoImpl.getBooksByclasskey(classkey);
}
@Override
public List<Books> searchBypos(String pos) {
return booksDaoImpl.getBooksBypos(pos);
}
@Override
public int deleteBook(String name) {
return booksDaoImpl.deleteByname(name);
}
//删除一条记录
@Override
public int deleteRecord(String userId,String bookName,String type)
{
Record rc = recordDaoimpl.getOneByAllkey(bookName, userId, type);
if(rc==null) {
System.out.println("Not found");
return -1;
}else {
return recordDaoimpl.deleteRecord(rc.getBookName(), userId, type);
}
}
//根据用户id获取这个人的借阅记录
@Override
public List<Record> getRecordByUid(String id) {
return recordDaoimpl.getRecordByUserId(id);
}
//根据用户id获取这个人的借阅未还记录
@Override
public List<Record> getRecordByUid_false(String id) {
List<Record> rcList = new ArrayList<>();
rcList=recordDaoimpl.getRecordByUserId(id);
//把这个人的所有的借阅记录中已经还了的删去
for (Record record : rcList) {
if(record.getType().equals("已还")) {
rcList.remove(record);
}
}
return rcList;
}
@Override
public List<Record> getRecordByBookname(String name) {
return recordDaoimpl.getByBookName(name);
}
@Override
public int getNumofBook(String name) {
return booksDaoImpl.getAbookByname(name).getQuantity();
}
@Override
public List<Manager> getAllManagers() {
return managerDaoimpl.getAllmanagers();
}
@Override
public int deleteManager(String id, String pass) {
Manager m = managerDaoimpl.getOneByid(id);
if(m==null)
{
return -1;
}else {
if(!m.getmPass().equals(pass))
{
System.out.println(m.getmPass());
System.out.println(pass);
return -2;
}
else {
return managerDaoimpl.deleteManager(id, pass);
}
}
}
}
UserServiceimpl.java
package com.qdu.service.impl;
import java.util.List;
import com.qdu.dao.impl.BooksDaoImpl;
import com.qdu.dao.impl.RecordDaoimpl;
import com.qdu.dao.impl.UserDaoimpl;
import com.qdu.entity.Books;
import com.qdu.entity.User;
import com.qdu.entity.Record;
import com.qdu.services.UserService;
public class UserServiceimpl implements UserService{
private UserDaoimpl userDaoimpl = new UserDaoimpl();
private BooksDaoImpl booksDaoimpl=new BooksDaoImpl();
private RecordDaoimpl recordDaoimpl = new RecordDaoimpl();
//登录函数:登录成功返回true 不成功(密码不对)
@Override
public int Login(String id, String pass) {
User u = userDaoimpl.getOneByid(id);
if(u==null)
{
System.out.println("Not found");
return 1;
}else if(!u.getUserPass().equals(pass)){
System.out.println("pass error");
return 2;
}else {
return 3;
}
}
//注册:返回整形(影响的表格里的行数,如果注册失败就返回-1)
@Override
public int Register(String userName, String uId, String userPass, String sex, String phoneNum, String eMail) {
User u=userDaoimpl.getOneByid(uId);
if(u==null)
{
return userDaoimpl.addNew(userName, uId, userPass, sex, phoneNum, eMail);
}else {
return -1;
}
}
@Override
public int UpdatePass(String id, String newPass, String oldPass) {
return userDaoimpl.updatePass(id, newPass);
}
//修改用户信息的函数:输入id、密码以及其他信息,如果找不到这个id则修改失败用户不存在,如果用户存在就对比密码,如果密码不符合
//也是修改失败,如果密码和id都对了就可以正常修改
@Override
public int UpdateInfo(User u) {
return userDaoimpl.update(u);
}
//根据作者名字的关键字来获取一堆书
@Override
public List<Books> getBywriter(String nme) {
return booksDaoimpl.getBooksBynamekey(nme);
}
//根据书记所属类别来获取一堆书
@Override
public List<Books> getByclskey(String cls) {
return booksDaoimpl.getBooksByclasskey(cls);
}
//根据书籍的名字获取一本书
@Override
public Books getOneByname(String name) {
return booksDaoimpl.getAbookByname(name);
}
@Override
public List<Record> searchMyrecords(String id) {
return recordDaoimpl.getRecordByUserId(id);
}
}
5.com.qdu.frames
该包实现了应用的操作页面,包括页面的布局和触发事件等,可通过Design页面直接拖动自动生成代码
Home2.java:图书馆管理系统首页,包括用户登录和管理员登陆。
ManagerHome.java:管理员操作页面
UserHome.java:用户操作页面
Uregister.java:用户注册页面
Home2.java
package com.qdu.frames;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import com.qdu.service.impl.ManagerServiceimpl;
import com.qdu.service.impl.UserServiceimpl;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Home2 extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Home2 frame = new Home2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Home2() {
this.setSize(787, 563);
this.setLocationRelativeTo(null);
this.setTitle("��ҳ");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
panel1.setBackground(Color.WHITE);
panel2.setBackground(Color.WHITE);
JTabbedPane pane = new JTabbedPane(JTabbedPane.BOTTOM);//��������Ӳ���ָ��λ�ã�TOP LEFT ... ...
//2. ��ҳǩ�����ӵ�������
getContentPane().add(pane);
//3. ������JPanel��ӵ�ҳǩ����У���Ϊһ��ҳǩ
pane.addTab("管理员登陆", panel1);
panel1.setLayout(null);
JLabel lblNewLabel = new JLabel("\u7BA1\u7406\u5458\u767B\u9646");
lblNewLabel.setFont(new Font("���Ŀ���", Font.BOLD, 33));
lblNewLabel.setBounds(289, 118, 178, 52);
panel1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u8D26\u53F7ID\uFF1A");
lblNewLabel_1.setFont(new Font("���ź�", Font.PLAIN, 25));
lblNewLabel_1.setBounds(193, 220, 115, 34);
panel1.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("\u5BC6\u7801\uFF1A");
lblNewLabel_2.setFont(new Font("���ź�", Font.PLAIN, 25));
lblNewLabel_2.setBounds(193, 300, 115, 34);
panel1.add(lblNewLabel_2);
textField = new JTextField();
textField.setBounds(331, 219, 230, 40);
panel1.add(textField);
textField.setColumns(10);
JPasswordField textField_1 = new JPasswordField();
textField_1.setBounds(331, 301, 230, 40);
textField_1.setColumns(10);
textField_1.setEchoChar('*');
panel1.add(textField_1);
JButton btnNewButton = new JButton("\u767B\u5F55");
btnNewButton.setFont(new Font("���ź�", Font.PLAIN, 20));
btnNewButton.setBounds(331, 414, 109, 34);
panel1.add(btnNewButton);
pane.addTab("用户登录", panel2);
panel2.setLayout(null);
JLabel lblNewLabel_3 = new JLabel("\u7528\u6237\u767B\u5F55");
lblNewLabel_3.setFont(new Font("���Ŀ���", Font.BOLD, 33));
lblNewLabel_3.setBounds(295, 118, 190, 43);
panel2.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("\u7528\u6237\u540DID\uFF1A");
lblNewLabel_4.setFont(new Font("���ź�", Font.PLAIN, 25));
lblNewLabel_4.setBounds(193, 220, 130, 43);
panel2.add(lblNewLabel_4);
JLabel lblNewLabel_5 = new JLabel("\u5BC6\u7801\uFF1A");
lblNewLabel_5.setFont(new Font("���ź�", Font.PLAIN, 25));
lblNewLabel_5.setBounds(193, 300, 130, 43);
panel2.add(lblNewLabel_5);
textField_2 = new JTextField();
textField_2.setBounds(331, 219, 230, 40);
panel2.add(textField_2);
textField_2.setColumns(10);
JPasswordField textField_3 = new JPasswordField();
textField_3.setBounds(331, 301, 230, 40);
textField_3.setColumns(10);
textField_3.setEchoChar('*');
panel2.add(textField_3);
JButton btnNewButton_1 = new JButton("\u767B\u5F55");
btnNewButton_1.setFont(new Font("���ź�", Font.PLAIN, 16));
btnNewButton_1.setBounds(219, 428, 104, 34);
panel2.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("\u6CE8\u518C");
btnNewButton_2.setFont(new Font("���ź�", Font.PLAIN, 16));
btnNewButton_2.setBounds(443, 428, 104, 34);
panel2.add(btnNewButton_2);
UserServiceimpl userServiceimpl = new UserServiceimpl();
ManagerServiceimpl managerServiceimpl = new ManagerServiceimpl();
//给Manager的登录按钮添加事件
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String mIdString = textField.getText();
System.out.println(mIdString);
String mPassString = new String(textField_1.getPassword());//获取密码要这样哦!!!
if(managerServiceimpl.Login(mIdString, mPassString))
{
ManagerHome mHome = new ManagerHome(mIdString);
mHome.setVisible(true);
Home2.this.setVisible(false);
}else {
JOptionPane.showMessageDialog(Home2.this,"密码错误","注意",JOptionPane.ERROR_MESSAGE);
}
}
});
//给user的登录按钮添加事件
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String uIdString = textField_2.getText().trim();
String uPassString = new String(textField_3.getPassword());//获取密码要这样哦!!!
System.out.println(uIdString+uPassString);
int a = userServiceimpl.Login(uIdString, uPassString);
if(a==3)
{
UserHome uHome = new UserHome(uIdString,uPassString);//别忘了传一下参数哈,这里先都不传
uHome.setVisible(true);
}else if(a==2){
JOptionPane.showMessageDialog(Home2.this,"密码错误","注意",JOptionPane.ERROR_MESSAGE);
}else {
JOptionPane.showMessageDialog(Home2.this,"用户不存在,请注册","注意",JOptionPane.ERROR_MESSAGE);
}
}
});
//给user的注册按钮添加事件打开注册界面
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Uregister uregister = new Uregister();
uregister.setVisible(true);
}
});
}
}
ManagerHome.java
package com.qdu.frames;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import com.qdu.entity.Books;
import com.qdu.entity.Manager;
import com.qdu.entity.Record;
import com.qdu.service.impl.ManagerServiceimpl;
import com.qdu.service.impl.UserServiceimpl;
public class ManagerHome extends JFrame {
private JPanel contentPane;
private JTable table;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;
private JTextField textField_9;
private JTextField textField_10;
private JTextField textField_11;
private JTextField textField_12;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ManagerHome frame = new ManagerHome(" ");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ManagerHome(String id) {
this.setTitle("管理员界面首页");
this.setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 952, 1024);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JTabbedPane pane = new JTabbedPane(JTabbedPane.BOTTOM);//创建页签面板
JPanel accountPanel = new JPanel();
accountPanel.setBackground(Color.WHITE);
getContentPane().add(pane);
pane.add("账户管理",accountPanel);
accountPanel.setLayout(null);
JLabel lblNewLabel_5 = new JLabel("账户管理");
lblNewLabel_5.setFont(new Font("华文楷体", Font.PLAIN, 37));
lblNewLabel_5.setBounds(369, 10, 153, 46);
accountPanel.add(lblNewLabel_5);
JLabel lblNewLabel_6 = new JLabel("当前管理员:");
lblNewLabel_6.setFont(new Font("微软雅黑", Font.PLAIN, 21));
lblNewLabel_6.setBounds(37, 164, 153, 33);
accountPanel.add(lblNewLabel_6);
//账户管理的第一个JTable
JTable table_acc1 = new JTable();
DefaultTableCellRenderer renderer3 = new DefaultTableCellRenderer();
renderer3.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
table_acc1.setDefaultRenderer(Object.class, renderer3);
table_acc1.setAutoCreateRowSorter(true);
JScrollPane scrollPane_2 = new JScrollPane(table_acc1);
scrollPane_2.setBackground(Color.WHITE);
scrollPane_2.setBounds(183, 203, 708, 163);
accountPanel.add(scrollPane_2);
JLabel lblNewLabel_6_1 = new JLabel("新建管理员:");
lblNewLabel_6_1.setFont(new Font("微软雅黑", Font.PLAIN, 21));
lblNewLabel_6_1.setBounds(37, 388, 153, 33);
accountPanel.add(lblNewLabel_6_1);
//新建管理员:姓名框
textField_6 = new JTextField();
textField_6.setColumns(10);
textField_6.setBounds(164, 439, 111, 33);
accountPanel.add(textField_6);
JLabel lblNewLabel_3_4 = new JLabel("姓名:");
lblNewLabel_3_4.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_4.setBounds(95, 440, 59, 24);
accountPanel.add(lblNewLabel_3_4);
JLabel lblNewLabel_3_5 = new JLabel("账号:");
lblNewLabel_3_5.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_5.setBounds(322, 440, 59, 24);
accountPanel.add(lblNewLabel_3_5);
//新建管理员:账号框
textField_7 = new JTextField();
textField_7.setColumns(10);
textField_7.setBounds(391, 439, 111, 33);
accountPanel.add(textField_7);
JLabel lblNewLabel_3_5_1 = new JLabel("密码:");
lblNewLabel_3_5_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_5_1.setBounds(560, 440, 59, 24);
accountPanel.add(lblNewLabel_3_5_1);
//新建管理员:密码框
textField_8 = new JTextField();
textField_8.setColumns(10);
textField_8.setBounds(629, 439, 111, 33);
accountPanel.add(textField_8);
JButton btnNewButton_1 = new JButton("更新");
btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
btnNewButton_1.setBorder(new LineBorder(new Color(0, 0, 0), 0, true));
btnNewButton_1.setBounds(183, 170, 59, 23);
accountPanel.add(btnNewButton_1);
JButton btnNewButton_2_1_2 = new JButton("点击添加");
btnNewButton_2_1_2.setFont(new Font("微软雅黑", Font.PLAIN, 17));
btnNewButton_2_1_2.setBorder(new LineBorder(new Color(0, 0, 0), 0, true));
btnNewButton_2_1_2.setBounds(778, 444, 101, 23);
accountPanel.add(btnNewButton_2_1_2);
JLabel lblNewLabel_6_1_1 = new JLabel("账户删除:");
lblNewLabel_6_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 21));
lblNewLabel_6_1_1.setBounds(37, 502, 153, 33);
accountPanel.add(lblNewLabel_6_1_1);
JLabel lblNewLabel_3_5_2 = new JLabel("确认账号:");
lblNewLabel_3_5_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_5_2.setBounds(95, 545, 95, 24);
accountPanel.add(lblNewLabel_3_5_2);
//账户删除操作里的账号:
textField_9 = new JTextField();
textField_9.setColumns(10);
textField_9.setBounds(201, 544, 111, 33);
accountPanel.add(textField_9);
JLabel lblNewLabel_3_5_3 = new JLabel("确认密码:");
lblNewLabel_3_5_3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_5_3.setBounds(375, 545, 95, 24);
accountPanel.add(lblNewLabel_3_5_3);
//账户删除里的密码:
textField_10 = new JTextField();
textField_10.setColumns(10);
textField_10.setBounds(480, 545, 111, 33);
accountPanel.add(textField_10);
JButton btnNewButton_2_1_2_1 = new JButton("确定删除");
btnNewButton_2_1_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
btnNewButton_2_1_2_1.setBorder(new LineBorder(new Color(0, 0, 0), 0, true));
btnNewButton_2_1_2_1.setBounds(662, 549, 101, 23);
accountPanel.add(btnNewButton_2_1_2_1);
JLabel lblNewLabel_6_1_1_1 = new JLabel("当前借阅记录:");
lblNewLabel_6_1_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 21));
lblNewLabel_6_1_1_1.setBounds(37, 608, 153, 33);
accountPanel.add(lblNewLabel_6_1_1_1);
//账户管理页面第二个JTable:
JTable table_acc2 = new JTable();
DefaultTableCellRenderer renderer4 = new DefaultTableCellRenderer();
renderer4.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
table_acc2.setDefaultRenderer(Object.class, renderer4);
table_acc2.setAutoCreateRowSorter(true);
JScrollPane scrollPane_2_1 = new JScrollPane(table_acc2);
scrollPane_2_1.setBackground(Color.WHITE);
scrollPane_2_1.setBounds(183, 647, 708, 163);
accountPanel.add(scrollPane_2_1);
//1. 创建单选按钮,指定文本内容和是否选中
JRadioButton rb1=new JRadioButton("未还");
rb1.setSize(59, 16);
rb1.setLocation(95, 698);
JRadioButton rb2=new JRadioButton("已还");
rb2.setSize(59, 16);
rb2.setLocation(95, 680);
JRadioButton rb3=new JRadioButton("全部");
rb3.setSize(59, 16);
rb3.setLocation(95, 660);
ButtonGroup btnGroup=new ButtonGroup();
rb3.setSelected(true);
rb1.setOpaque(false);
rb2.setOpaque(false);
rb3.setOpaque(false);
btnGroup.add(rb1);
btnGroup.add(rb2);
btnGroup.add(rb3);
accountPanel.add(rb1);accountPanel.add(rb2);accountPanel.add(rb3);
JLabel lblNewLabel_3_5_2_1 = new JLabel("指定用户ID:");
lblNewLabel_3_5_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_5_2_1.setBounds(180, 832, 111, 24);
accountPanel.add(lblNewLabel_3_5_2_1);
textField_11 = new JTextField();
textField_11.setColumns(10);
textField_11.setBounds(301, 831, 111, 33);
accountPanel.add(textField_11);
JLabel lblNewLabel_3_5_2_1_1 = new JLabel("指定书名:");
lblNewLabel_3_5_2_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_5_2_1_1.setBounds(519, 832, 111, 24);
accountPanel.add(lblNewLabel_3_5_2_1_1);
textField_12 = new JTextField();
textField_12.setColumns(10);
textField_12.setBounds(609, 831, 111, 33);
accountPanel.add(textField_12);
JButton btnNewButton_2_1_1_1 = new JButton("搜索");
btnNewButton_2_1_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
btnNewButton_2_1_1_1.setBorder(new LineBorder(new Color(0, 0, 0), 0, true));
btnNewButton_2_1_1_1.setBounds(719, 830, 59, 33);
accountPanel.add(btnNewButton_2_1_1_1);
JButton btnNewButton_2_1_1_2 = new JButton("搜索");
btnNewButton_2_1_1_2.setFont(new Font("微软雅黑", Font.PLAIN, 17));
btnNewButton_2_1_1_2.setBorder(new LineBorder(new Color(0, 0, 0), 0, true));
btnNewButton_2_1_1_2.setBounds(411, 830, 59, 33);
accountPanel.add(btnNewButton_2_1_1_2);
JPanel bookPanel = new JPanel();
bookPanel.setBackground(Color.WHITE);
pane.add("图书管理",bookPanel);
bookPanel.setLayout(null);
JLabel lblNewLabel = new JLabel("图书管理");
lblNewLabel.setBounds(369, 10, 153, 46);
lblNewLabel.setFont(new Font("华文楷体", Font.PLAIN, 37));
bookPanel.add(lblNewLabel);
table = new JTable();
//这部分到底是用来干啥的呢
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
table.setDefaultRenderer(Object.class, renderer);
table.setAutoCreateRowSorter(true);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(183, 123, 708, 163);
scrollPane.setPreferredSize(new Dimension(700, 120));
bookPanel.add(scrollPane);
JButton btnNewButton = new JButton("更新");
btnNewButton.setBounds(37, 184, 59, 23);
btnNewButton.setBorder(new LineBorder(new Color(0, 0, 0), 0, true));
btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 17));
bookPanel.add(btnNewButton);
JLabel lblNewLabel_1 = new JLabel("馆内现有图书:");
lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 21));
lblNewLabel_1.setBounds(37, 117, 153, 33);
bookPanel.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("添加图书:");
lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 21));
lblNewLabel_2.setBounds(37, 320, 111, 33);
bookPanel.add(lblNewLabel_2);
textField = new JTextField();
textField.setBounds(183, 363, 111, 33);
bookPanel.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_3 = new JLabel("书名:");
lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3.setBounds(114, 372, 59, 24);
bookPanel.add(lblNewLabel_3);
String[] items = {"历史文献","新闻杂志","科学研究","文化文艺","文学名著","论文期刊"};
JComboBox<String> comboBox = new JComboBox<String>(items);
comboBox.setBounds(450, 373, 90, 23);
bookPanel.add(comboBox);
JLabel lblNewLabel_4 = new JLabel("类别:");
lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_4.setBounds(375, 372, 59, 24);
bookPanel.add(lblNewLabel_4);
JLabel lblNewLabel_3_1 = new JLabel("作者:");
lblNewLabel_3_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_1.setBounds(581, 372, 59, 24);
bookPanel.add(lblNewLabel_3_1);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(650, 363, 111, 33);
bookPanel.add(textField_1);
JLabel lblNewLabel_3_2 = new JLabel("数量:");
lblNewLabel_3_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_2.setBounds(114, 428, 59, 24);
bookPanel.add(lblNewLabel_3_2);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(183, 428, 111, 33);
bookPanel.add(textField_2);
JLabel lblNewLabel_4_1 = new JLabel("位置:");
lblNewLabel_4_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_4_1.setBounds(375, 428, 59, 24);
bookPanel.add(lblNewLabel_4_1);
JButton btnNewButton_2 = new JButton("点击添加");
btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 17));
btnNewButton_2.setBorder(new LineBorder(new Color(0, 0, 0), 0, true));
btnNewButton_2.setBounds(650, 429, 101, 23);
bookPanel.add(btnNewButton_2);
JLabel lblNewLabel_2_1 = new JLabel("修改图书信息:");
lblNewLabel_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 21));
lblNewLabel_2_1.setBounds(37, 491, 153, 33);
bookPanel.add(lblNewLabel_2_1);
JLabel lblNewLabel_3_3 = new JLabel("书名:");
lblNewLabel_3_3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_3.setBounds(114, 534, 59, 24);
bookPanel.add(lblNewLabel_3_3);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(183, 525, 111, 33);
bookPanel.add(textField_3);
JLabel lblNewLabel_3_2_1 = new JLabel("更改数量为:");
lblNewLabel_3_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_2_1.setBounds(359, 525, 111, 24);
bookPanel.add(lblNewLabel_3_2_1);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(480, 525, 111, 33);
bookPanel.add(textField_4);
JLabel lblNewLabel_4_1_1 = new JLabel("更改位置为:");
lblNewLabel_4_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_4_1_1.setBounds(634, 525, 117, 24);
bookPanel.add(lblNewLabel_4_1_1);
String[] items3 = {"一楼西区","一楼东区","一楼南区","一楼北区","二楼西区","二楼东区","二楼南区","二楼北区"};
JComboBox<String> comboBox_3 = new JComboBox<String>(items3);
comboBox_3.setBounds(800, 530, 90, 23);
bookPanel.add(comboBox_3);
JButton btnNewButton_2_1 = new JButton("点击更新");
btnNewButton_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
btnNewButton_2_1.setBorder(new LineBorder(new Color(0, 0, 0), 0, true));
btnNewButton_2_1.setBounds(114, 589, 101, 23);
bookPanel.add(btnNewButton_2_1);
JButton btnNewButton_2_2 = new JButton("点击删除");
btnNewButton_2_2.setFont(new Font("微软雅黑", Font.PLAIN, 17));
btnNewButton_2_2.setBorder(new LineBorder(new Color(0, 0, 0), 0, true));
btnNewButton_2_2.setBounds(291, 589, 101, 23);
bookPanel.add(btnNewButton_2_2);
JLabel lblNewLabel_2_1_1 = new JLabel("图书查询:");
lblNewLabel_2_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 21));
lblNewLabel_2_1_1.setBounds(37, 650, 153, 33);
bookPanel.add(lblNewLabel_2_1_1);
//第二个预览表格:
JTable table2 = new JTable();
DefaultTableCellRenderer renderer2 = new DefaultTableCellRenderer();
renderer2.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
table2.setDefaultRenderer(Object.class, renderer2);
table2.setAutoCreateRowSorter(true);
JScrollPane scrollPane_1 = new JScrollPane(table2);
scrollPane_1.setPreferredSize(new Dimension(700, 120));
scrollPane_1.setBounds(183, 672, 708, 120);
bookPanel.add(scrollPane_1);
JLabel lblNewLabel_4_2 = new JLabel("按类别筛选:");
lblNewLabel_4_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_4_2.setBounds(183, 819, 111, 24);
bookPanel.add(lblNewLabel_4_2);
String[] items4 = {"历史文献","新闻杂志","科学研究","文化文艺","文学名著","论文期刊"};
JComboBox<String> comboBox_2 = new JComboBox<String>(items4);
comboBox_2.setBounds(358, 823, 90, 23);
bookPanel.add(comboBox_2);
JLabel lblNewLabel_3_3_1 = new JLabel("书名匹配:");
lblNewLabel_3_3_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_3_3_1.setBounds(581, 819, 90, 24);
bookPanel.add(lblNewLabel_3_3_1);
textField_5 = new JTextField();
textField_5.setColumns(10);
textField_5.setBounds(681, 819, 111, 33);
bookPanel.add(textField_5);
JButton btnNewButton_2_1_1 = new JButton("搜索");
btnNewButton_2_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
btnNewButton_2_1_1.setBorder(new LineBorder(new Color(0, 0, 0), 0, true));
btnNewButton_2_1_1.setBounds(790, 819, 59, 33);
bookPanel.add(btnNewButton_2_1_1);
JLabel lblNewLabel_4_2_1 = new JLabel("按位置筛选:");
lblNewLabel_4_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
lblNewLabel_4_2_1.setBounds(183, 874, 111, 24);
bookPanel.add(lblNewLabel_4_2_1);
String[] items5 = {"一楼西区","一楼东区","一楼南区","一楼北区","二楼西区","二楼东区","二楼南区","二楼北区"};
JComboBox<String> comboBox_4 = new JComboBox<String>(items5);
comboBox_4.setBounds(358, 874, 90, 23);
bookPanel.add(comboBox_4);
String[] items2 = {"一楼西区","一楼东区","一楼南区","一楼北区","二楼西区","二楼东区","二楼南区","二楼北区"};
JComboBox<String> comboBox_1 = new JComboBox<String>(items2);
comboBox_1.setBounds(450, 432, 90, 23);
bookPanel.add(comboBox_1);
//图书管理的事件:
UserServiceimpl userServiceimpl = new UserServiceimpl();
ManagerServiceimpl managerServiceimpl = new ManagerServiceimpl();
//为更新按钮添加事件:获取所有图书显示在JTable里面
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
List<Books> booklist = managerServiceimpl.searchBynamekey("");
Vector<String> colName = new Vector<String>();
colName.add("书名");colName.add("作者");colName.add("类别");
colName.add("剩余(本)");colName.add("位置");
Vector data = new Vector();//用这个Vector对象来存储从表格获取的学生信息
for(Books b : booklist) {
//每个学生的数据,也就是每行数据,使用一个Vector对象封装
//一个学生的每个信息可能数据类型不同,所以这里使用Object类型
Vector row = new Vector();
row.add(b.getBookName());
row.add(b.getWriter());
row.add(b.getBookClass());
row.add(b.getQuantity());
row.add(b.getPosition());
data.add(row);
}
// 可以将数据封装到一个TableModel对象中,设置为JTable的数据模型
// TableModel的常用的一个实现类是DefaultTableModel
// 指定包含数据的集合和包含列表的集合,创建TableModel对象
DefaultTableModel model = new DefaultTableModel(data,colName);
// 将填充了数据的TableModel设置为JTable组件的数据模型,也就是填充JTable的数据
// 也就是将数据显示在JTable中
table.setModel(model);
}
});
//为电磁添加按钮添加事件:
btnNewButton_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String bkname = textField.getText();//获取书名
String str = textField_2.getText();//获取书的数量
int bknum = Integer.valueOf(str);
String writeName = textField_1.getText();//获取作者名字
String bkclass = comboBox.getSelectedItem().toString();//获取类别
String bkpos = comboBox_1.getSelectedItem().toString();//获取位置
if(bkname.isEmpty()||str.isEmpty()||writeName.isEmpty()||bkclass.isEmpty()||bkpos.isEmpty())
{
JOptionPane.showMessageDialog(ManagerHome.this,"请完整输入您要添加的图书信息","注意",JOptionPane.ERROR_MESSAGE);
}else {
if(bknum<0) {
JOptionPane.showMessageDialog(ManagerHome.this,"请输入正确的书籍数量","注意",JOptionPane.ERROR_MESSAGE);
}else {
int state=managerServiceimpl.addNewBook(bkname,bkclass,bkpos,writeName,bknum);
if(state==-1) {
JOptionPane.showMessageDialog(ManagerHome.this,"添加失败:该书已经存在","注意",JOptionPane.ERROR_MESSAGE);
}else {
JOptionPane.showMessageDialog(ManagerHome.this,"添加成功!:该书已经存在","提示",JOptionPane.WARNING_MESSAGE);
}
}
}
}
});
//为更新图书信息按钮添加:
btnNewButton_2_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bkname = textField_3.getText();
int bknum = -1;//Integer.valueOf();
if(!textField_4.getText().isEmpty())
{
bknum=Integer.valueOf(textField_4.getText());
System.out.println(bknum);
}
String bkPos = comboBox_3.getSelectedItem().toString();
if(bknum>=0) {
int a=managerServiceimpl.updataNum(bkname, bknum);
int b=managerServiceimpl.updataPos(bkname, bkPos);
if(a==-1||b==-1)
{
JOptionPane.showMessageDialog(ManagerHome.this,"修改失败:该书不存在","注意",JOptionPane.ERROR_MESSAGE);
}else {
JOptionPane.showMessageDialog(ManagerHome.this,"修改成功","提示",JOptionPane.WARNING_MESSAGE);
}
}else {
JOptionPane.showMessageDialog(ManagerHome.this,"请输入正确的书籍数量","注意",JOptionPane.ERROR_MESSAGE);
}
}
});
//为点击删除按钮添加:
btnNewButton_2_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bkname = textField_3.getText();
if(bkname.isEmpty())
{
JOptionPane.showMessageDialog(ManagerHome.this,"书名不能为空","注意",JOptionPane.WARNING_MESSAGE);
}else if(managerServiceimpl.searchBooks(bkname)==null){
JOptionPane.showMessageDialog(ManagerHome.this,"该书不存在","注意",JOptionPane.ERROR_MESSAGE);
}else {
managerServiceimpl.deleteBook(bkname);
JOptionPane.showMessageDialog(ManagerHome.this,"删除成功","提示",JOptionPane.WARNING_MESSAGE);
}
}
});
//为按类别筛选下拉列表combBox_2添加事件:
comboBox_2.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
String bkclass = comboBox_2.getSelectedItem().toString();
List<Books> booklist = managerServiceimpl.searchByclasskey(bkclass);
Vector<String> colName = new Vector<String>();
colName.add("书名");colName.add("作者");colName.add("类别");
colName.add("剩余(本)");colName.add("位置");
Vector data = new Vector();
for(Books b : booklist) {
Vector row = new Vector();
row.add(b.getBookName());
row.add(b.getWriter());
row.add(b.getBookClass());
row.add(b.getQuantity());
row.add(b.getPosition());
data.add(row);
}
DefaultTableModel model = new DefaultTableModel(data,colName);
table2.setModel(model);
}
});
//为按类筛选添加事件:
comboBox_4.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
String bkPos = comboBox_4.getSelectedItem().toString();
List<Books> booklist = managerServiceimpl.searchBypos(bkPos);
Vector<String> colName = new Vector<String>();
colName.add("书名");colName.add("作者");colName.add("类别");
colName.add("剩余(本)");colName.add("位置");
Vector data = new Vector();
for(Books b : booklist) {
Vector row = new Vector();
row.add(b.getBookName());
row.add(b.getWriter());
row.add(b.getBookClass());
row.add(b.getQuantity());
row.add(b.getPosition());
data.add(row);
}
DefaultTableModel model = new DefaultTableModel(data,colName);
table2.setModel(model);
}
});
//按书名搜索按钮:
btnNewButton_2_1_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bkname = textField_5.getText();
List<Books> booklist = managerServiceimpl.searchBynamekey(bkname);
Vector<String> colName = new Vector<String>();
colName.add("书名");colName.add("作者");colName.add("类别");
colName.add("剩余(本)");colName.add("位置");
Vector data = new Vector();
for(Books b : booklist) {
Vector row = new Vector();
row.add(b.getBookName());
row.add(b.getWriter());
row.add(b.getBookClass());
row.add(b.getQuantity());
row.add(b.getPosition());
data.add(row);
}
DefaultTableModel model = new DefaultTableModel(data,colName);
table2.setModel(model);
}
});
//账户管理的事件:
//为更新按钮添加事件获取最新管理员列表:
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
List<Manager> mList = managerServiceimpl.getAllManagers();
Vector<String> colName = new Vector<String>();
colName.add("姓名");colName.add("账号");colName.add("密码");
Vector data = new Vector();
for(Manager m : mList) {
Vector row = new Vector();
row.add(m.getmName());
row.add(m.getmId());
row.add("********");
data.add(row);
}
DefaultTableModel model = new DefaultTableModel(data,colName);
table_acc1.setModel(model);
}
});
//为添加管理员按钮添加事件:
btnNewButton_2_1_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String mName = textField_6.getText();
String mIdString = textField_7.getText();
String mPass = textField_8.getText();
if(mName.isEmpty()||mIdString.isEmpty()||mPass.isEmpty())
{
JOptionPane.showMessageDialog(ManagerHome.this,"请输入完整您要添加的管理员信息","注意",JOptionPane.WARNING_MESSAGE);
}else {
Manager m = new Manager(mName, mIdString, mPass);
int a = managerServiceimpl.addNew(mName, mIdString, mPass);
if(a==-1)
{
JOptionPane.showMessageDialog(ManagerHome.this,"添加失败:该用户ID已经存在","注意",JOptionPane.ERROR_MESSAGE);
}
else {
JOptionPane.showMessageDialog(ManagerHome.this,"添加成功,请刷新管理员列表!","成功",JOptionPane.WARNING_MESSAGE);
}
}
}
});
//为删除管理员按钮添加:
btnNewButton_2_1_2_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String mId = textField_9.getText();
String pass = textField_10.getText();
if(mId.isEmpty()||pass.isEmpty()) {
JOptionPane.showMessageDialog(ManagerHome.this,"请输入完整您要添加的管理员信息","注意",JOptionPane.WARNING_MESSAGE);
}else {
int a = managerServiceimpl.deleteManager(mId,pass);
if(a==-1)
{
JOptionPane.showMessageDialog(ManagerHome.this,"删除失败:该用户不存在","注意",JOptionPane.ERROR_MESSAGE);
}
else if(a==-2)
{
JOptionPane.showMessageDialog(ManagerHome.this,"删除失败:密码错误","注意",JOptionPane.ERROR_MESSAGE);
}else {
JOptionPane.showMessageDialog(ManagerHome.this,"删除成功,请刷新管理员列表!","成功",JOptionPane.WARNING_MESSAGE);
}
}
}
});
//指定用户ID旁边的搜索:
btnNewButton_2_1_1_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String userId = textField_11.getText();
List<Record> rcList = managerServiceimpl.getRecordByUid(userId);
Vector<String> colName = new Vector<String>();
colName.add("书名");colName.add("借阅人");colName.add("借阅人账号");colName.add("借阅状态");colName.add("借阅日期");
//获取单选按钮的状态:rb1未还 rb2已还 rb3全部
Vector data = new Vector();
if(rb3.isSelected())
{
for(Record r : rcList)
{
Vector row = new Vector();
row.add(r.getBookName());
row.add(r.getUserName());
row.add(r.getUserId());
row.add(r.getType());
row.add(r.getDate());
data.add(row);
}
}
if(rb1.isSelected())
{
for(Record r : rcList)
{
Vector row = new Vector();
row.add(r.getBookName());
row.add(r.getUserName());
row.add(r.getUserId());
row.add(r.getType());
row.add(r.getDate());
if(r.getType().equals("未还"))
{
data.add(row);
}
}
}
if(rb2.isSelected())
{
for(Record r : rcList)
{
Vector row = new Vector();
row.add(r.getBookName());
row.add(r.getUserName());
row.add(r.getUserId());
row.add(r.getType());
row.add(r.getDate());
if(r.getType().equals("已还"))
{
data.add(row);
}
}
}
DefaultTableModel model = new DefaultTableModel(data,colName);
table_acc2.setModel(model);
}
});
//指定书名搜索的那个搜索框
btnNewButton_2_1_1_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String bokname = textField_12.getText();
List<Record> rcList = managerServiceimpl.getRecordByBookname(bokname);
Vector<String> colName = new Vector<String>();
colName.add("书名");colName.add("借阅人");colName.add("借阅人账号");colName.add("借阅状态");colName.add("借阅日期");
//获取单选按钮的状态:rb1未还 rb2已还 rb3全部
Vector data = new Vector();
if(rb3.isSelected())
{
for(Record r : rcList)
{
Vector row = new Vector();
row.add(r.getBookName());
row.add(r.getUserName());
row.add(r.getUserId());
row.add(r.getType());
row.add(r.getDate());
data.add(row);
}
}
if(rb1.isSelected())
{
for(Record r : rcList)
{
Vector row = new Vector();
row.add(r.getBookName());
row.add(r.getUserName());
row.add(r.getUserId());
row.add(r.getType());
row.add(r.getDate());
if(r.getType().equals("未还"))
{
data.add(row);
}
}
}
if(rb2.isSelected())
{
for(Record r : rcList)
{
Vector row = new Vector();
row.add(r.getBookName());
row.add(r.getUserName());
row.add(r.getUserId());
row.add(r.getType());
row.add(r.getDate());
if(r.getType().equals("已还"))
{
data.add(row);
}
}
}
DefaultTableModel model = new DefaultTableModel(data,colName);
table_acc2.setModel(model);
}
});
}
}
UserHome.java
package com.qdu.frames;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import com.qdu.dao.impl.BooksDaoImpl;
import com.qdu.dao.impl.RecordDaoimpl;
import com.qdu.dao.impl.UserDaoimpl;
import com.qdu.entity.Books;
import com.qdu.entity.Record;
import com.qdu.entity.User;
import com.qdu.service.impl.UserServiceimpl;
public class UserHome extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserHome frame = new UserHome("","");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
private String id;//用这个变量接收传来的id
private String pass;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
public UserHome(String id,String pass) {
this.id=id;
this.pass=pass;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 776, 870);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
UserServiceimpl userServiceimpl = new UserServiceimpl();
UserDaoimpl userDaoimpl = new UserDaoimpl();
User userSelf = userDaoimpl.getOneByid(id);
JPanel panelBook=new JPanel();
JPanel panelAccount=new JPanel();
panelBook.setBackground(Color.WHITE);
panelAccount.setBackground(Color.WHITE);
JTabbedPane tabbedPane=new JTabbedPane(JTabbedPane.BOTTOM);
tabbedPane.add("图书服务",panelBook);
panelBook.setLayout(null);
JLabel lblNewLabel_3 = new JLabel("个人账户管理");
lblNewLabel_3.setFont(new Font("华文楷体", Font.PLAIN, 28));
lblNewLabel_3.setBounds(285, 20, 179, 54);
panelBook.add(lblNewLabel_3);
JLabel lblNewLabel_1_2 = new JLabel("馆藏图书:");
lblNewLabel_1_2.setFont(new Font("微软雅黑", Font.PLAIN, 19));
lblNewLabel_1_2.setBounds(33, 92, 98, 35);
panelBook.add(lblNewLabel_1_2);
JLabel lblNewLabel_1_2_1 = new JLabel("我的借阅记录:");
lblNewLabel_1_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 19));
lblNewLabel_1_2_1.setBounds(33, 547, 144, 35);
panelBook.add(lblNewLabel_1_2_1);
JTable table_books = new JTable();
DefaultTableCellRenderer renderer1 = new DefaultTableCellRenderer();
renderer1.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
table_books.setDefaultRenderer(Object.class, renderer1);
table_books.setAutoCreateRowSorter(true);
JScrollPane scrollPane = new JScrollPane(table_books);
scrollPane.setBackground(Color.WHITE);
scrollPane.setBounds(124, 131, 595, 100);
panelBook.add(scrollPane);
JTable table_rec = new JTable();
DefaultTableCellRenderer renderer2 = new DefaultTableCellRenderer();
renderer2.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
table_rec.setDefaultRenderer(Object.class, renderer2);
table_rec.setAutoCreateRowSorter(true);
JScrollPane scrollPane_1 = new JScrollPane(table_rec);
scrollPane_1.setBackground(Color.WHITE);
scrollPane_1.setBounds(124, 588, 595, 100);
panelBook.add(scrollPane_1);
JButton btnNewButton_2 = new JButton("刷新");
btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
btnNewButton_2.setBounds(124, 721, 93, 23);
panelBook.add(btnNewButton_2);
ButtonGroup btnGroup2=new ButtonGroup();
JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("已还");
rdbtnNewRadioButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
rdbtnNewRadioButton_2.setBounds(248, 722, 64, 23);
JRadioButton rdbtnNewRadioButton_2_1 = new JRadioButton("未还");
rdbtnNewRadioButton_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
rdbtnNewRadioButton_2_1.setBounds(312, 722, 64, 23);
rdbtnNewRadioButton_2.setOpaque(false);
rdbtnNewRadioButton_2_1.setOpaque(false);
rdbtnNewRadioButton_2.setSelected(true);
btnGroup2.add(rdbtnNewRadioButton_2);
btnGroup2.add(rdbtnNewRadioButton_2_1);
panelBook.add(rdbtnNewRadioButton_2_1);
panelBook.add(rdbtnNewRadioButton_2);
JLabel lblNewLabel_2_4 = new JLabel("按类筛选:");
lblNewLabel_2_4.setFont(new Font("微软雅黑", Font.PLAIN, 17));
lblNewLabel_2_4.setBounds(124, 253, 87, 23);
panelBook.add(lblNewLabel_2_4);
String[] items = {"历史文献","新闻杂志","科学研究","文化文艺","文学名著","论文期刊"};
JComboBox<String> comboBox = new JComboBox(items);
comboBox.setBounds(210, 254, 100, 23);
panelBook.add(comboBox);
JLabel lblNewLabel_2_4_1 = new JLabel("作者检索:");
lblNewLabel_2_4_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
lblNewLabel_2_4_1.setBounds(312, 253, 87, 23);
panelBook.add(lblNewLabel_2_4_1);
textField_5 = new JTextField();
textField_5.setBounds(398, 255, 78, 21);
panelBook.add(textField_5);
textField_5.setColumns(10);
JButton btnNewButton_3 = new JButton("检索");
btnNewButton_3.setFont(new Font("微软雅黑", Font.PLAIN, 11));
btnNewButton_3.setBounds(475, 254, 57, 23);
panelBook.add(btnNewButton_3);
JLabel lblNewLabel_2_4_1_1 = new JLabel("书名检索:");
lblNewLabel_2_4_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
lblNewLabel_2_4_1_1.setBounds(124, 288, 87, 23);
panelBook.add(lblNewLabel_2_4_1_1);
textField_6 = new JTextField();
textField_6.setColumns(10);
textField_6.setBounds(221, 287, 78, 21);
panelBook.add(textField_6);
JButton btnNewButton_3_1 = new JButton("检索");
btnNewButton_3_1.setFont(new Font("微软雅黑", Font.PLAIN, 11));
btnNewButton_3_1.setBounds(298, 286, 57, 23);
panelBook.add(btnNewButton_3_1);
JButton btnNewButton_2_1 = new JButton("显示全部");
btnNewButton_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
btnNewButton_2_1.setBounds(439, 286, 93, 23);
panelBook.add(btnNewButton_2_1);
JLabel lblNewLabel_1_2_2 = new JLabel("图书借阅:");
lblNewLabel_1_2_2.setFont(new Font("微软雅黑", Font.PLAIN, 19));
lblNewLabel_1_2_2.setBounds(33, 321, 98, 35);
panelBook.add(lblNewLabel_1_2_2);
JLabel lblNewLabel_2_4_1_1_1 = new JLabel("输入书名:");
lblNewLabel_2_4_1_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
lblNewLabel_2_4_1_1_1.setBounds(254, 395, 87, 23);
panelBook.add(lblNewLabel_2_4_1_1_1);
textField_7 = new JTextField();
textField_7.setColumns(10);
textField_7.setBounds(385, 396, 78, 21);
panelBook.add(textField_7);
JButton btnNewButton_3_1_1 = new JButton("借阅");
btnNewButton_3_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
btnNewButton_3_1_1.setBounds(330, 449, 69, 25);
panelBook.add(btnNewButton_3_1_1);
tabbedPane.add("账户管理",panelAccount);
panelAccount.setLayout(null);
JLabel lblNewLabel = new JLabel("个人账户管理");
lblNewLabel.setFont(new Font("华文楷体", Font.PLAIN, 28));
lblNewLabel.setBounds(285, 20, 179, 54);
panelAccount.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("个人资料:");
lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 19));
lblNewLabel_1.setBounds(84, 111, 98, 35);
panelAccount.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("姓名:");
lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 17));
lblNewLabel_2.setBounds(217, 165, 52, 23);
panelAccount.add(lblNewLabel_2);
textField = new JTextField();
textField.setBounds(474, 165, 66, 21);
textField.setText(userSelf.getUserName());
panelAccount.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_2_1 = new JLabel("性别:");
lblNewLabel_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
lblNewLabel_2_1.setBounds(217, 272, 52, 23);
panelAccount.add(lblNewLabel_2_1);
JLabel lblNewLabel_2_2 = new JLabel("用户名/电话:");
lblNewLabel_2_2.setFont(new Font("微软雅黑", Font.PLAIN, 17));
lblNewLabel_2_2.setBounds(217, 336, 111, 23);
panelAccount.add(lblNewLabel_2_2);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(409, 340, 131, 21);
textField_1.setText(userSelf.getPhoneNum());//默认显示的值是多少
panelAccount.add(textField_1);
JLabel lblNewLabel_2_3 = new JLabel("邮箱:");
lblNewLabel_2_3.setFont(new Font("微软雅黑", Font.PLAIN, 17));
lblNewLabel_2_3.setBounds(217, 218, 52, 23);
panelAccount.add(lblNewLabel_2_3);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(415, 218, 95, 21);
//这里我们进行一个字符串的分割
String main[]=userSelf.geteMail().split("@");
//然后设置默认值
textField_2.setText(main[0]);
panelAccount.add(textField_2);
//
String[] items2= {"@qq.com","@163.com","@126.com","@gmail.com"};
JComboBox<String> comboBox_1 = new JComboBox<>(items2);
comboBox_1.setBounds(507, 218, 100, 21);
comboBox_1.setSelectedItem('@'+main[1]);//拼接以后作为combox的默认选中项
panelAccount.add(comboBox_1);
JButton btnNewButton = new JButton("保存修改");
btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
btnNewButton.setBounds(317, 398, 93, 23);
panelAccount.add(btnNewButton);
JLabel lblNewLabel_1_1 = new JLabel("修改密码:");
lblNewLabel_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 19));
lblNewLabel_1_1.setBounds(84, 478, 98, 35);
panelAccount.add(lblNewLabel_1_1);
JLabel lblNewLabel_2_2_1 = new JLabel("请输入原密码:");
lblNewLabel_2_2_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
lblNewLabel_2_2_1.setBounds(217, 549, 131, 23);
panelAccount.add(lblNewLabel_2_2_1);
JPasswordField textField_3 = new JPasswordField();
textField_3.setColumns(10);
textField_3.setBounds(409, 550, 131, 21);
panelAccount.add(textField_3);
JLabel lblNewLabel_2_2_1_1 = new JLabel("新密码:");
lblNewLabel_2_2_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
lblNewLabel_2_2_1_1.setBounds(217, 617, 131, 23);
panelAccount.add(lblNewLabel_2_2_1_1);
JPasswordField textField_4 = new JPasswordField();
textField_4.setColumns(10);
textField_4.setBounds(409, 618, 131, 21);
panelAccount.add(textField_4);
JButton btnNewButton_1 = new JButton("提交");
btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
btnNewButton_1.setBounds(317, 684, 93, 23);
panelAccount.add(btnNewButton_1);
JRadioButton rdbtnNewRadioButton = new JRadioButton("男");
rdbtnNewRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
rdbtnNewRadioButton.setOpaque(false);
rdbtnNewRadioButton.setBounds(444, 272, 57, 23);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("女");
rdbtnNewRadioButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
rdbtnNewRadioButton_1.setOpaque(false);
rdbtnNewRadioButton_1.setBounds(503, 272, 52, 23);
ButtonGroup btnGroup=new ButtonGroup();
btnGroup.add(rdbtnNewRadioButton_1);
btnGroup.add(rdbtnNewRadioButton);
if(userSelf.getSex().equals("男"))
{
rdbtnNewRadioButton.setSelected(true);
}else {
rdbtnNewRadioButton_1.setSelected(true);
}
panelAccount.add(rdbtnNewRadioButton_1);
panelAccount.add(rdbtnNewRadioButton);
getContentPane().add(tabbedPane);
//修改密码那个按钮:
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String oldPass = new String(textField_3.getPassword());
String newPass = new String(textField_4.getPassword());
if(oldPass.isEmpty())
{
JOptionPane.showMessageDialog(UserHome.this,"请输入原密码","提示",JOptionPane.WARNING_MESSAGE);
}
else if(newPass.isEmpty())
{
JOptionPane.showMessageDialog(UserHome.this,"请输入新密码","提示",JOptionPane.WARNING_MESSAGE);
}else {
if(oldPass.equals(pass))
{
userServiceimpl.UpdatePass(id, newPass, oldPass);
JOptionPane.showMessageDialog(UserHome.this,"修改成功","提示",JOptionPane.WARNING_MESSAGE);
}else {
JOptionPane.showMessageDialog(UserHome.this,"原密码输入错误","提示",JOptionPane.ERROR_MESSAGE);
}
}
}
});
//修改个人信息的那个按钮
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String uName = textField.getText();
String id_phone = textField_1.getText();
String email = (textField_2.getText())+(comboBox_1.getSelectedItem().toString());
String sex = "";
if(rdbtnNewRadioButton.isSelected())
{
sex="男";
}else {
sex="女";
}
User u = new User(uName, id_phone, pass, sex, id_phone, email);
User testUser = userDaoimpl.getOneByid(id_phone);
if(uName.isEmpty()||id_phone.isEmpty()||email.isEmpty())
{
JOptionPane.showMessageDialog(UserHome.this,"请完整填写信息!","提示",JOptionPane.ERROR_MESSAGE);
}else {
if(testUser==null||id_phone.equals(id))
{
userDaoimpl.update(u);
JOptionPane.showMessageDialog(UserHome.this,"修改成功!","提示",JOptionPane.WARNING_MESSAGE);
}else {
JOptionPane.showMessageDialog(UserHome.this,"该用户名(电话)已存在","提示",JOptionPane.ERROR_MESSAGE);
}
}
}
});
// *******************************************************************
//刷新借书记录的那个表
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
List<Record> rcList = userServiceimpl.searchMyrecords(id);
Vector<String> colName = new Vector<String>();
colName.add("书名");colName.add("借阅人");colName.add("借阅人账号");colName.add("借阅状态");colName.add("借阅日期");
Vector data = new Vector();
int choice = rdbtnNewRadioButton_2.isSelected()? 1 : 2;
if(choice==1)//_2这个是已经还了
{
for(Record r : rcList)
{
Vector row = new Vector();
row.add(r.getBookName());
row.add(r.getUserName());
row.add(r.getUserId());
row.add(r.getType());
row.add(r.getDate());
if(r.getType().equals("已还"))
{
data.add(row);
}
}
}else {
for(Record r : rcList)
{
Vector row = new Vector();
row.add(r.getBookName());
row.add(r.getUserName());
row.add(r.getUserId());
row.add(r.getType());
row.add(r.getDate());
if(r.getType().equals("未还"))
{
data.add(row);
}
}
}
DefaultTableModel model = new DefaultTableModel(data,colName);
table_rec.setModel(model);
}
});
BooksDaoImpl booksDaoImpl2 = new BooksDaoImpl();
RecordDaoimpl recordDaoimpl = new RecordDaoimpl();
//借阅按钮:
btnNewButton_3_1_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//获取当前系统时间:
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
String dataString = dateFormat.format(date);
String bkname=textField_7.getText();
//验证该书是否存在
if(userServiceimpl.getOneByname(bkname)!=null)
{
if(userServiceimpl.getOneByname(bkname).getQuantity()>=0)
{
recordDaoimpl.insertRecord(bkname, userSelf.getUserName(), userSelf.getuId(), "未还", dataString);
booksDaoImpl2.updateBooknum(bkname, booksDaoImpl2.getAbookByname(bkname).getQuantity()-1);//注意一下这个地方
JOptionPane.showMessageDialog(UserHome.this,"借阅成功:请刷新借阅记录","提示",JOptionPane.WARNING_MESSAGE);
}
else {
JOptionPane.showMessageDialog(UserHome.this,"借阅失败:该书库存不足","提示",JOptionPane.ERROR_MESSAGE);
}
}else {
JOptionPane.showMessageDialog(UserHome.this,"该书不存在","提示",JOptionPane.ERROR_MESSAGE);
}
}
});
//显示全部按钮:
btnNewButton_2_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
List<Books> booklist = userServiceimpl.getByclskey("");
Vector<String> colName = new Vector<String>();
colName.add("书名");colName.add("作者");colName.add("类别");
colName.add("剩余(本)");colName.add("位置");
Vector data = new Vector();
for(Books b : booklist) {
Vector row = new Vector();
row.add(b.getBookName());
row.add(b.getWriter());
row.add(b.getBookClass());
row.add(b.getQuantity());
row.add(b.getPosition());
data.add(row);
}
DefaultTableModel model = new DefaultTableModel(data,colName);
table_books.setModel(model);
}
});
BooksDaoImpl booksDaoImpl = new BooksDaoImpl();
//为按书名检索按钮添加事件:
btnNewButton_3_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bknamekey = textField_6.getText();
List<Books> booklist = booksDaoImpl.getBooksBynamekey(bknamekey);
Vector<String> colName = new Vector<String>();
colName.add("书名");colName.add("作者");colName.add("类别");
colName.add("剩余(本)");colName.add("位置");
Vector data = new Vector();
for(Books b : booklist) {
Vector row = new Vector();
row.add(b.getBookName());
row.add(b.getWriter());
row.add(b.getBookClass());
row.add(b.getQuantity());
row.add(b.getPosition());
data.add(row);
}
DefaultTableModel model = new DefaultTableModel(data,colName);
table_books.setModel(model);
}
});
//为下拉选框添加事件
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
String selectedItem=comboBox.getSelectedItem().toString();
List<Books> booklist = userServiceimpl.getByclskey(selectedItem);
Vector<String> colName = new Vector<String>();
colName.add("书名");colName.add("作者");colName.add("类别");
colName.add("剩余(本)");colName.add("位置");
Vector data = new Vector();
for(Books b : booklist) {
Vector row = new Vector();
row.add(b.getBookName());
row.add(b.getWriter());
row.add(b.getBookClass());
row.add(b.getQuantity());
row.add(b.getPosition());
data.add(row);
}
DefaultTableModel model = new DefaultTableModel(data,colName);
table_books.setModel(model);
}
});
//按作者名检索:texteField_5和btnNewButton_3
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String wname = textField_5.getText();
List<Books> booklist = booksDaoImpl.getByWriternamekey(wname);
Vector<String> colName = new Vector<String>();
colName.add("书名");colName.add("作者");colName.add("类别");
colName.add("剩余(本)");colName.add("位置");
Vector data = new Vector();
for(Books b : booklist) {
Vector row = new Vector();
row.add(b.getBookName());
row.add(b.getWriter());
row.add(b.getBookClass());
row.add(b.getQuantity());
row.add(b.getPosition());
data.add(row);
}
DefaultTableModel model = new DefaultTableModel(data,colName);
table_books.setModel(model);
}
});
}
}
Uregister.java
package com.qdu.frames;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.qdu.service.impl.UserServiceimpl;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Uregister extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Uregister frame = new Uregister();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Uregister() {
setTitle("用户注册");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 527, 357);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("用户注册");
lblNewLabel.setFont(new Font("华文楷体", Font.PLAIN, 30));
lblNewLabel.setBounds(191, 10, 127, 47);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("电话/账号:");
lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
lblNewLabel_1.setBounds(92, 75, 94, 28);
panel.add(lblNewLabel_1);
JLabel lblNewLabel_1_1 = new JLabel("姓名:");
lblNewLabel_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
lblNewLabel_1_1.setBounds(92, 113, 57, 28);
panel.add(lblNewLabel_1_1);
JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
lblNewLabel_1_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
lblNewLabel_1_1_1.setBounds(92, 151, 57, 28);
panel.add(lblNewLabel_1_1_1);
String[] items = {"男","女"};
JComboBox<String> comboBox = new JComboBox<>(items);
comboBox.setBounds(205, 155, 52, 23);
panel.add(comboBox);
JLabel lblNewLabel_1_1_1_1 = new JLabel("邮箱:");
lblNewLabel_1_1_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
lblNewLabel_1_1_1_1.setBounds(92, 189, 57, 28);
panel.add(lblNewLabel_1_1_1_1);
textField = new JTextField();
textField.setBounds(205, 81, 152, 21);
panel.add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("注册");
btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
btnNewButton.setBounds(205, 274, 93, 23);
panel.add(btnNewButton);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(205, 119, 152, 21);
panel.add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(205, 195, 127, 21);
panel.add(textField_2);
JLabel lblNewLabel_1_1_1_1_1 = new JLabel("密码:");
lblNewLabel_1_1_1_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
lblNewLabel_1_1_1_1_1.setBounds(92, 225, 57, 28);
panel.add(lblNewLabel_1_1_1_1_1);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(205, 231, 127, 21);
panel.add(textField_3);
String[] mails= {"@qq.com","@163.com","@126.com","@gmail.com"};
JComboBox<String> comboBox_1 = new JComboBox<>(mails);
comboBox_1.setBounds(330, 195, 100, 21);
panel.add(comboBox_1);
UserServiceimpl userServiceimpl=new UserServiceimpl();
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name=textField_1.getText();
String sex=comboBox.getSelectedItem().toString();
String id=textField.getText();
String phone=textField.getText();
String email=textField_2.getText()+(comboBox_1.getSelectedItem().toString());
String pass = textField_3.getText();
if(name.isEmpty()||sex.isEmpty()||id.isEmpty()||phone.isEmpty()||email.isEmpty())
{
JOptionPane.showMessageDialog(Uregister.this,"请完整填写信息","提示",JOptionPane.WARNING_MESSAGE);
}else{
String pass2 = JOptionPane.showInputDialog(Uregister.this,"请再次输入密码");
if(pass2.equals(pass))
{
int a=userServiceimpl.Register(name, id, pass, sex, phone, email);
if(a==-1)
{
JOptionPane.showMessageDialog(Uregister.this,"注册失败:该用户已存在","错误",JOptionPane.ERROR_MESSAGE);
}else {
JOptionPane.showMessageDialog(Uregister.this,"注册成功!","成功",JOptionPane.DEFAULT_OPTION);
}
}else {
JOptionPane.showMessageDialog(Uregister.this,"注册失败:两次输入密码不符","错误",JOptionPane.ERROR_MESSAGE);
}
}
}
});
}
}
6.com.qdu.util
有两个文件:
Database.java将对数据库的基础操作封装到了一个类里面,比如链接数据库的getConnection函数,和负责关闭资源的close函数
dbconfig.properties是一个配置文件,里面用来存放Database.java中一些函数操作所要用到的常量
DataBase.java
package com.qdu.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
/**
** 数据库工具类(utility class):实现打开数据库连接和关闭数据库连接
*
* @author NIIT
*
*/
public class DatabaseUtil {
private static final String DRIVER_CLASS;
private static final String URL;
private static final String USERNAME;
private static final String PASSWORD;
static {
ResourceBundle rb = ResourceBundle.getBundle("com.qdu.util.dbconfig");
DRIVER_CLASS = rb.getString("jdbc.driver");
URL = rb.getString("jdbc.url");
USERNAME = rb.getString("jdbc.username");
PASSWORD = rb.getString("jdbc.password");
}
/**
** 负责打开一个数据库连接,并返回打开的连接
*
* @return 一个Connection对象,表示和数据库的一个连接
*/
public static Connection getConnection() {
Connection con = null;
try {
// 1. 加载驱动类
Class.forName(DRIVER_CLASS);
// 2. 获取数据库连接
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/**
** 关闭要关闭的资源
*
* @param rs 要关闭的结果集
* @param stmt 要关闭的语句对象
* @param con 要关闭的连接
*/
public static void close(ResultSet rs, Statement stmt, Connection con) {
try {
if (null != rs)
rs.close();
if (null != stmt)
stmt.close();
if (null != con)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
dbconfig.properties
# data source configuration
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/Project?serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=root
7.MySQL脚本
CREATE DATABASE project character set utf8 collate utf8_general_ci;
USE project;
create table managers
(
Mname varchar(30) not null,
Mid varchar(30) primary key,
Mpass varchar(30) not null
);
INSERT INTO `managers` VALUES ('程树显','0001','0001');
INSERT INTO `managers` VALUES ('王新宇','0002','0002');
INSERT INTO `managers` VALUES ('张立','0003','0003');
INSERT INTO `managers` VALUES ('陈家和','0004','0004');
select * from managers;
create table users
(
Uid varchar(30) primary key,
Uname varchar(30) not null,
Upass varchar(30) not null,
Usex char(1) not null,
Uphone varchar(30) not null,
Umail varchar(30)
);
INSERT INTO `users` VALUES ('13100000001','胡英俊','1111','男','13100000001','0001@qq.com');
INSERT INTO `users` VALUES ('13100000002','张小丽','1111','女','13100000002','0002@163.com');
INSERT INTO `users` VALUES ('13100000003','胡图图','1111','男','13100000003','0003@qq.com');
INSERT INTO `users` VALUES ('13100000004','壮壮妈','1111','女','13100000004','0004@126.com');
INSERT INTO `users` VALUES ('13100000005','牛爷爷','1111','男','13100000005','0005@gmail.com');
select * from users;
create table books
(
Bname varchar(30) primary key,
Bwriter varchar(30) not null,
Bclass varchar(30) not null,
Bquantity int not null,
Bposition varchar(30) not null
);
-- "历史文献","新闻杂志","科学研究","文化文艺","文学名著","论文期刊"
-- "一楼西区","一楼东区","一楼南区","一楼北区","二楼西区","二楼东区","二楼南区","二楼北区"
insert into `books` values("三国演义","罗贯中","文学名著",100,"二楼西区");
insert into `books` values("四国演义","罗贯中","文学名著",100,"二楼东区");
insert into `books` values("水浒传","施耐庵","文化文艺",100,"一楼西区");
insert into `books` values("火浒传","施耐庵","文化文艺",100,"一楼西区");
insert into `books` values("科学","杂志社","论文期刊",100,"二楼北区");
insert into `books` values("自然","杂志社","论文期刊",100,"二楼南区");
insert into `books` values("人民日报","新闻社","新闻杂志",100,"二楼南区");
insert into `books` values("读者","新闻社","新闻杂志",100,"一楼东区");
insert into `books` values("史记","司马迁","历史文献",100,"二楼西区");
insert into `books` values("资治通鉴","司马光","历史文献",100,"二楼西区");
insert into `books` values("相对论","爱因斯坦","科学研究",100,"一楼南区");
select * from books;
create table record
(
bookname varchar(30) not null,
username varchar(30) not null,
userid varchar(30) not null,
type varchar(10) not null,
data varchar(20) not null,
primary key(bookname,username,userid,type,data)
);
select * from record;
INSERT INTO `record` VALUES ('三国演义','胡英俊','13100000001','未还','2022/12/12');
INSERT INTO `record` VALUES ('四国演义','胡图图','13100000003','已还','2022/12/12');
INSERT INTO `record` VALUES ('火浒传','胡英俊','13100000001','未还','2022/12/12');
INSERT INTO `record` VALUES ('史记','胡图图','13100000003','已还','2022/12/12');
INSERT INTO `record` VALUES ('史记','胡英俊','13100000001','未还','2022/12/12');
INSERT INTO `record` VALUES ('相对论','胡图图','13100000003','已还','2022/12/12');