2、在MySQL中运行以下脚本 create database support; use support; create table student( sno varchar(10) primary key, sname varchar(20), sbirth varchar(15) ); insert into student values('1001','zhou','1980-12-12'); insert into student values('1002','zhou','1980-12-12'); insert into student values('1003','zhou','1980-12-12'); insert into student values('1004','zhou','1980-12-12'); insert into student values('1005','zhou','1980-12-12'); 要求在java界面中输出学生表中的内容。使用JTable
时间: 2023-08-20 15:36:37 浏览: 131
可以使用以下代码实现:
```java
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
public class StudentTable {
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/support", "root", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
DefaultTableModel model = new DefaultTableModel();
model.addColumn("学号");
model.addColumn("姓名");
model.addColumn("生日");
while (rs.next()) {
Object[] row = new Object[3];
row[0] = rs.getString("sno");
row[1] = rs.getString("sname");
row[2] = rs.getString("sbirth");
model.addRow(row);
}
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
JFrame frame = new JFrame("学生表");
frame.add(scrollPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
注:需要替换掉代码中的数据库连接信息(URL、用户名和密码)。
阅读全文