Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. package JDBC;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Example {
  6. public static void main(String[] args) throws Exception {
  7. // Load and register a JDBC driver
  8. try {
  9. // Load the driver (registers itself)
  10. Class.forName("com.mysql.jdbc.Driver");
  11. } catch (Exception E) {
  12. System.err.println("Unable to load driver.");
  13. E.printStackTrace();
  14. }
  15. try {
  16. // Connect to the database
  17. Connection conn1;
  18. Statement statement = null;
  19. String dbUrl = "jdbc:mysql://csdb.cs.iastate.edu:3306/db363bbanothu";
  20. String user = "dbu363bbanothu";
  21. String password = "KmfE9217";
  22. conn1 = DriverManager.getConnection(dbUrl, user, password);
  23. System.out.println("*** Connected to the database ***");
  24.  
  25. statement = conn1.createStatement();
  26. Statement statement_1 = conn1.createStatement();
  27. Statement statement_2 = conn1.createStatement();
  28.  
  29. String sql = "SELECT StudentID, Classification, GPA, CreditHours FROM Student";
  30. ResultSet outerResultSet = statement.executeQuery(sql);
  31.  
  32. // Extract data from result set.. executing once for each student
  33. while (outerResultSet.next()) {
  34.  
  35. // Retrieve Each Student record's individual columns
  36. int studentId = outerResultSet.getInt("StudentID");
  37. String classification = outerResultSet.getString("Classification");
  38. float gpa = outerResultSet.getFloat("GPA");
  39. int creditHour = outerResultSet.getInt("CreditHours");
  40.  
  41. // Display values before updating
  42. System.out.println("\nBefore updating:");
  43. System.out.print("Student Id: " + studentId);
  44. System.out.print(", GPA: " + gpa);
  45. System.out.print(", Classification: " + classification);
  46. System.out.println(", CreditHour: " + creditHour);
  47.  
  48. // select enrollment record for this particualr student
  49. sql = "SELECT StudentID, Grade FROM Enrollment where StudentID=" + studentId;
  50. ResultSet innerResultSet = statement_1.executeQuery(sql);
  51.  
  52. int totalCreditHours = creditHour;
  53. float gpaSum = gpa * creditHour;
  54. while (innerResultSet.next()) {
  55. String grade = innerResultSet.getString("Grade");
  56. totalCreditHours += 3;
  57. gpaSum += grade_to_number(grade) * 3;
  58. }
  59.  
  60. // calculate final gpa by dividing the sum by total credits
  61. float x = gpaSum / totalCreditHours;
  62. double gpaToBeUpdated = Math.round(x * 100) / 100.0;
  63.  
  64. System.out.print(gpaToBeUpdated + " ");
  65. System.out.print(totalCreditHours + " ");
  66. System.out.print(studentId + " ");
  67. System.out.print(get_new_classification(totalCreditHours) + " ");
  68.  
  69. sql = "UPDATE Student " + "SET Classification = '" + get_new_classification(totalCreditHours) + "', GPA = "
  70. + gpaToBeUpdated + ", CreditHours = " + totalCreditHours + " WHERE StudentID=" + studentId;
  71. statement_2.executeUpdate(sql);
  72.  
  73.  
  74. innerResultSet.close();
  75.  
  76. }
  77. outerResultSet.close();
  78. } catch (SQLException se) {
  79. se.printStackTrace();
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. }
  83. }
  84.  
  85. public static float grade_to_number(String grade) {
  86. if (grade.equalsIgnoreCase("A")) {
  87. return (float) 4.0;
  88. } else if (grade.equalsIgnoreCase("A-")) {
  89. return (float) 3.66;
  90. } else if (grade.equalsIgnoreCase("B+")) {
  91. return (float) 3.33;
  92. } else if (grade.equalsIgnoreCase("B")) {
  93. return (float) 3.00;
  94. } else if (grade.equalsIgnoreCase("B-")) {
  95. return (float) 2.66;
  96. } else if (grade.equalsIgnoreCase("C+")) {
  97. return (float) 2.33;
  98. } else if (grade.equalsIgnoreCase("C")) {
  99. return (float) 2.00;
  100. } else if (grade.equalsIgnoreCase("C-")) {
  101. return (float) 1.66;
  102. } else if (grade.equalsIgnoreCase("D+")) {
  103. return (float) 1.33;
  104. } else if (grade.equalsIgnoreCase("D")) {
  105. return (float) 1.00;
  106. } else {
  107. return 0;
  108. }
  109. }
  110.  
  111. /*
  112. * Function which returns classification value based on the total credits of
  113. * student
  114. */
  115. public static String get_new_classification(int credits) {
  116. if (credits < 30) {
  117. return "Freshman";
  118. } else if (credits < 60) {
  119. return "Sophomore";
  120. } else if (credits < 90) {
  121. return "Junior";
  122. } else {
  123. return "Senior";
  124. }
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement