要用MySQL写一个图书管理系统需要先创建数据库和表,然后编写Java代码进行连接,查询,插入等操作。同时,为了方便用户操作,可以通过可视化工具将数据以表格或者图形的形式展示出来。以下是一个简单的MySQL图书管理系统实现过程:
- 创建数据库和表
首先需要在MySQL中创建一个名为“library”的数据库,然后在该数据库下创建一个名为“books”的表,包含id、title、author、isbn、publisher、price六个字段。
CREATE DATABASE library;
USE library;
CREATE TABLE books (
id INT(11) NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
author VARCHAR(255),
isbn VARCHAR(20),
publisher VARCHAR(255),
price DOUBLE,
PRIMARY KEY(id)
);
2.编写Java代码
可以使用JDBC(Java Database Connectivity)连接MySQL数据库,进行查询、插入数据等操作。以下是一个简单的查询图书信息的范例代码:
import java.sql.*;
public class BookDao {
private static final String URL = "jdbc:mysql://localhost:3306/library";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "SELECT * FROM books WHERE title LIKE ?";
ps = conn.prepareStatement(sql);
ps.setString(1, "%" + "Java" + "%");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt("id") + ", "
+ rs.getString("title") + ", "
+ rs.getString("author") + ", "
+ rs.getString("isbn") + ", "
+ rs.getString("publisher") + ", "
+ rs.getDouble("price"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3.使用可视化工具展示数据
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class BookManagementSystemFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField searchField;
private JTable table;
public BookManagementSystemFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
setTitle("图书管理系统");
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][]{},
new String[]{
"编号", "书名", "作者", "ISBN", "出版社", "价格"
}
));
scrollPane.setViewportView(table);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("关键字:");
panel.add(lblNewLabel);
searchField = new JTextField();
panel.add(searchField);
searchField.setColumns(20);
JButton searchButton = new JButton("查询");
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String keyword = searchField.getText();
if (keyword != null && !keyword.trim().equals("")) {
BookDao dao = new BookDao();
List<Book> books = dao.getBooks(keyword);
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
for (Book book : books) {
model.addRow(new Object[]{
book.getId(),
book.getTitle(),
book.getAuthor(),
book.getIsbn(),
book.getPublisher(),
book.getPrice()
});
}
}
}
});
panel.add(searchButton);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
BookManagementSystemFrame frame = new BookManagementSystemFrame();
frame.setVisible(true);
});
}
}
注意,上述代码是一个简单的GUI程序,实际上需要更加深入地学习Java面向对象编程、GUI编程以及MySQL数据库连接和操作等相关知识,才能够写出完整可用的代码。
本文含有隐藏内容,请 开通VIP 后查看