JAVA图书管理系统MySQL创建GUI代码
时间: 2023-09-08 19:10:49 浏览: 110
以下是一个简单的JAVA图书管理系统的MySQL创建GUI代码示例:
```java
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class CreateGUI {
private JFrame frame;
private JTextField bookNameTextField;
private JTextField authorTextField;
private JTextField publisherTextField;
private final String url = "jdbc:mysql://localhost:3306/library";
private final String user = "root";
private final String password = "root";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CreateGUI window = new CreateGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public CreateGUI() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("图书名称:");
lblNewLabel.setBounds(50, 50, 100, 30);
frame.getContentPane().add(lblNewLabel);
bookNameTextField = new JTextField();
bookNameTextField.setBounds(150, 50, 200, 30);
frame.getContentPane().add(bookNameTextField);
bookNameTextField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("作者:");
lblNewLabel_1.setBounds(50, 100, 100, 30);
frame.getContentPane().add(lblNewLabel_1);
authorTextField = new JTextField();
authorTextField.setBounds(150, 100, 200, 30);
frame.getContentPane().add(authorTextField);
authorTextField.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("出版社:");
lblNewLabel_2.setBounds(50, 150, 100, 30);
frame.getContentPane().add(lblNewLabel_2);
publisherTextField = new JTextField();
publisherTextField.setBounds(150, 150, 200, 30);
frame.getContentPane().add(publisherTextField);
publisherTextField.setColumns(10);
JButton btnNewButton = new JButton("添加");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bookName = bookNameTextField.getText();
String author = authorTextField.getText();
String publisher = publisherTextField.getText();
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO book (book_name, author, publisher) VALUES (?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, bookName);
pstmt.setString(2, author);
pstmt.setString(3, publisher);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "添加成功");
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败");
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
btnNewButton.setBounds(150, 200, 100, 30);
frame.getContentPane().add(btnNewButton);
}
}
```
上述代码创建了一个简单的图书管理系统的添加图书的GUI界面,并且将数据保存到MySQL数据库中。你需要根据实际需求修改数据库的连接信息和SQL语句。同时,你还需要在代码中添加其他的功能,如删除图书、修改图书信息等。
阅读全文