利用javagui和jdbc编程实现学生信息查询系统
时间: 2024-01-14 10:04:20 浏览: 34
好的,您的问题是如何利用javagui和jdbc编程实现学生信息查询系统。
首先,您需要创建一个Java GUI应用程序,可以使用Swing或JavaFX库来实现。您可以使用文本框和标签等GUI控件来收集和显示学生信息。
然后,您需要使用JDBC连接到您的数据库,可以使用MySQL或Oracle等数据库。您需要编写SQL查询来检索学生信息,并将结果显示在您的GUI应用程序中。
以下是一个简单的示例程序,它演示了如何使用Java GUI和JDBC编程来查询学生信息:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class StudentQuery extends JFrame implements ActionListener {
private JLabel idLabel;
private JTextField idField;
private JButton queryButton;
private JTextArea resultArea;
public StudentQuery() {
// 设置窗口标题
super("学生信息查询");
// 创建GUI控件
idLabel = new JLabel("学生ID:");
idField = new JTextField(10);
queryButton = new JButton("查询");
resultArea = new JTextArea(10, 40);
// 添加GUI控件到窗口
JPanel panel = new JPanel();
panel.add(idLabel);
panel.add(idField);
panel.add(queryButton);
getContentPane().add(panel, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(resultArea), BorderLayout.CENTER);
// 添加事件处理程序
queryButton.addActionListener(this);
// 设置窗口大小和位置
setSize(500, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == queryButton) {
try {
// 连接到数据库
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
// 执行查询
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM student WHERE id = ?");
stmt.setInt(1, Integer.parseInt(idField.getText()));
ResultSet rs = stmt.executeQuery();
// 显示查询结果
resultArea.setText("");
while (rs.next()) {
resultArea.append("ID: " + rs.getInt("id") + "\n");
resultArea.append("姓名: " + rs.getString("name") + "\n");
resultArea.append("年龄: " + rs.getInt("age") + "\n");
}
// 关闭连接
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
resultArea.setText("查询失败:" + ex.getMessage());
}
}
}
public static void main(String[] args) {
StudentQuery app = new StudentQuery();
app.setVisible(true);
}
}
```
在这个示例中,我们创建了一个包含一个文本框、一个按钮和一个文本区域的GUI界面,当用户点击查询按钮时,我们使用JDBC从数据库中检索学生信息,并将结果显示在文本区域中。
请注意,这只是一个简单的示例程序,您需要根据您的实际需求进行更改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)