用Java编写实验内容:赵一、钱二、孙三三位同学来自A省,该三位同学进入大学之前英语成绩采用百分制;李四、王五、张六三位同学来自B省,该三位同学进入大学之前英语成绩采用五分制。六位同学的成绩如下: 学号 姓名 成绩 10001 赵一 85 10002 钱二 74 10003 孙三 68 10004. 李四 B 10005 王五 A 10006 张六 C_ 编程要求1:通过泛型类设 置set、get和打印等方法,将以上六位同学的成绩在控制台打印出来; 编程要求2:将以上数据先写入到数据库StudengScore表中进行记录,便于今后查询(注:先通过eclipse写入,之后从数据库里读出)。,
时间: 2024-03-05 14:55:14 浏览: 139
数据库实验3交互式SQL(三)查询操作(A).docx
以下是用Java编写的实验内容:
1. 定义学生类:
```
public class Student<T> {
private String id;
private String name;
private T score;
public Student(String id, String name, T score) {
this.id = id;
this.name = name;
this.score = score;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public T getScore() {
return score;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setScore(T score) {
this.score = score;
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",成绩:" + score;
}
}
```
2. 定义主函数,创建学生对象并打印:
```
public class Main {
public static void main(String[] args) {
// 创建学生对象
Student<Integer> s1 = new Student<>("10001", "赵一", 85);
Student<Integer> s2 = new Student<>("10002", "钱二", 74);
Student<Integer> s3 = new Student<>("10003", "孙三", 68);
Student<String> s4 = new Student<>("10004", "李四", "B");
Student<String> s5 = new Student<>("10005", "王五", "A");
Student<String> s6 = new Student<>("10006", "张六", "C-");
// 打印学生信息
System.out.println(s1.toString());
System.out.println(s2.toString());
System.out.println(s3.toString());
System.out.println(s4.toString());
System.out.println(s5.toString());
System.out.println(s6.toString());
}
}
```
3. 将学生信息写入数据库:
```
import java.sql.*;
public class Main {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC", "root", "123456");
// 创建PreparedStatement对象
String sql = "INSERT INTO StudentScore(id, name, score) VALUES (?, ?, ?)";
pstmt = conn.prepareStatement(sql);
// 设置参数并执行
Student<Integer> s1 = new Student<>("10001", "赵一", 85);
pstmt.setString(1, s1.getId());
pstmt.setString(2, s1.getName());
pstmt.setInt(3, s1.getScore());
pstmt.executeUpdate();
Student<Integer> s2 = new Student<>("10002", "钱二", 74);
pstmt.setString(1, s2.getId());
pstmt.setString(2, s2.getName());
pstmt.setInt(3, s2.getScore());
pstmt.executeUpdate();
Student<Integer> s3 = new Student<>("10003", "孙三", 68);
pstmt.setString(1, s3.getId());
pstmt.setString(2, s3.getName());
pstmt.setInt(3, s3.getScore());
pstmt.executeUpdate();
Student<String> s4 = new Student<>("10004", "李四", "B");
pstmt.setString(1, s4.getId());
pstmt.setString(2, s4.getName());
pstmt.setString(3, s4.getScore());
pstmt.executeUpdate();
Student<String> s5 = new Student<>("10005", "王五", "A");
pstmt.setString(1, s5.getId());
pstmt.setString(2, s5.getName());
pstmt.setString(3, s5.getScore());
pstmt.executeUpdate();
Student<String> s6 = new Student<>("10006", "张六", "C-");
pstmt.setString(1, s6.getId());
pstmt.setString(2, s6.getName());
pstmt.setString(3, s6.getScore());
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();
}
}
}
}
```
4. 从数据库中读取学生信息并打印:
```
import java.sql.*;
public class Main {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC", "root", "123456");
// 创建Statement对象
stmt = conn.createStatement();
// 执行查询并获取结果集
String sql = "SELECT * FROM StudentScore";
rs = stmt.executeQuery(sql);
// 打印学生信息
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String score = rs.getString("score");
System.out.println("学号:" + id + ",姓名:" + name + ",成绩:" + score);
}
} 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();
}
}
}
}
```
阅读全文