用Java代码打一个图书信息管理系统,要求有可视化窗口,实现Java与MySQL连接,功能要有增加图书,查找图书,删除图书,修改图书等,同时功能要分类
时间: 2024-03-24 07:39:20 浏览: 52
基于Java和MySQL语言实现图书管理系统
5星 · 资源好评率100%
好的,我可以提供一个简单的Java代码示例来实现这个图书信息管理系统。以下是基本的步骤和代码:
1. 创建一个Java项目,并添加MySQL驱动程序(JDBC驱动)。
2. 创建一个数据库,用于存储图书信息。在这个示例中,我们将创建一个名为“bookstore”的数据库,并添加一个名为“books”的表。
3. 在Java程序中使用JDBC连接MySQL数据库,并在代码中编写增加、查找、删除和修改图书信息的方法。以下是一些示例代码:
```java
import java.sql.*;
public class Bookstore {
private Connection conn;
private Statement stmt;
private ResultSet rs;
// 连接数据库
public void connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore", "root", "password");
stmt = conn.createStatement();
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
}
// 关闭连接
public void disconnect() {
try {
stmt.close();
conn.close();
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
}
// 增加图书
public void addBook(String title, String author, String publisher, double price, String category) {
try {
String sql = "INSERT INTO books (title, author, publisher, price, category) VALUES ('" + title + "', '" + author + "', '" + publisher + "', " + price + ", '" + category + "')";
stmt.executeUpdate(sql);
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
}
// 查找图书
public ResultSet findBook(String keyword, String category) {
try {
String sql = "SELECT * FROM books WHERE title LIKE '%" + keyword + "%' AND category = '" + category + "'";
rs = stmt.executeQuery(sql);
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
return rs;
}
// 删除图书
public void deleteBook(int id) {
try {
String sql = "DELETE FROM books WHERE id = " + id;
stmt.executeUpdate(sql);
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
}
// 修改图书
public void updateBook(int id, String title, String author, String publisher, double price, String category) {
try {
String sql = "UPDATE books SET title = '" + title + "', author = '" + author + "', publisher = '" + publisher + "', price = " + price + ", category = '" + category + "' WHERE id = " + id;
stmt.executeUpdate(sql);
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
}
}
```
4. 创建一个可视化窗口界面,用于用户操作。以下是一个简单的Java Swing窗口界面示例:
```java
import javax.swing.*;
import java.awt.event.*;
public class BookstoreGUI extends JFrame {
private JLabel titleLabel, authorLabel, publisherLabel, priceLabel, categoryLabel, keywordLabel;
private JTextField titleField, authorField, publisherField, priceField, keywordField;
private JComboBox<String> categoryBox;
private JButton addButton, findButton, deleteButton, updateButton;
private JTextArea resultArea;
private Bookstore bookstore;
public BookstoreGUI() {
setTitle("图书信息管理系统");
setSize(600, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建标签和文本框
titleLabel = new JLabel("书名:");
titleField = new JTextField(20);
authorLabel = new JLabel("作者:");
authorField = new JTextField(20);
publisherLabel = new JLabel("出版社:");
publisherField = new JTextField(20);
priceLabel = new JLabel("价格:");
priceField = new JTextField(20);
categoryLabel = new JLabel("分类:");
String[] categories = {"小说", "科技", "历史", "艺术"};
categoryBox = new JComboBox<>(categories);
keywordLabel = new JLabel("关键字:");
keywordField = new JTextField(20);
// 创建按钮
addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String title = titleField.getText();
String author = authorField.getText();
String publisher = publisherField.getText();
double price = Double.parseDouble(priceField.getText());
String category = (String) categoryBox.getSelectedItem();
bookstore.addBook(title, author, publisher, price, category);
resultArea.setText("添加成功!");
}
});
findButton = new JButton("查找");
findButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String keyword = keywordField.getText();
String category = (String) categoryBox.getSelectedItem();
ResultSet rs = bookstore.findBook(keyword, category);
resultArea.setText("");
try {
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String author = rs.getString("author");
String publisher = rs.getString("publisher");
double price = rs.getDouble("price");
String result = "ID:" + id + ",书名:" + title + ",作者:" + author + ",出版社:" + publisher + ",价格:" + price + "\n";
resultArea.append(result);
}
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
}
});
deleteButton = new JButton("删除");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(JOptionPane.showInputDialog("请输入要删除的图书ID:"));
bookstore.deleteBook(id);
resultArea.setText("删除成功!");
}
});
updateButton = new JButton("修改");
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(JOptionPane.showInputDialog("请输入要修改的图书ID:"));
String title = titleField.getText();
String author = authorField.getText();
String publisher = publisherField.getText();
double price = Double.parseDouble(priceField.getText());
String category = (String) categoryBox.getSelectedItem();
bookstore.updateBook(id, title, author, publisher, price, category);
resultArea.setText("修改成功!");
}
});
// 创建文本区域
resultArea = new JTextArea(10, 40);
resultArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(resultArea);
// 创建面板
JPanel inputPanel = new JPanel();
inputPanel.add(titleLabel);
inputPanel.add(titleField);
inputPanel.add(authorLabel);
inputPanel.add(authorField);
inputPanel.add(publisherLabel);
inputPanel.add(publisherField);
inputPanel.add(priceLabel);
inputPanel.add(priceField);
inputPanel.add(categoryLabel);
inputPanel.add(categoryBox);
inputPanel.add(addButton);
inputPanel.add(findButton);
inputPanel.add(deleteButton);
inputPanel.add(updateButton);
JPanel resultPanel = new JPanel();
resultPanel.add(keywordLabel);
resultPanel.add(keywordField);
resultPanel.add(scrollPane);
// 添加面板到窗口中
add(inputPanel, "North");
add(resultPanel, "Center");
// 连接数据库
bookstore = new Bookstore();
bookstore.connect();
}
public static void main(String[] args) {
BookstoreGUI gui = new BookstoreGUI();
gui.setVisible(true);
}
}
```
这个示例程序实现了一个基本的图书信息管理系统,包括添加图书、查找图书、删除图书和修改图书等功能。同时,它将图书按照分类进行分类,并提供了一个简单的可视化窗口界面,供用户进行操作。请注意,这个示例程序仅供参考,您可能需要根据自己的需求进行修改和调整。
阅读全文