Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. package proj2;
  2.  
  3. import java.sql.*;
  4.  
  5.  
  6.  
  7. public class P3 {
  8.  
  9. public static void main(String[] args) throws Exception {
  10. // Load and register a JDBC driver
  11. try {
  12. // Load the driver (registers itself)
  13. Class.forName("com.mysql.jdbc.Driver");
  14. } catch (Exception E) {
  15. System.err.println("Unable to load driver.");
  16. E.printStackTrace();
  17. }
  18. try {
  19. // Connect to the database
  20. Connection conn1;
  21. String dbUrl = "jdbc:mysql://csdb.cs.iastate.edu:3306/db363schultz1";
  22. String user = "dbu363schultz1";
  23. String password = "IxjD1867";
  24. conn1 = DriverManager.getConnection(dbUrl, user, password);
  25. System.out.println("*** Connected to the database ***");
  26.  
  27. // Create Statement and ResultSet variables to use throughout the
  28. // project
  29. Statement statement = conn1.createStatement();
  30. ResultSet rs;
  31.  
  32. // rs = statement.executeQuery("drop table Person");
  33. // rs = statement.executeQuery("drop table Instructor");
  34. // rs = statement.executeQuery("drop table Student");
  35. // rs = statement.executeQuery("drop table Course");
  36. // rs = statement.executeQuery("drop table Enrollment");
  37. // rs = statement.executeQuery("drop table Offering");
  38. //
  39. // rs = statement.executeQuery("create table Person (" + " "
  40. // + "Name char (20)," + " "
  41. // + "ID char (9) not null," + " "
  42. // + "Address char (30)," + " "
  43. // + "DOB date," + " "
  44. // + "Primary key (ID)");
  45. //
  46. // rs = statement.executeQuery("create table Instructor (" + " "
  47. // + "InstructorID char (9) not null references Person (ID)," + " "
  48. // + "Rank char (12)," + " "
  49. // + "Salary int," + " "
  50. // + "primary key (InstructorID))");
  51. //
  52. // rs = statement.executeQuery("create table Student (" + " "
  53. // + "StudentID char (9) not null references Person (ID)," + " "
  54. // + "Classification char (10)," + " "
  55. // + "GPA double," + " "
  56. // + "MentorID char (9) references Instructor (InstructorID)," + " "
  57. // + "CreditHours int," + " "
  58. // + "Primary key (StudentID))");
  59. //
  60. // rs = statement.executeQuery("create table Course (" + " "
  61. // + "CourseCode char (6) not null," + " "
  62. // + "CourseName char (50)," + " "
  63. // + "PreReq char (6))");
  64. //
  65. // rs = statement.executeQuery("create table Offering (" + " "
  66. // + "CourseCode char (6) not null references Course (CourseCode)," + " "
  67. // + "SectionNo int not null," + " "
  68. // + "InstructorID char (9) not null references Instructor (InstructorID)," + " "
  69. // + "Primary key (CourseCode, SectionNo))");
  70. //
  71. // rs = statement.executeQuery("create table Enrollment (" + " "
  72. // + "CourseCode char(6) NOT NULL," + " "
  73. // + "SectionNo int NOT NULL," + " "
  74. // + "StudentID char(9) NOT NULL references Student," + " "
  75. // + "Grade char(4) NOT NULL," + " "
  76. // + "primary key (CourseCode, StudentID)," + " "
  77. // + "foreign key (CourseCode, SectionNo) references Offering(CourseCode, SectionNo))");
  78. //
  79. // System.out.println("it made it this far");
  80.  
  81.  
  82.  
  83. rs = statement.executeQuery("select S.StudentID, S.GPA, S.CreditHours, E.Grade" + " "
  84. + "from Student S, Enrollment E" + " "
  85. + "where S.StudentID = E.StudentID");
  86. double gradeValue = 0;
  87.  
  88. while(rs.next()){
  89.  
  90. switch(rs.getString("Grade").trim()){
  91. case "A": //do things
  92. gradeValue = 4.0;
  93. break;
  94. case "A-":
  95. gradeValue = 3.66;
  96. break;
  97. case "B+":
  98. gradeValue = 3.33;
  99. break;
  100. case "B":
  101. gradeValue = 3.0;
  102. break;
  103. case "B-":
  104. gradeValue = 2.66;
  105. break;
  106. case "C+":
  107. gradeValue = 2.33;
  108. break;
  109. case "C":
  110. gradeValue = 2.0;
  111. break;
  112. case "C-":
  113. gradeValue = 1.66;
  114. break;
  115. case "D+":
  116. gradeValue = 1.33;
  117. break;
  118. case "D":
  119. gradeValue = 1.00;
  120. break;
  121. case "F":
  122. gradeValue = 0.0;
  123. break;
  124. }
  125.  
  126.  
  127. int newCreditHours = rs.getInt("CreditHours") + 3;
  128. double newGPA = ((rs.getDouble("GPA") * rs.getInt("CreditHours")) + (gradeValue * 3)) / (newCreditHours);
  129. newGPA = Math.round(newGPA*100)/100.0;
  130.  
  131. String newClassification = "";
  132. if(newCreditHours < 30){
  133. newClassification = "Freshman";
  134. }
  135. else if(newCreditHours < 60){
  136. newClassification = "Sophomore";
  137. }
  138. else if(newCreditHours < 90){
  139. newClassification = "Junior";
  140. }
  141. else{
  142. newClassification = "Senior";
  143. }
  144.  
  145.  
  146.  
  147.  
  148. // PreparedStatement stmt2 =
  149. // conn1.prepareStatement ("update Emp" + " " +
  150. // "set DName=? , Salary=?" + " " +
  151. // "where Name = ? " );
  152. // // Execute stmt2: Move Hari to Shoes with a salary of $65,000:
  153. // stmt2.setString(1,"Shoes");
  154. // stmt2.setInt(2,65000);
  155. // stmt2.setString(3,"Hari");
  156. // stmt2.executeUpdate();
  157. PreparedStatement stmt2 =
  158. conn1.prepareStatement("update Student" + " "
  159. + "set GPA=?, Classification=?, CreditHours=?" + " "
  160. + "where StudentID=?"
  161. );
  162. stmt2.setDouble(1, newGPA);
  163. stmt2.setString(2, newClassification);
  164. stmt2.setInt(3, newCreditHours);
  165. stmt2.setString(4, rs.getString("StudentID"));
  166. stmt2.executeUpdate();
  167.  
  168. }
  169.  
  170.  
  171. ResultSet rs2;
  172. // rs2 = statement.executeQuery("select S.StudentID, S.GPA, S.CreditHours, E.Grade" + " "
  173. // + "from Student S, Enrollment E" + " "
  174. // + "where S.StudentID = E.StudentID");
  175. rs2 = statement.executeQuery("select S.StudentID, S.MentorID, S.GPA" + " "
  176. + "from Student S" + " "
  177. + "where S.Classification = 'Senior'" + " "
  178. + "order by S.GPA desc"
  179. );
  180. double lowestOfHigh = 0;
  181. rs2.next();
  182. for(int i=0; i<5; i++){
  183. System.out.println(rs2.getString("StudentID") + " " + rs2.getString("MentorID") + " " + rs2.getDouble("GPA"));
  184. lowestOfHigh = rs2.getDouble("GPA");
  185. rs2.next();
  186. }
  187. while(rs2.getDouble("GPA") == lowestOfHigh){
  188. System.out.println(rs2.getString("StudentID") + " " + rs2.getString("MentorID") + " " + rs2.getDouble("GPA"));
  189. rs2.next();
  190. }
  191.  
  192.  
  193. // // get salaries of all instructors
  194. // rs = statement.executeQuery("select * from Instructor f");
  195. //
  196. // int totalSalary = 0;
  197. // int salary;
  198. //
  199. // while (rs.next()) {
  200. // // get value of salary from each tuple
  201. // salary = rs.getInt("Salary");
  202. // totalSalary += salary;
  203. // }
  204. // System.out.println("Total Salary of all faculty: " + totalSalary);
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. // Close all statements and connections
  217. statement.close();
  218. rs.close();
  219. conn1.close();
  220.  
  221. } catch (SQLException e) {
  222. System.out.println("SQLException: " + e.getMessage());
  223. System.out.println("SQLState: " + e.getSQLState());
  224. System.out.println("VendorError: " + e.getErrorCode());
  225. }
  226. }
  227.  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement