import java.sql.*; import java.util.Scanner; public class keshe { public static String stu_name; public static String Enrollment_time; public static String Faculties; public static String majors; public static String stu_class; public static String stu_num;//学号 public static String Course; public static String fir_sem; public static String sec_sem; public static String thr_sem; public static String fou_sem; public static String fiv_sem; public static String six_sem; public static String sev_sem; public static String eig_sem; static Connection conn; static Statement stmt = null; public static void main(String[] args) throws SQLException { String url = "jdbc:mysql://localhost:3306/student"; String username = "root"; String password = "qwj1514399005"; try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (Exception e) { e.printStackTrace(); } try { conn = DriverManager.getConnection(url, username, password); stmt = conn.createStatement(); } catch (Exception e) { e.printStackTrace(); } } //插入课程名字和成绩 public static void Insert_course() throws SQLException { try { PreparedStatement prestmt = conn.prepareStatement("INSERT INTO Course_grades(course,fir_sem,sec_sem,thr_sem,fou_sem,fiv_sem,six_sem,sev_sem,eig_sem,stu_name,stu_num)VALUES (?, ?, ?, ?, ?,?, ?, ?, ?, ?,?)"); System.out.println("分别输入插入的课程名,第一学期成绩,第二学期成绩,第三学期成绩,第四学期成绩,第五学期成绩,第六学期成绩,第七学期成绩,第八学期成绩,学生姓名,学号"); Scanner scan = new Scanner(System.in); Course = scan.nextLine(); fir_sem = scan.nextLine(); sec_sem = scan.nextLine(); thr_sem = scan.nextLine(); fou_sem = scan.nextLine(); fiv_sem = scan.nextLine(); six_sem = scan.nextLine(); sev_sem = scan.nextLine(); eig_sem = scan.nextLine(); stu_name = scan.nextLine(); stu_num = scan.nextLine(); prestmt.setString(1,Course ); prestmt.setString(2,fir_sem ); prestmt.setString(3,sec_sem ); prestmt.setString(4,thr_sem ); prestmt.setString(5,fou_sem ); prestmt.setString(6,fiv_sem ); prestmt.setString(7,six_sem ); prestmt.setString(8,sev_sem ); prestmt.setString(9,eig_sem ); prestmt.setString(10,stu_name ); prestmt.setString(11,stu_num ); prestmt.executeUpdate(); prestmt.close(); System.out.println("插入学生课程成功"); }catch (Exception e){ e.printStackTrace(); System.out.println("插入出错"); } } 添加可以计算出所有人的总分和平均分,课程的平均分 直接给出代码
时间: 2024-03-18 15:39:43 浏览: 77
JAVA String转化成java.sql.date和java.sql.time方法示例
5星 · 资源好评率100%
以下是添加可以计算出所有人的总分和平均分,课程的平均分的代码:
```
//计算所有人的总分和平均分
public static void Calculate_total_avg() throws SQLException {
try {
ResultSet rs = stmt.executeQuery("SELECT stu_name, SUM(fir_sem+sec_sem+thr_sem+fou_sem+fiv_sem+six_sem+sev_sem+eig_sem) AS total_score, AVG(fir_sem+sec_sem+thr_sem+fou_sem+fiv_sem+six_sem+sev_sem+eig_sem) AS avg_score FROM Course_grades GROUP BY stu_name");
while (rs.next()) {
System.out.println("学生姓名:" + rs.getString("stu_name") + " 总分:" + rs.getInt("total_score") + " 平均分:" + rs.getFloat("avg_score"));
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("计算出错");
}
}
//计算课程的平均分
public static void Calculate_course_avg() throws SQLException {
try {
ResultSet rs = stmt.executeQuery("SELECT course, AVG(fir_sem+sec_sem+thr_sem+fou_sem+fiv_sem+six_sem+sev_sem+eig_sem) AS avg_score FROM Course_grades GROUP BY course");
while (rs.next()) {
System.out.println("课程名:" + rs.getString("course") + " 平均分:" + rs.getFloat("avg_score"));
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("计算出错");
}
}
```
你可以在代码中调用这两个方法来计算总分和平均分,以及课程的平均分。
阅读全文