Advertisement
Guest User

Untitled

a guest
Mar 4th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. //Import required packages
  2. import java.sql.*;
  3.  
  4. public class jdbc {
  5. // JDBC driver name and database URL
  6. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  7. static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
  8.  
  9. // Database credentials
  10. static final String USERNAME = "username";
  11. static final String PASSWORD = "password";
  12.  
  13.  
  14. /*
  15. * Function which returns point value based on the grades
  16. */
  17. public static float getGradeValue(String c) {
  18. if (c.equalsIgnoreCase("A")) {
  19. return (float) 4.0;
  20. } else if (c.equalsIgnoreCase("A-")) {
  21. return (float) 3.66;
  22. } else if (c.equalsIgnoreCase("B+")) {
  23. return (float) 3.33;
  24. } else if (c.equalsIgnoreCase("B")) {
  25. return (float) 3.00;
  26. } else if (c.equalsIgnoreCase("B-")) {
  27. return (float) 2.66;
  28. } else if (c.equalsIgnoreCase("C+")) {
  29. return (float) 2.33;
  30. } else if (c.equalsIgnoreCase("C")) {
  31. return (float) 2.00;
  32. } else if (c.equalsIgnoreCase("C-")) {
  33. return (float) 1.66;
  34. } else if (c.equalsIgnoreCase("D+")) {
  35. return (float) 1.33;
  36. } else if (c.equalsIgnoreCase("D")) {
  37. return (float) 1.00;
  38. } else {
  39. return 0;
  40. }
  41. }
  42.  
  43. /*
  44. * Function which returns classification value based on the total credits of student
  45. */
  46. public static String getClassification(int creditHour) {
  47. if (creditHour < 30) {
  48. return "Freshman";
  49. } else if (creditHour < 60) {
  50. return "Sophomore";
  51. } else if (creditHour < 90) {
  52. return "Junior";
  53. } else {
  54. return "Senior";
  55. }
  56. }
  57.  
  58. /**
  59. * Driver program. Please update the database setting such as driver, username password etc.
  60. * @param args
  61. */
  62. public static void main(String[] args) {
  63. Connection connection = null;
  64. Statement statement = null;
  65. try {
  66. // Register JDBC driver
  67. Class.forName("com.mysql.jdbc.Driver");
  68.  
  69. // Open a connection
  70. System.out.println("Connecting to a selected database...");
  71. connection = DriverManager
  72. .getConnection(DB_URL, USERNAME, PASSWORD);
  73. System.out.println("Connected database successfully...");
  74.  
  75. // Execute a query
  76. System.out.println("Creating statement...");
  77. statement = connection.createStatement();
  78.  
  79. String sql = "SELECT StudentID, Classification, GPA, CreditHours FROM Student";
  80. ResultSet outerResultSet = statement.executeQuery(sql);
  81.  
  82. // Extract data from result set.. executing once for each student
  83. while (outerResultSet.next()) {
  84.  
  85. // Retrieve Each Student record's individual columns
  86. int studentId = outerResultSet.getInt("StudentID");
  87. String classification = outerResultSet.getString("Classification");
  88. float gpa = outerResultSet.getFloat("GPA");
  89. int creditHour = outerResultSet.getInt("CreditHours");
  90.  
  91. // Display values before updating
  92. System.out.println("\nBefore updating:");
  93. System.out.print("Student Id: " + studentId);
  94. System.out.print(", GPA: " + gpa);
  95. System.out.print(", Classification: " + classification);
  96. System.out.println(", CreditHour: " + creditHour);
  97.  
  98. // select enrollment record for this particualr student
  99. sql = "SELECT StudentID, Grade FROM Enrollment where StudentID="
  100. + studentId;
  101. ResultSet innerResultSet = statement.executeQuery(sql);
  102.  
  103. int totalCreditHours = creditHour;
  104. float gpaSum = gpa * creditHour;
  105. while (innerResultSet.next()) {
  106. String grade = innerResultSet.getString("Grade");
  107. totalCreditHours += 3;
  108. gpaSum += getGradeValue(grade) * 3;
  109. }
  110.  
  111. // calculate final gpa by dividing the sum by total credits
  112. float gpaToBeUpdated = gpaSum / totalCreditHours;
  113.  
  114. // update the new values for this student into the database
  115. sql = "UPDATE Student " +
  116. "SET Classification = '" + getClassification(totalCreditHours) +
  117. "', GPA = " + gpaToBeUpdated +
  118. ", CreditHours = " + totalCreditHours +
  119. "WHERE StudentID=" + studentId;
  120. statement.executeUpdate(sql);
  121.  
  122. // again select
  123. sql = "SELECT StudentID, Classification, GPA, CreditHours FROM Student where StudentID="
  124. + studentId;
  125. innerResultSet = statement.executeQuery(sql);
  126.  
  127. // Retrieve by column name
  128. studentId = innerResultSet.getInt("StudentID");
  129. classification = innerResultSet.getString("Classification");
  130. gpa = innerResultSet.getFloat("GPA");
  131. creditHour = innerResultSet.getInt("CreditHours");
  132.  
  133. System.out.println("\nAfter update:");
  134. System.out.print("Student Id: " + studentId);
  135. System.out.print(", GPA: " + gpa);
  136. System.out.print(", Classification: " + classification);
  137. System.out.println(", CreditHour: " + creditHour);
  138. System.out
  139. .println("****************************************************************");
  140.  
  141. }
  142. outerResultSet.close();
  143. } catch (SQLException se) {
  144. // Handle errors for JDBC
  145. se.printStackTrace();
  146. } catch (Exception e) {
  147. // Handle errors for Class.forName
  148. e.printStackTrace();
  149. } finally {
  150. // finally block used to close resources
  151. try {
  152. if (statement != null)
  153. connection.close();
  154. } catch (SQLException se) {
  155. }// do nothing
  156. try {
  157. if (connection != null)
  158. connection.close();
  159. } catch (SQLException se) {
  160. se.printStackTrace();
  161. }// end finally try
  162. }// end try
  163.  
  164. System.out.println("Finishing program!");
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement