Advertisement
Guest User

Untitled

a guest
Oct 24th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1.  
  2. import java.sql.*;
  3.  
  4. public class P3 {
  5. public static void main(String[] args) throws Exception {new P3();}
  6.  
  7. public P3() throws Exception {
  8. // Load and register a JDBC driver
  9. try {
  10. // Load the driver (registers itself)
  11. Class.forName("com.mysql.cj.jdbc.Driver");
  12. } catch (Exception E) {
  13. System.err.println("Unable to load driver.");
  14. E.printStackTrace();
  15. }
  16. try {
  17. // Connect to the database
  18. Connection conn1;
  19. String dbUrl = "jdbc:mysql://csdb.cs.iastate.edu:3306/db363mhoppe";
  20. String user = "dbu363mhoppe";
  21. String password = "TrlK2579";
  22. conn1 = DriverManager.getConnection(dbUrl + "?useSSL=false&serverTimezone=CST", user, password);
  23. System.out.println("*** Connected to the database ***");
  24.  
  25. loadTables(conn1);
  26.  
  27. PartA_E(conn1);
  28.  
  29. PartB(conn1);
  30.  
  31. PartC(conn1);
  32.  
  33. PartD(conn1);
  34.  
  35. PartA_E(conn1);
  36.  
  37. PartF(conn1);
  38.  
  39. } catch (SQLException e) {
  40. System.out.println("SQLException: " + e.getMessage());
  41. System.out.println("SQLState: " + e.getSQLState());
  42. System.out.println("VendorError: " + e.getErrorCode());
  43. }
  44. }
  45.  
  46. private void PartA_E(Connection conn1) throws SQLException {
  47. Statement statement = conn1.createStatement();
  48. ResultSet rs = null;
  49.  
  50. rs = statement.executeQuery( "select p.Name, i.Salary " +
  51. "from Instructor i, Person p " +
  52. "where i.InstructorID = p.ID");
  53.  
  54. int totalSalary = 0;
  55.  
  56. while (rs.next()) {
  57. String name = rs.getString("Name");
  58. int salary = rs.getInt("Salary");
  59.  
  60. System.out.println(name + ", " + salary);
  61. totalSalary += salary;
  62. }
  63. System.out.println("Total Salary of all faculty: " + totalSalary);
  64.  
  65. statement.close();
  66. rs.close();
  67. }
  68.  
  69. private void PartB(Connection conn1) throws SQLException {
  70. Statement statement = conn1.createStatement();
  71. ResultSet rs = null;
  72.  
  73. rs = statement.executeQuery("select s.GPA from Student s order by GPA DESC");
  74. for(int i = 0; i < 20; i++) rs.next();
  75. double TWTYthGPA = rs.getDouble("GPA");
  76.  
  77. try {statement.executeUpdate("drop table MeritList");} catch (SQLException e) {}
  78.  
  79. statement.executeUpdate("create table MeritList as " +
  80. "select s.StudentID, s.Classification, s.MentorID, s.GPA " +
  81. "from Student s " +
  82. "where s.GPA >= " + TWTYthGPA + ";");
  83.  
  84. statement.close();
  85. rs.close();
  86. }
  87.  
  88. private void PartC(Connection conn1) throws SQLException {
  89. Statement statement = conn1.createStatement();
  90. ResultSet rs = null;
  91.  
  92. rs = statement.executeQuery("select * from MeritList m order by m.GPA");
  93.  
  94. while (rs.next()) {
  95. System.out.println(rs.getString("StudentID") + ", " +
  96. rs.getString("Classification") + ", " +
  97. rs.getString("MentorID") + ", " +
  98. rs.getDouble("GPA"));
  99. }
  100.  
  101. statement.close();
  102. rs.close();
  103. }
  104.  
  105. private void PartD(Connection conn1) throws SQLException {
  106. double[] raisePer = {1.04, 1.06, 1.08, 1.1};
  107.  
  108. Statement statement = conn1.createStatement();
  109. ResultSet rs = null;
  110. rs = statement.executeQuery(
  111. "select max( case " +
  112. "when sub.Classification = 'Freshman' then 1 " +
  113. "when sub.Classification = 'Sophomore' then 2 " +
  114. "when sub.Classification = 'Junior' then 3 " +
  115. "when sub.Classification = 'Senior' then 4 end ), sub.* " +
  116. "from( " +
  117. "select m.Classification, i.* " +
  118. "from MeritList m, Instructor i where i.InstructorID = m.MentorID) as sub "
  119. + "group by sub.InstructorID");
  120.  
  121. PreparedStatement stmt2 =
  122. conn1.prepareStatement ("update Instructor" + " " +
  123. "set Rank=? , Salary=?" + " " +
  124. "where InstructorID = ? " );
  125.  
  126. while (rs.next()) {
  127. stmt2.setString(1, rs.getString(4));
  128. stmt2.setInt(2,(int) (Integer.parseInt(rs.getString(5)) * raisePer[Integer.parseInt(rs.getString(1)) - 1]) );
  129. stmt2.setString(3,rs.getString(3));
  130. stmt2.executeUpdate();
  131. }
  132. statement.close();
  133. rs.close();
  134. }
  135.  
  136. private void PartF(Connection conn1) throws SQLException {
  137. Statement statement = conn1.createStatement();
  138.  
  139. try {statement.executeUpdate("drop table MeritList");} catch (SQLException e) {}
  140.  
  141. statement.close();
  142.  
  143. conn1.close();
  144. }
  145.  
  146. public void loadTables(Connection conn1){
  147. Statement stmt = null;
  148. try {
  149. stmt = conn1.createStatement();
  150.  
  151. stmt.executeUpdate("use db363mhoppe;");
  152. } catch (SQLException e) {}
  153.  
  154. try {stmt.executeUpdate("drop table Enrollment");} catch (SQLException e) {}
  155. try {stmt.executeUpdate("drop table Offering");} catch (SQLException e) {}
  156. try {stmt.executeUpdate("drop table Course");} catch (SQLException e) {}
  157. try {stmt.executeUpdate("drop table Student");} catch (SQLException e) {}
  158. try {stmt.executeUpdate("drop table Instructor");} catch (SQLException e) {}
  159. try {stmt.executeUpdate("drop table Person;");} catch (SQLException e) {}
  160.  
  161. try {
  162. stmt.executeUpdate("create table Person (" +
  163. "Name char (20)," +
  164. "ID char (9) not null," +
  165. "Address char (30)," +
  166. "DOB date," +
  167. "Primary key (ID));");
  168.  
  169.  
  170.  
  171. stmt.executeUpdate("create table Instructor (" +
  172. "InstructorID char (9) not null references Person(ID)," +
  173. "Rank char (12), " +
  174. "Salary int," +
  175. "Primary key ( InstructorID));");
  176.  
  177. stmt.executeUpdate("create table Student (" +
  178. "StudentID char (9) not null references Person," +
  179. "Classification char (10)," +
  180. "GPA double," +
  181. "MentorID char (9) references InstructorID(Instructor)," +
  182. "CreditHours int," +
  183. "Primary key ( StudentID )" +
  184. ");");
  185.  
  186. stmt.executeUpdate("create table Course (" +
  187. "CourseCode char (6) not null," +
  188. "CourseName char (50)," +
  189. "PreReq char (6)," +
  190. "Primary key ( CourseCode,PreReq)" +
  191. ");");
  192.  
  193. stmt.executeUpdate("create table Offering(" +
  194. "CourseCode char (6) not null," +
  195. "SectionNo int not null," +
  196. "InstructorID char (9) not null references Instructor(InstructorID), " +
  197. "Primary key ( CourseCode , SectionNo )" +
  198. ");");
  199.  
  200. stmt.executeUpdate("create table Enrollment (" +
  201. "CourseCode char(6) NOT NULL, " +
  202. "SectionNo int NOT NULL, " +
  203. "StudentID char(9) NOT NULL references Student, " +
  204. "Grade char(4) NOT NULL," +
  205. "primary key (CourseCode, StudentID)" +
  206. ");");
  207.  
  208. stmt.executeUpdate( "load xml local infile 'D:/External Storage/Documents/IASTATE/CS363/Project 1/UniversityXML/Course.xml' " +
  209. "into table Course " +
  210. "rows identified by '<Course>';");
  211.  
  212. stmt.executeUpdate( "load xml local infile 'D:/External Storage/Documents/IASTATE/CS363/Project 1/UniversityXML/Enrollment.xml' " +
  213. "into table Enrollment " +
  214. "rows identified by '<Enrollment>';");
  215.  
  216. stmt.executeUpdate( "load xml local infile 'D:/External Storage/Documents/IASTATE/CS363/Project 1/UniversityXML/Instructor.xml' " +
  217. "into table Instructor " +
  218. "rows identified by '<Instructor>';");
  219.  
  220. stmt.executeUpdate( "load xml local infile 'D:/External Storage/Documents/IASTATE/CS363/Project 1/UniversityXML/Offering.xml' " +
  221. "into table Offering " +
  222. "rows identified by '<Offering>';");
  223.  
  224. stmt.executeUpdate( "load xml local infile 'D:/External Storage/Documents/IASTATE/CS363/Project 1/UniversityXML/Person.xml' " +
  225. "into table Person " +
  226. "rows identified by '<Person>';");
  227.  
  228. stmt.executeUpdate( "load xml local infile 'D:/External Storage/Documents/IASTATE/CS363/Project 1/UniversityXML/Student.xml' " +
  229. "into table Student " +
  230. "rows identified by '<Student>';");
  231. stmt.close();
  232. } catch (SQLException e) {}
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement