Java代码界面成绩管理连接数据库
时间: 2023-08-29 15:04:33 浏览: 50
好的,以下是Java代码界面成绩管理连接数据库的示例代码:
```java
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScoreManagementSystem extends JFrame implements ActionListener {
private Connection conn;
private Statement stmt;
private ResultSet rs;
private JButton connectButton;
private JButton queryButton;
private JTextField idField;
private JTextField nameField;
private JTextField scoreField;
private JTextArea resultArea;
public ScoreManagementSystem() {
super("Score Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
connectButton = new JButton("Connect to Database");
queryButton = new JButton("Query");
idField = new JTextField(10);
nameField = new JTextField(10);
scoreField = new JTextField(10);
resultArea = new JTextArea(20, 50);
connectButton.addActionListener(this);
queryButton.addActionListener(this);
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(3, 2));
inputPanel.add(new JLabel("Student ID:"));
inputPanel.add(idField);
inputPanel.add(new JLabel("Student Name:"));
inputPanel.add(nameField);
inputPanel.add(new JLabel("Score:"));
inputPanel.add(scoreField);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2));
buttonPanel.add(connectButton);
buttonPanel.add(queryButton);
JScrollPane scrollPane = new JScrollPane(resultArea);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
contentPane.add(scrollPane, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == connectButton) {
connectToDatabase();
} else if (e.getSource() == queryButton) {
queryDatabase();
}
}
private void connectToDatabase() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/score_db";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
JOptionPane.showMessageDialog(this, "Connection to database successful!");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
private void queryDatabase() {
try {
String id = idField.getText();
String name = nameField.getText();
String score = scoreField.getText();
String sql = "SELECT * FROM score WHERE 1=1";
if (!id.isEmpty()) {
sql += " AND id='" + id + "'";
}
if (!name.isEmpty()) {
sql += " AND name='" + name + "'";
}
if (!score.isEmpty()) {
sql += " AND score='" + score + "'";
}
rs = stmt.executeQuery(sql);
resultArea.setText("");
while (rs.next()) {
String result = "ID: " + rs.getString("id") + "\n";
result += "Name: " + rs.getString("name") + "\n";
result += "Score: " + rs.getString("score") + "\n\n";
resultArea.append(result);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
public static void main(String[] args) {
new ScoreManagementSystem();
}
}
```
该代码实现了一个简单的成绩管理系统界面,包括了连接数据库和查询数据库两个功能。其中,连接数据库使用了MySQL数据库,需要使用MySQL JDBC驱动。查询数据库可以根据学生的ID、姓名或成绩进行查询。在查询过程中,根据用户输入的条件构造SQL语句进行查询操作,将结果显示在界面的文本框中。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)