JAVA图书管理系统登录、注册、添加图书、删除图书信息连接MySQL创建GUI可视化界面的代码
时间: 2023-08-04 13:03:25 浏览: 105
java swing图书管理系统(eclipse+MySQL)
5星 · 资源好评率100%
以下是一个简单的Java图书管理系统的登录、注册、添加图书、删除图书信息的MySQL代码示例,并且使用Java Swing创建了可视化界面。
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class BookManagementSystem extends JFrame implements ActionListener {
private JTextField usernameField, titleField, authorField, isbnField, yearField;
private JPasswordField passwordField;
private JButton loginButton, registerButton, addButton, deleteButton;
private JTextArea messageArea;
public BookManagementSystem() {
super("Java图书管理系统");
// 登录界面
JPanel loginPanel = new JPanel(new GridLayout(3, 2));
loginPanel.add(new JLabel("用户名:"));
usernameField = new JTextField();
loginPanel.add(usernameField);
loginPanel.add(new JLabel("密码:"));
passwordField = new JPasswordField();
loginPanel.add(passwordField);
loginButton = new JButton("登录");
loginButton.addActionListener(this);
loginPanel.add(loginButton);
registerButton = new JButton("注册");
registerButton.addActionListener(this);
loginPanel.add(registerButton);
add(loginPanel, BorderLayout.NORTH);
// 添加图书界面
JPanel addPanel = new JPanel(new GridLayout(5, 2));
addPanel.add(new JLabel("书名:"));
titleField = new JTextField();
addPanel.add(titleField);
addPanel.add(new JLabel("作者:"));
authorField = new JTextField();
addPanel.add(authorField);
addPanel.add(new JLabel("ISBN:"));
isbnField = new JTextField();
addPanel.add(isbnField);
addPanel.add(new JLabel("年份:"));
yearField = new JTextField();
addPanel.add(yearField);
addButton = new JButton("添加图书");
addButton.addActionListener(this);
addPanel.add(addButton);
deleteButton = new JButton("删除图书");
deleteButton.addActionListener(this);
addPanel.add(deleteButton);
add(addPanel, BorderLayout.CENTER);
// 消息区域
messageArea = new JTextArea();
messageArea.setEditable(false);
JScrollPane messagePane = new JScrollPane(messageArea);
add(messagePane, BorderLayout.SOUTH);
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (login(username, password)) {
messageArea.setText("登录成功!");
} else {
messageArea.setText("登录失败!请检查用户名和密码。");
}
} else if (e.getSource() == registerButton) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (register(username, password)) {
messageArea.setText("注册成功!");
} else {
messageArea.setText("注册失败!请检查用户名是否已存在。");
}
} else if (e.getSource() == addButton) {
String title = titleField.getText();
String author = authorField.getText();
String isbn = isbnField.getText();
int year = Integer.parseInt(yearField.getText());
if (addBook(title, author, isbn, year)) {
messageArea.setText("添加图书成功!");
} else {
messageArea.setText("添加图书失败!");
}
} else if (e.getSource() == deleteButton) {
int id = Integer.parseInt(JOptionPane.showInputDialog(this, "请输入要删除的图书ID:"));
if (deleteBook(id)) {
messageArea.setText("删除图书成功!");
} else {
messageArea.setText("删除图书失败!");
}
}
}
public static Connection getConnection() throws SQLException {
String url = "jdbc:mysql://localhost:3306/book_management_system";
String user = "root";
String password = "password";
return DriverManager.getConnection(url, user, password);
}
public static boolean login(String username, String password) {
try {
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(
"SELECT * FROM users WHERE username=? AND password=?");
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
boolean result = rs.next();
rs.close();
ps.close();
conn.close();
return result;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static boolean register(String username, String password) {
try {
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(
"INSERT INTO users (username, password) VALUES (?, ?)");
ps.setString(1, username);
ps.setString(2, password);
int result = ps.executeUpdate();
ps.close();
conn.close();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static boolean addBook(String title, String author, String isbn, int year) {
try {
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(
"INSERT INTO books (title, author, isbn, year) VALUES (?, ?, ?, ?)");
ps.setString(1, title);
ps.setString(2, author);
ps.setString(3, isbn);
ps.setInt(4, year);
int result = ps.executeUpdate();
ps.close();
conn.close();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static boolean deleteBook(int id) {
try {
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(
"DELETE FROM books WHERE id=?");
ps.setInt(1, id);
int result = ps.executeUpdate();
ps.close();
conn.close();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
new BookManagementSystem();
}
}
```
希望这些代码对你有所帮助!
阅读全文