Java新手,有错误多多包涵
导入JDBC依赖包,CSDN有很多教程
创建7个类,GUILogin,GUIMenu,GUIListBook,GUISearchBook,GUIAddBook,GUIUpdate,GUIUpdatePassword
数据库可以在本机创建,也可以在服务器创建,我自己是把数据库放到服务器上了
GUILogin类,测试与服务器的连接以及验证管理员密码
import java.awt.Font;
import java.io.IOException;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.WindowConstants;
public class GUILogin
{
public static void main(String[] args)
{
//数据库放在本地将这块正在连接服务器的提示窗口删除掉
JFrame tw = new JFrame();
tw.setSize(300,70);
tw.setResizable(false);
tw.setUndecorated(true);
tw.setLocationRelativeTo(null);
tw.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel tw1 = new JPanel(null);
JLabel twtext = new JLabel();
twtext.setText("正在连接服务器......");
twtext.setSize(180,50);
twtext.setFont(new Font("Serif",Font.BOLD,15));
twtext.setLocation(80,10);
tw1.add(twtext);
tw.setContentPane(tw1);
tw.setVisible(true);
if (!isServerReachable("这里填写你的服务器地址")) //如果想把数据库放在本地可以将这块与服务器连通性的判断删除掉
{
tw.dispose();
JOptionPane.showMessageDialog(null, "无法连接到服务器,请检查网络连接", "网络连接错误", JOptionPane.ERROR_MESSAGE);
return;
}
else
{
tw.dispose();//数据库放在本地将这句话删除掉
//创建管理员密码输入窗口顶层容器
JFrame pw = new JFrame("图书管理系统登录页面");
pw.setSize(480,300);
pw.setResizable(false);//禁止用户进行窗口大小调整
pw.setLocationRelativeTo(null);// 把窗口位置设置到屏幕中心
pw.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// 当点击窗口的关闭按钮时退出程序
//创建中间容器
JPanel panel = new JPanel(null); //创建面板容器使用绝对布局
//创建标签
JLabel text = new JLabel();
text.setText("请输入管理员密码");
text.setSize(300,80);
text.setFont(new Font("Serif",Font.BOLD,30));
text.setLocation(105,20);
panel.add(text);
//创建输入登录密码框组件
JPasswordField login = new JPasswordField();
login.setFont(new Font("Serif",Font.BOLD,30));
login.setSize(300,40);
login.setLocation(80, 100);
panel.add(login);
//创建登录按钮组件
JButton lgbt = new JButton("登录");
lgbt.setFont(new Font("Serif",Font.BOLD,30));//按钮字体大小
lgbt.setSize(150,40);//设置按钮大小
lgbt.setLocation(155, 180);//设置按钮坐标
panel.add(lgbt);
//登录按钮的 ActionListener
//Lambda 表达式
lgbt.addActionListener(e -> {
//获取输入的密码
char[] VerificationPassword = login.getPassword();
String password = new String(VerificationPassword);
//连接数据库
String connectSql = "jdbc:mysql://这里填你数据库的地址:3306/bookmanage";
String sqlUser = "数据库用户名";
String sqlPassword = "数据库密码";
Connection con = null;
PreparedStatement psm = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection(connectSql, sqlUser, sqlPassword);
//准备查询语句
//关键字BINARY考虑大小写
psm = con.prepareStatement("SELECT pw FROM password WHERE BINARY pw = ?");
psm.setString(1, password);
// 执行查询语句
rs = psm.executeQuery();
// 检查结果集是否有记录
if (rs.next())
{
JOptionPane.showMessageDialog(pw, "登录成功,欢迎使用图书管理系统", "登录成功", JOptionPane.INFORMATION_MESSAGE);
pw.dispose();//关闭登录页面
GUIMenu menu = new GUIMenu();
menu.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(pw, "密码错误", "密码错误", JOptionPane.ERROR_MESSAGE);
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
});
//把面板容器作为窗口的内容面板设置到窗口
pw.setContentPane(panel);
//显示窗口
pw.setVisible(true);
}
}
//这里是ping服务器的方法数据库在本地把这个方法删除掉
private static boolean isServerReachable(String serverAddress)
{
try
{
InetAddress server = InetAddress.getByName(serverAddress);
return server.isReachable(8000); // 8秒超时
}
catch (IOException e)
{
return false;
}
}
}
GUIMenu类,这里是各种功能的入口
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class GUIMenu
{
public void setVisible(boolean b)
{
JFrame Menu = new JFrame("图书管理系统功能菜单 by:cjt");
Menu.setSize(500,600);
Menu.setResizable(false);
Menu.setLocationRelativeTo(null);
Menu.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//点击关闭按钮终止程序
JPanel panel = new JPanel(null);
JLabel text = new JLabel();
text.setText("欢迎使用图书管理系统");
text.setSize(400,30);
text.setFont(new Font("Serif",Font.BOLD,30));
text.setLocation(90,20);
panel.add(text);
JButton Look = new JButton("查看所有图书信息");
Look.setFont(new Font(null,Font.PLAIN,30));//按钮字体大小
Look.setSize(300,40);//设置按钮大小
Look.setLocation(90,100);//设置按钮坐标
panel.add(Look);
JButton Search = new JButton("查询图书信息");
Search.setFont(new Font(null,Font.PLAIN,30));//按钮字体大小
Search.setSize(300,40);//设置按钮大小
Search.setLocation(90,200);//设置按钮坐标
panel.add(Search);
JButton Add = new JButton("添加图书");
Add.setFont(new Font(null,Font.PLAIN,30));//按钮字体大小
Add.setSize(300,40);//设置按钮大小
Add.setLocation(90,300);//设置按钮坐标
panel.add(Add);
JButton Update = new JButton("修改借阅状态");
Update.setFont(new Font(null,Font.PLAIN,30));//按钮字体大小
Update.setSize(300,40);//设置按钮大小
Update.setLocation(90,400);//设置按钮坐标
panel.add(Update);
JButton UpdatePassword = new JButton("修改管理员密码");
UpdatePassword.setFont(new Font(null,Font.PLAIN,30));//按钮字体大小
UpdatePassword.setSize(300,40);//设置按钮大小
UpdatePassword.setLocation(90,500);//设置按钮坐标
panel.add(UpdatePassword);
//点击查看所有书本信息
Look.addActionListener(e -> {
GUIListBook listbook = new GUIListBook();
listbook.setVisible(true);
});
//点击查询,修改,删除图书信息
Search.addActionListener(e -> {
GUISearchBook searchbook = new GUISearchBook();
searchbook.setVisible(true);
});
//点击添加图书
Add.addActionListener(e ->{
GUIAddBook addbook = new GUIAddBook();
addbook.setVisible(true);
});
//点击修改借阅状态
Update.addActionListener(e ->{
GUIUpdate update = new GUIUpdate();
update.setVisible(true);
});
//点击修改管理员密码
UpdatePassword.addActionListener(e ->{
GUIUpdate updatePassword = new GUIUpdatePassword();
updatePassword.setVisible(true);
});
Menu.setContentPane(panel);
Menu.setVisible(true);
}
}
GUIListBook类,列出所有书籍信息
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class GUIListBook
{
public void setVisible(boolean b)
{
String connectSql = "jdbc:mysql://这里填你数据库的地址:3306/bookmanage";
String sqlUser = "数据库用户名";
String sqlPassword = "数据库密码";
Connection con=null;
PreparedStatement psm=null;
ResultSet rs=null;
try
{
//连接MySQL
con = DriverManager.getConnection(connectSql, sqlUser, sqlPassword);
//执行MySQL语句
psm = con.prepareStatement("SELECT * FROM bookdata");
rs = psm.executeQuery();
//创建表格模型
DefaultTableModel tableModel = new DefaultTableModel();
//设置列名
ResultSetMetaData Data = rs.getMetaData();
//列数量赋值给columnCount
int columnCount = Data.getColumnCount();
//for循环向表格模型tableModel中添加列
for (int column = 1; column <= columnCount; column++)
{
tableModel.addColumn(Data.getColumnLabel(column));
}
//将数据填充到表格模型中
while (rs.next())
{
Object[] row = new Object[columnCount];
for (int column = 1; column <= columnCount; column++)
{
row[column - 1] = rs.getObject(column);
}
tableModel.addRow(row);
}
//获取图书数量
int bookCount = tableModel.getRowCount();
//创建顶层容器
JFrame listbook = new JFrame("所有图书信息 (共有" + bookCount + "本图书)");
listbook.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//JFrame.DISPOSE_ON_CLOSE:释放窗口资源而不终止程序。
//创建表格对象
JTable table = new JTable(tableModel);
//创建滚动面板,并将表格添加到其中
JScrollPane scrollPane = new JScrollPane(table);
//将滚动面板添加到其中
listbook.getContentPane().add(scrollPane);
listbook.setSize(1500, 800);
listbook.setLocationRelativeTo(null);
listbook.setVisible(true);
}
catch(SQLException e)
{
//打印异常堆栈跟踪
e.printStackTrace();
}
}
}
GUISearchBook类,这个类实现了书籍的查找,删除以及修改书籍信息
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class GUISearchBook
{
public void setVisible(boolean b)
{
JFrame Search = new JFrame("图书查询");
Search.setSize(480,300);
Search.setResizable(false);
Search.setLocationRelativeTo(null);
Search.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(null);
JLabel text = new JLabel();
text.setText("请输入图书ISBN");
text.setSize(300,80);
text.setFont(new Font("Serif",Font.BOLD,30));
text.setLocation(120,10);
panel.add(text);
JTextField ISBN = new JTextField();
ISBN.setFont(new Font("Serif",Font.BOLD,30));
ISBN.setSize(300,40);
ISBN.setLocation(80, 90);
panel.add(ISBN);
JButton SearchBook = new JButton("查询");
SearchBook.setFont(new Font("Serif",Font.BOLD,30));//按钮字体大小
SearchBook.setSize(150,40);//设置按钮大小
SearchBook.setLocation(155, 180);//设置按钮坐标
panel.add(SearchBook);
SearchBook.addActionListener(e -> {
String isbn = ISBN.getText();
if(!isbn.isEmpty())
{
try(Connection con = DriverManager.getConnection("jdbc:mysql://这里填你数据库的地址:3306/bookmanage", "数据库用户名", "数据库密码"))
{
// 执行查询语句
String sql = "SELECT * FROM bookdata WHERE ISBN = ?";
try (PreparedStatement pstmt = con.prepareStatement(sql))
{
pstmt.setString(1, isbn);
try (ResultSet rs = pstmt.executeQuery())
{
if (rs.next())
{
// 获取图书信息
String bookName = rs.getString("书名");
String author = rs.getString("作者");
String publisher = rs.getString("出版社");
String ISBNResult = rs.getString("ISBN");
String state = rs.getString("状态");
String StudentName = rs.getString("学生姓名");
String StudentID = rs.getString("学号");
String Time = rs.getString("借出、归还时间");
// 创建新页面
JFrame bookInfoFrame = new JFrame("图书信息");
bookInfoFrame.setSize(480, 380);
bookInfoFrame.setResizable(false);
bookInfoFrame.setLocationRelativeTo(null);
bookInfoFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel bookInfoPanel = new JPanel(null);
JLabel bookNameLabel = new JLabel("书名:" + bookName);
bookNameLabel.setFont(new Font("微软雅黑",Font.BOLD,20));
bookNameLabel.setSize(300,80);
bookNameLabel.setLocation(50,5);
bookInfoPanel.add(bookNameLabel);
JLabel authorLabel = new JLabel("作者:" + author);
authorLabel.setFont(new Font("微软雅黑",Font.BOLD,20));
authorLabel.setSize(300,80);
authorLabel.setLocation(50,35);
bookInfoPanel.add(authorLabel);
JLabel publisherLabel = new JLabel("出版社:" + publisher);
publisherLabel.setFont(new Font("微软雅黑",Font.BOLD,20));
publisherLabel.setSize(300,80);
publisherLabel.setLocation(50,65);
bookInfoPanel.add(publisherLabel);
JLabel ISBNLabel = new JLabel("ISBN:" + ISBNResult);
ISBNLabel.setFont(new Font("微软雅黑",Font.BOLD,20));
ISBNLabel.setSize(300,80);
ISBNLabel.setLocation(50,95);
bookInfoPanel.add(ISBNLabel);
JLabel stateLabel = new JLabel("状态:" + state);
stateLabel.setFont(new Font("微软雅黑",Font.BOLD,20));
stateLabel.setSize(300,80);
stateLabel.setLocation(50,125);
bookInfoPanel.add(stateLabel);
JLabel Name = new JLabel("学生姓名:" + StudentName);
Name.setFont(new Font("微软雅黑",Font.BOLD,20));
Name.setSize(300,80);
Name.setLocation(50,155);
bookInfoPanel.add(Name);
JLabel ID = new JLabel("学号:" + StudentID);
ID.setFont(new Font("微软雅黑",Font.BOLD,20));
ID.setSize(300,80);
ID.setLocation(50,180);
bookInfoPanel.add(ID);
JLabel time = new JLabel("借出、归还时间:" + Time);
time.setFont(new Font("微软雅黑",Font.BOLD,20));
time.setSize(400,80);
time.setLocation(50,205);
bookInfoPanel.add(time);
JButton delBook = new JButton("删除本书");
delBook.setFont(new Font("Serif",Font.BOLD,20));
delBook.setSize(150,50);
delBook.setLocation(50,280);
bookInfoPanel.add(delBook);
JButton UpdateBook = new JButton("修改信息");
UpdateBook.setFont(new Font("Serif",Font.BOLD,20));
UpdateBook.setSize(150,50);
UpdateBook.setLocation(260,280);
bookInfoPanel.add(UpdateBook);
delBook.addActionListener(f -> {
JFrame tw = new JFrame("警告");
tw.setSize(300,180);
tw.setResizable(false);//禁止用户进行窗口大小调整
tw.setLocationRelativeTo(null);// 把窗口位置设置到屏幕中心
tw.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel twpanel = new JPanel(null);
JLabel waring = new JLabel("是否确定要删除这本书?");
waring.setFont(new Font("Serif",Font.BOLD,20));
waring.setSize(300,80);
waring.setLocation(35,5);
twpanel.add(waring);
JButton DelBook = new JButton("是");
DelBook.setFont(new Font("Serif",Font.BOLD,15));
DelBook.setSize(80,30);
DelBook.setLocation(25,100);
twpanel.add(DelBook);
JButton nodelBook = new JButton("否");
nodelBook.setFont(new Font("Serif",Font.BOLD,15));
nodelBook.setSize(80,30);
nodelBook.setLocation(180,100);
twpanel.add(nodelBook);
DelBook.addActionListener(g -> {
try (Connection con2 = DriverManager.getConnection("jdbc:mysql://这里填你数据库的地址:3306/bookmanage", "数据库用户名", "数据库密码"))
{
String deleteQuery = "DELETE FROM bookdata WHERE ISBN = ?";
try (PreparedStatement psm = con2.prepareStatement(deleteQuery))
{
psm.setString(1, ISBNResult);
psm.executeUpdate();
JOptionPane.showMessageDialog(null, "图书删除成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
tw.dispose(); // 关闭警告窗口
bookInfoFrame.dispose(); // 关闭图书信息窗口
});
nodelBook.addActionListener(g ->
{
tw.dispose(); // 关闭警告窗口
});
tw.setContentPane(twpanel);
tw.setVisible(true);
});
UpdateBook.addActionListener(g ->{
JFrame tw = new JFrame("请输入新的书本信息");
tw.setSize(480,300);
tw.setResizable(false);//禁止用户进行窗口大小调整
tw.setLocationRelativeTo(null);// 把窗口位置设置到屏幕中心
tw.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel twpanel = new JPanel(null);
JLabel Bookname = new JLabel("书名:");
Bookname.setFont(new Font("Serif",Font.BOLD,20));
Bookname.setSize(300,80);
Bookname.setLocation(50,10);
twpanel.add(Bookname);
JTextField BooknameText = new JTextField();
BooknameText.setFont(new Font("Serif",Font.BOLD,20));
BooknameText.setSize(250,25);
BooknameText.setLocation(150, 40);
twpanel.add(BooknameText);
JLabel Author = new JLabel("作者:");
Author.setFont(new Font("Serif",Font.BOLD,20));
Author.setSize(300,80);
Author.setLocation(50,40);
twpanel.add(Author);
JTextField AuthorText = new JTextField();
AuthorText.setFont(new Font("Serif",Font.BOLD,20));
AuthorText.setSize(250,25);
AuthorText.setLocation(150, 70);
twpanel.add(AuthorText);
JLabel Publisher = new JLabel("出版社:");
Publisher.setFont(new Font("Serif",Font.BOLD,20));
Publisher.setSize(300,80);
Publisher.setLocation(50,70);
twpanel.add(Publisher);
JTextField PublisherText = new JTextField();
PublisherText.setFont(new Font("Serif",Font.BOLD,20));
PublisherText.setSize(250,25);
PublisherText.setLocation(150, 100);
twpanel.add(PublisherText);
JLabel Isbn = new JLabel("ISBN:");
Isbn.setFont(new Font("Serif",Font.BOLD,20));
Isbn.setSize(300,80);
Isbn.setLocation(50,100);
twpanel.add(Isbn);
JTextField IsbnText = new JTextField();
IsbnText.setFont(new Font("Serif",Font.BOLD,20));
IsbnText.setSize(250,25);
IsbnText.setLocation(150, 130);
twpanel.add(IsbnText);
JButton OK = new JButton("确定修改");
OK.setFont(new Font("Serif",Font.BOLD,20));
OK.setSize(150,50);
OK.setLocation(160,200);
twpanel.add(OK);
//将信息预填写在文本框中
BooknameText.setText(bookName);
AuthorText.setText(author);
PublisherText.setText(publisher);
IsbnText.setText(ISBNResult);
OK.addActionListener(z ->{
String connectSql = "jdbc:mysql://这里填你数据库的地址:3306/bookmanage";
String sqlUser = "用户名";
String sqlPassword = "密码";
//连接数据库
Connection con3=null;
PreparedStatement psm3=null;
String BookNameText = BooknameText.getText();
String AuthoRText = AuthorText.getText();
String PublisheRText = PublisherText.getText();
String IsbNText = IsbnText.getText();
try
{
con3 = DriverManager.getConnection(connectSql, sqlUser, sqlPassword);
psm3 = con3.prepareStatement("UPDATE bookdata SET 书名 = ?, 作者 = ?, 出版社 = ?, ISBN = ? WHERE ISBN = ?");
psm3.setString(1, BookNameText);
psm3.setString(2, AuthoRText);
psm3.setString(3, PublisheRText);
psm3.setString(4, IsbNText);
psm3.setString(5, isbn);
psm3.executeUpdate();
JOptionPane.showMessageDialog(tw, "书籍信息修改成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
tw.dispose();
}
catch (SQLException h)
{
h.printStackTrace();
}
});
tw.setContentPane(twpanel);
tw.setVisible(true);
});
bookInfoFrame.setContentPane(bookInfoPanel);
bookInfoFrame.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null, "该图书不存在!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
});
Search.setContentPane(panel);
Search.setVisible(true);
}
}
GUIAddBook类,这个类实现了书籍的添加
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class GUIAddBook
{
public void setVisible(boolean b)
{
JFrame AddBook = new JFrame("请输入图书信息");
AddBook.setSize(480,300);
AddBook.setResizable(false);//禁止用户进行窗口大小调整
AddBook.setLocationRelativeTo(null);// 把窗口位置设置到屏幕中心
AddBook.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(null);
JLabel Bookname = new JLabel("书名:");
Bookname.setFont(new Font("Serif",Font.BOLD,20));
Bookname.setSize(300,80);
Bookname.setLocation(50,10);
panel.add(Bookname);
JTextField BooknameText = new JTextField();
BooknameText.setFont(new Font("Serif",Font.BOLD,20));
BooknameText.setSize(250,25);
BooknameText.setLocation(150, 40);
panel.add(BooknameText);
JLabel Author = new JLabel("作者:");
Author.setFont(new Font("Serif",Font.BOLD,20));
Author.setSize(300,80);
Author.setLocation(50,40);
panel.add(Author);
JTextField AuthorText = new JTextField();
AuthorText.setFont(new Font("Serif",Font.BOLD,20));
AuthorText.setSize(250,25);
AuthorText.setLocation(150, 70);
panel.add(AuthorText);
JLabel Publisher = new JLabel("出版社:");
Publisher.setFont(new Font("Serif",Font.BOLD,20));
Publisher.setSize(300,80);
Publisher.setLocation(50,70);
panel.add(Publisher);
JTextField PublisherText = new JTextField();
PublisherText.setFont(new Font("Serif",Font.BOLD,20));
PublisherText.setSize(250,25);
PublisherText.setLocation(150, 100);
panel.add(PublisherText);
JLabel Isbn = new JLabel("ISBN:");
Isbn.setFont(new Font("Serif",Font.BOLD,20));
Isbn.setSize(300,80);
Isbn.setLocation(50,100);
panel.add(Isbn);
JTextField IsbnText = new JTextField();
IsbnText.setFont(new Font("Serif",Font.BOLD,20));
IsbnText.setSize(250,25);
IsbnText.setLocation(150, 130);
panel.add(IsbnText);
JButton Add = new JButton("添加");
Add.setFont(new Font("Serif",Font.BOLD,20));
Add.setSize(150,50);
Add.setLocation(160,200);
panel.add(Add);
Add.addActionListener(e ->{
String connectSql = "jdbc:mysql://数据库地址:3306/bookmanage";
String sqlUser = "数据库用户名";
String sqlPassword = "数据库密码";
Connection con=null;
PreparedStatement psm=null;
String BookNameText = BooknameText.getText();
String AuthoRText = AuthorText.getText();
String PublisheRText = PublisherText.getText();
String IsbNText = IsbnText.getText();
try
{
con = DriverManager.getConnection(connectSql, sqlUser, sqlPassword);
PreparedStatement checkPsm = con.prepareStatement("SELECT COUNT(*) FROM bookdata WHERE ISBN = ?");
checkPsm.setString(1, IsbNText);
ResultSet resultSet = checkPsm.executeQuery();
resultSet.next();
int count = resultSet.getInt(1);
if(count > 0)
{
JOptionPane.showMessageDialog(null, "请勿添加重复书籍!", "错误", JOptionPane.ERROR_MESSAGE);
}
else
{
psm = con.prepareStatement("INSERT INTO bookdata (书名, 作者, 出版社, ISBN, 状态, 学生姓名, 学号, 借出、归还时间) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
psm.setString(1, BookNameText);
psm.setString(2, AuthoRText);
psm.setString(3, PublisheRText);
psm.setString(4, IsbNText);
psm.setString(5, "入库");
psm.setString(6, "");
psm.setString(7, "");
psm.setString(8, "");
psm.executeUpdate();
JOptionPane.showMessageDialog(null, "书籍添加成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
AddBook.dispose();
}
}
catch (SQLException h)
{
h.printStackTrace();
}
});
AddBook.setContentPane(panel);
AddBook.setVisible(true);
}
}
GUIUpdate类,这个类实现了修改书籍借阅信息以及借书还书的时间,还书只需要填写书籍ISBN,借书需要填写书籍ISBN,借书人姓名,借书人学号。
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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.JTextField;
import javax.swing.WindowConstants;
public class GUIUpdate
{
public void setVisible(boolean b)
{
// 获取当前时间
LocalDateTime currentTime = LocalDateTime.now();
// 定义时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 格式化当前时间为字符串
String Time = currentTime.format(formatter);
JFrame Update = new JFrame("借阅状态修改");
Update.setSize(480,300);
Update.setResizable(false);
Update.setLocationRelativeTo(null);
Update.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(null);
JLabel ISBN = new JLabel("ISBN:");
ISBN.setFont(new Font("Serif",Font.BOLD,20));
ISBN.setSize(300,80);
ISBN.setLocation(50,10);
panel.add(ISBN);
JTextField ISBNText = new JTextField();
ISBNText.setFont(new Font("Serif",Font.BOLD,20));
ISBNText.setSize(250,25);
ISBNText.setLocation(150, 40);
panel.add(ISBNText);
JLabel Name = new JLabel("姓名:");
Name.setFont(new Font("Serif",Font.BOLD,20));
Name.setSize(300,80);
Name.setLocation(50,40);
panel.add(Name);
JTextField NameText = new JTextField();
NameText.setFont(new Font("Serif",Font.BOLD,20));
NameText.setSize(250,25);
NameText.setLocation(150, 70);
panel.add(NameText);
JLabel StudentID = new JLabel("学号:");
StudentID.setFont(new Font("Serif",Font.BOLD,20));
StudentID.setSize(300,80);
StudentID.setLocation(50,70);
panel.add(StudentID);
JTextField StudentIDText = new JTextField();
StudentIDText.setFont(new Font("Serif",Font.BOLD,20));
StudentIDText.setSize(250,25);
StudentIDText.setLocation(150, 100);
panel.add(StudentIDText);
JLabel State = new JLabel("状态:");
State.setFont(new Font("Serif",Font.BOLD,20));
State.setSize(300,80);
State.setLocation(50,100);
panel.add(State);
String[] statusOptions = {"入库", "借出"}; //定义下拉框的选项
JComboBox<String> StateComboBox = new JComboBox<>(statusOptions);
StateComboBox.setFont(new Font("Serif", Font.BOLD, 20));
StateComboBox.setSize(250, 25);
StateComboBox.setLocation(150, 130);
panel.add(StateComboBox);
JButton UpdateBook = new JButton("确定");
UpdateBook.setFont(new Font("Serif",Font.BOLD,20));
UpdateBook.setSize(150,50);
UpdateBook.setLocation(160,180);
panel.add(UpdateBook);
UpdateBook.addActionListener(e ->{
String connectSql = "jdbc:mysql://数据库地址:3306/bookmanage";
String sqlUser = "用户名";
String sqlPassword = "密码";
Connection con = null;
PreparedStatement psm = null;
try
{
String isbn = ISBNText.getText();
String studentName = NameText.getText();
String studentId = StudentIDText.getText();
String newState = (String) StateComboBox.getSelectedItem(); // 获取选择的状态值
//根据ISBN查询图书
con = DriverManager.getConnection(connectSql, sqlUser, sqlPassword);
PreparedStatement queryPsm = con.prepareStatement("SELECT * FROM bookdata WHERE ISBN = ?");
queryPsm.setString(1, isbn);
ResultSet resultSet = queryPsm.executeQuery();
if (resultSet.next())
{
//图书存在,更新图书状态
psm = con.prepareStatement("UPDATE bookdata SET 状态 = ?, 学生姓名 = ?, 学号 = ?, 借出、归还时间 = ? WHERE ISBN = ?");
psm.setString(1, newState);
//根据状态选择更新姓名和学号字段
if (newState.equals("入库"))
{
psm.setString(2, "");
psm.setString(3, "");
psm.setString(4, Time);
}
else
{
//验证学生姓名和学号是否为空
if (studentName.isEmpty() || studentId.isEmpty())
{
JOptionPane.showMessageDialog(null, "请输入借书人姓名和学号!", "错误", JOptionPane.ERROR_MESSAGE);
return; // 终止更新操作
}
psm.setString(2, studentName);
psm.setString(3, studentId);
psm.setString(4, Time);
}
psm.setString(5, isbn);
psm.executeUpdate();
JOptionPane.showMessageDialog(null, "状态修改成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "图书不存在!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
});
Update.setContentPane(panel);
Update.setVisible(true);
}
}
GUIUpdatePassword类,这个类实现了管理员密码的修改,需要二次确认密码才可以修改。
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.WindowConstants;
public class GUIUpdatePassword extends GUIUpdate
{
public void setVisible(boolean b)
{
JFrame UpdatePassword = new JFrame("修改密码");
UpdatePassword.setSize(480,300);
UpdatePassword.setResizable(false);
UpdatePassword.setLocationRelativeTo(null);
UpdatePassword.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(null);
JLabel PW1 = new JLabel("密码:");
PW1.setFont(new Font("Serif",Font.BOLD,20));
PW1.setSize(300,80);
PW1.setLocation(50,10);
panel.add(PW1);
JPasswordField PW1Text = new JPasswordField();
PW1Text.setFont(new Font("Serif",Font.BOLD,20));
PW1Text.setSize(250,25);
PW1Text.setLocation(150, 40);
panel.add(PW1Text);
JLabel PW2 = new JLabel("确认密码:");
PW2.setFont(new Font("Serif",Font.BOLD,20));
PW2.setSize(300,80);
PW2.setLocation(50,70);
panel.add(PW2);
JPasswordField PW2Text = new JPasswordField();
PW2Text.setFont(new Font("Serif",Font.BOLD,20));
PW2Text.setSize(250,25);
PW2Text.setLocation(150,100);
panel.add(PW2Text);
JButton Update = new JButton("确认修改");
Update.setFont(new Font("Serif",Font.BOLD,30));//按钮字体大小
Update.setSize(200,40);//设置按钮大小
Update.setLocation(130, 180);//设置按钮坐标
panel.add(Update);
Update.addActionListener(e ->{
//获取输入的密码
char[] password1 = PW1Text.getPassword();
char[] password2 = PW2Text.getPassword();
String pw1 = new String(password1);
String pw2 = new String(password2);
//检查密码长度是否满足要求
if (pw1.length() < 5 || pw2.length() < 5)
{
JOptionPane.showMessageDialog(UpdatePassword, "密码长度不能小于5", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
else if (pw1.equals(pw2)) //检查密码是否一致
{
String connectSql = "jdbc:mysql://数据库地址:3306/bookmanage";
String sqlUser = "用户名";
String sqlPassword = "密码";
Connection con = null;
PreparedStatement psm = null;
try
{
con = DriverManager.getConnection(connectSql, sqlUser, sqlPassword);
psm = con.prepareStatement("UPDATE password SET pw = ?");
psm.setString(1, pw2);
psm.executeUpdate();
JOptionPane.showMessageDialog(UpdatePassword, "密码修改成功", "成功", JOptionPane.INFORMATION_MESSAGE);
UpdatePassword.dispose();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
else
{
//密码不一致,提示用户
JOptionPane.showMessageDialog(UpdatePassword, "两次输入的密码不一致", "错误", JOptionPane.ERROR_MESSAGE);
}
});
UpdatePassword.setContentPane(panel);
UpdatePassword.setVisible(true);
}
}
MySQL数据库设置
新建数据库bookmanage
在bookmanage中新建表bookdata以及password
bookdata这样设计
password这样设计