Java编写:1.数据库student中的表stud中存放了含有学号学号、姓名和成绩信息的若干学生信息,编写程序实现下列功能: (1)针对表stud编写一个java类Stud,包含表中各字段作为属性及对各属性的操作; (2)输入若干学生信息,将其插入到表stud中; (3)查询表stud中所有学生信息,将查询结果转换成类Stud对象,并为客户从ArrayList集合中; (4)使用匿名类实现接口Comparator,完成对ArrayList集合中学生信息的成绩进行降序排列; (5)输出排序前后的结果。
时间: 2024-03-18 22:46:02 浏览: 50
好的,我会为您解答该问题。
首先,我们需要建立数据库和表。假设我们创建了一个名为"student"的数据库,并在其中创建了一个名为"stud"的表,表中包含三个字段:学号(id)、姓名(name)和成绩(score)。
接下来,我们按照题目要求编写Java程序。
1. 编写Java类Stud
```java
public class Stud {
private String id;
private String name;
private double score;
// 构造方法
public Stud(String id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
// getter和setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
```
2. 插入学生信息
```java
public class InsertData {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载驱动类
Class.forName("com.mysql.jdbc.Driver");
// 建立连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "password");
// 插入数据
String sql = "insert into stud(id, name, score) values(?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "001");
pstmt.setString(2, "张三");
pstmt.setDouble(3, 90.5);
pstmt.executeUpdate();
pstmt.setString(1, "002");
pstmt.setString(2, "李四");
pstmt.setDouble(3, 85);
pstmt.executeUpdate();
pstmt.setString(1, "003");
pstmt.setString(2, "王五");
pstmt.setDouble(3, 92);
pstmt.executeUpdate();
System.out.println("插入数据成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
3. 查询学生信息
```java
public class QueryData {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 加载驱动类
Class.forName("com.mysql.jdbc.Driver");
// 建立连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "password");
// 查询数据
String sql = "select * from stud";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
// 将查询结果转换成Stud对象并存入ArrayList集合中
ArrayList<Stud> list = new ArrayList<Stud>();
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
double score = rs.getDouble("score");
Stud stud = new Stud(id, name, score);
list.add(stud);
}
System.out.println("查询结果:");
for (Stud stud : list) {
System.out.println(stud.getId() + "\t" + stud.getName() + "\t" + stud.getScore());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
4. 对学生成绩进行降序排列
```java
public class SortData {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 加载驱动类
Class.forName("com.mysql.jdbc.Driver");
// 建立连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "password");
// 查询数据
String sql = "select * from stud order by score desc";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
// 将查询结果转换成Stud对象并存入ArrayList集合中
ArrayList<Stud> list = new ArrayList<Stud>();
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
double score = rs.getDouble("score");
Stud stud = new Stud(id, name, score);
list.add(stud);
}
// 使用匿名类实现接口Comparator,完成对ArrayList集合中学生信息的成绩进行降序排列
Collections.sort(list, new Comparator<Stud>() {
public int compare(Stud s1, Stud s2) {
if (s1.getScore() > s2.getScore())
return -1;
else if (s1.getScore() < s2.getScore())
return 1;
else
return 0;
}
});
System.out.println("排序前后的结果:");
for (Stud stud : list) {
System.out.println(stud.getId() + "\t" + stud.getName() + "\t" + stud.getScore());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
希望这个Java程序可以帮助到您。
阅读全文