编写一个Java操作数据库的程序,可以实现数据浏览、插入、修改、删除等操作,并在图形用户界面实现数据的显示。要求如下: (1)在数据库中创建一个学生表,有学号、姓名、性别、年龄、籍贯、院系属性。 (2)用JDBC中常用类对数据库中的学生表进行查询、插入、修改、删除等操作,最后在控制台输出。 (3)使用面向对象的思想,每一个功能写成方法在测试类中调用。 (4)在控制台中选择每个功绩操作。
时间: 2023-07-10 22:17:10 浏览: 115
JDBC连接数据库实现增删改查操作,饭店点餐系统。刚学Java时写的,Java入门项目,适合初学者学习.zip
好的,我会根据您的要求为您提供一个Java操作数据库的程序,可以实现数据浏览、插入、修改、删除等操作,并在图形用户界面实现数据的显示,具体代码如下:
```
import java.sql.*;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class StudentManagementSystem {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private PreparedStatement pstmt = null;
private String url = "jdbc:mysql://localhost:3306/student";
private String user = "root";
private String password = "root";
public StudentManagementSystem() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public void close() {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void insert(String sno, String name, String sex, int age, String nativePlace, String department) {
try {
String sql = "insert into student(sno, name, sex, age, nativePlace, department) values(?, ?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, sno);
pstmt.setString(2, name);
pstmt.setString(3, sex);
pstmt.setInt(4, age);
pstmt.setString(5, nativePlace);
pstmt.setString(6, department);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "插入成功");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "插入失败");
e.printStackTrace();
}
}
public void update(String sno, String name, String sex, int age, String nativePlace, String department) {
try {
String sql = "update student set name=?, sex=?, age=?, nativePlace=?, department=? where sno=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, sex);
pstmt.setInt(3, age);
pstmt.setString(4, nativePlace);
pstmt.setString(5, department);
pstmt.setString(6, sno);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "更新成功");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "更新失败");
e.printStackTrace();
}
}
public void delete(String sno) {
try {
String sql = "delete from student where sno=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, sno);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "删除成功");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "删除失败");
e.printStackTrace();
}
}
public void query() {
try {
rs = stmt.executeQuery("select * from student");
JFrame f = new JFrame();
JTable table = new JTable(buildTableModel(rs));
f.add(new JScrollPane(table));
f.pack();
f.setVisible(true);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
// 构造表头
String[] columnNames = new String[columnCount];
for (int i = 0; i < columnCount; i++) {
columnNames[i] = metaData.getColumnLabel(i + 1);
}
// 构造表格数据
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
while (rs.next()) {
Object[] rowData = new Object[columnCount];
for (int i = 0; i < columnCount; i++) {
rowData[i] = rs.getObject(i + 1);
}
model.addRow(rowData);
}
return model;
}
public static void main(String[] args) {
StudentManagementSystem sms = new StudentManagementSystem();
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1.浏览数据 2.插入数据 3.修改数据 4.删除数据 0.退出");
int choice = input.nextInt();
switch (choice) {
case 1:
sms.query();
break;
case 2:
System.out.println("请输入学号:");
String sno = input.next();
System.out.println("请输入姓名:");
String name = input.next();
System.out.println("请输入性别:");
String sex = input.next();
System.out.println("请输入年龄:");
int age = input.nextInt();
System.out.println("请输入籍贯:");
String nativePlace = input.next();
System.out.println("请输入院系:");
String department = input.next();
sms.insert(sno, name, sex, age, nativePlace, department);
break;
case 3:
System.out.println("请输入学号:");
sno = input.next();
System.out.println("请输入姓名:");
name = input.next();
System.out.println("请输入性别:");
sex = input.next();
System.out.println("请输入年龄:");
age = input.nextInt();
System.out.println("请输入籍贯:");
nativePlace = input.next();
System.out.println("请输入院系:");
department = input.next();
sms.update(sno, name, sex, age, nativePlace, department);
break;
case 4:
System.out.println("请输入学号:");
sno = input.next();
sms.delete(sno);
break;
case 0:
sms.close();
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
```
该程序实现了对数据库中的学生表进行查询、插入、修改、删除等操作,并通过图形用户界面显示数据。在控制台中选择每个操作,程序会自动调用相应的方法完成操作。需要注意的是,由于在图形界面中显示数据需要用到JTable组件,因此需要导入javax.swing.*和javax.swing.table.DefaultTableModel这两个包。
阅读全文