Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. package JDBC;
  2.  
  3. import java.sql.*;
  4.  
  5. public class JDBC_Students {
  6. public static void main(String[] args) throws Exception {
  7. try {
  8. Class.forName("com.mysql.jdbc.Driver");
  9. } catch (Exception E) {
  10. System.err.println("Unable to load driver.");
  11. E.printStackTrace();
  12. }
  13. try {
  14. Connection conn1;
  15. String dbUrl = "jdbc:mysql://csdb.cs.iastate.edu:3306/db363bbanothu";
  16. String user = "dbu363bbanothu";
  17. String password = "KmfE9217";
  18. conn1 = DriverManager.getConnection(dbUrl, user, password);
  19. System.out.println("*** Connected to the database ***");
  20.  
  21. // Functions to do
  22. update_student_values( conn1);
  23.  
  24.  
  25. } catch (SQLException se) {
  26. se.printStackTrace();
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }
  31.  
  32. private static void update_student_values(Connection conn1) throws SQLException {
  33. Statement statement = conn1.createStatement();
  34. Statement statement_1 = conn1.createStatement();
  35. Statement statement_2 = conn1.createStatement();
  36.  
  37. String sql = "SELECT StudentID, Classification, GPA, CreditHours FROM Student";
  38. ResultSet outerResultSet = statement.executeQuery(sql);
  39.  
  40. while (outerResultSet.next()) {
  41. int studentId = outerResultSet.getInt("StudentID");
  42. String classification = outerResultSet.getString("Classification");
  43. float gpa = outerResultSet.getFloat("GPA");
  44. int credits = outerResultSet.getInt("CreditHours");
  45.  
  46. System.out.println("\nBefore update: " + "Student Id: " + studentId + ", GPA: " + gpa + ", Classification: " + classification + ", CreditHour: " + credits);
  47.  
  48. sql = "SELECT StudentID, Grade FROM Enrollment where StudentID=" + studentId;
  49. ResultSet innerResultSet = statement_1.executeQuery(sql);
  50. int total_credit_hours = credits;
  51. float total_gpa = gpa * credits;
  52. while (innerResultSet.next()) {
  53. String grade = innerResultSet.getString("Grade");
  54. total_credit_hours += 3;
  55. total_gpa += grade_to_number(grade) * 3;
  56. }
  57. innerResultSet.close();
  58.  
  59.  
  60. double gpaToBeUpdated = Math.round((total_gpa / total_credit_hours) * 100) / 100.0;
  61. System.out.println("After update: " + "Student Id: "+ studentId + ", GPA: " + gpaToBeUpdated + ", Classification: " + get_new_classification(total_credit_hours) + ", CreditHour: " + total_credit_hours);
  62.  
  63. sql = "UPDATE Student " + "SET Classification = '" + get_new_classification(total_credit_hours) + "', GPA = " + gpaToBeUpdated + ", CreditHours = " + total_credit_hours + " WHERE StudentID=" + studentId;
  64. statement_2.executeUpdate(sql);
  65. }
  66. outerResultSet.close();
  67.  
  68. }
  69.  
  70. public static float grade_to_number(String grade) {
  71. if (grade.equalsIgnoreCase("A")) {
  72. return (float) 4.0;
  73. } else if (grade.equalsIgnoreCase("A-")) {
  74. return (float) 3.66;
  75. } else if (grade.equalsIgnoreCase("B+")) {
  76. return (float) 3.33;
  77. } else if (grade.equalsIgnoreCase("B")) {
  78. return (float) 3.00;
  79. } else if (grade.equalsIgnoreCase("B-")) {
  80. return (float) 2.66;
  81. } else if (grade.equalsIgnoreCase("C+")) {
  82. return (float) 2.33;
  83. } else if (grade.equalsIgnoreCase("C")) {
  84. return (float) 2.00;
  85. } else if (grade.equalsIgnoreCase("C-")) {
  86. return (float) 1.66;
  87. } else if (grade.equalsIgnoreCase("D+")) {
  88. return (float) 1.33;
  89. } else if (grade.equalsIgnoreCase("D")) {
  90. return (float) 1.00;
  91. } else {
  92. return 0;
  93. }
  94. }
  95.  
  96.  
  97. public static String get_new_classification(int credits) {
  98. if (credits < 30) {
  99. return "Freshman";
  100. } else if (credits < 60) {
  101. return "Sophomore";
  102. } else if (credits < 90) {
  103. return "Junior";
  104. } else {
  105. return "Senior";
  106. }
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement