.数据库student中的表stud中存放了含有学号学号、姓名和成绩信息的若干学生信息,编写程序实现下列功能: (1)针对表stud编写一个java类Stud,包含表中各字段作为属性及对各属性的操作; (2)输入若干学生信息,将其插入到表stud中; (3)查询表stud中所有学生信息,将查询结果转换成类Stud对象,并为客户从ArrayList集合中; (4)使用匿名类实现接口Comparator,完成对ArrayList集合中学生信息的成绩进行降序排列; (5)输出排序前后的结果。
时间: 2024-04-23 13:23:43 浏览: 46
用java编写的实现学生信息操作的程序
3星 · 编辑精心推荐
好的,以下是实现上述功能的Java代码:
Stud.java:
```
public class Stud {
private String stuNo;
private String name;
private int score;
public Stud(String stuNo, String name, int score) {
this.stuNo = stuNo;
this.name = name;
this.score = score;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
```
Main.java:
```
import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
ArrayList<Stud> studList = new ArrayList<>();
//连接数据库
String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
String user = "root";
String password = "password";
try (Connection conn = DriverManager.getConnection(url, user, password)) {
System.out.println("连接数据库成功!");
//插入学生信息
String insertSql = "INSERT INTO stud (stuNo, name, score) VALUES (?, ?, ?)";
try (PreparedStatement stmt = conn.prepareStatement(insertSql)) {
stmt.setString(1, "001");
stmt.setString(2, "张三");
stmt.setInt(3, 90);
stmt.executeUpdate();
}
try (PreparedStatement stmt = conn.prepareStatement(insertSql)) {
stmt.setString(1, "002");
stmt.setString(2, "李四");
stmt.setInt(3, 85);
stmt.executeUpdate();
}
try (PreparedStatement stmt = conn.prepareStatement(insertSql)) {
stmt.setString(1, "003");
stmt.setString(2, "王五");
stmt.setInt(3, 95);
stmt.executeUpdate();
}
System.out.println("插入学生信息成功!");
//查询学生信息并转换为对象
String querySql = "SELECT * FROM stud";
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(querySql);
while (rs.next()) {
String stuNo = rs.getString("stuNo");
String name = rs.getString("name");
int score = rs.getInt("score");
Stud stud = new Stud(stuNo, name, score);
studList.add(stud);
}
}
System.out.println("查询学生信息成功!");
//按成绩降序排列学生信息
Collections.sort(studList, new Comparator<Stud>() {
@Override
public int compare(Stud o1, Stud o2) {
return o2.getScore() - o1.getScore();
}
});
System.out.println("排序学生信息成功!");
//输出排序前后的结果
System.out.println("排序前:");
for (Stud stud : studList) {
System.out.println(stud.getStuNo() + " " + stud.getName() + " " + stud.getScore());
}
System.out.println("排序后:");
for (Stud stud : studList) {
System.out.println(stud.getStuNo() + " " + stud.getName() + " " + stud.getScore());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
需要注意的是,为了运行上述代码,需要先在MySQL数据库中创建一个名为`test`的数据库并在其中创建一个名为`stud`的表,表的结构如下:
```
CREATE TABLE stud (
stuNo VARCHAR(20) NOT NULL PRIMARY KEY,
name VARCHAR(20) NOT NULL,
score INT NOT NULL
);
```
阅读全文