Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.13 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Exercise2 {
  7.  
  8. private static String url = "jdbc:postgresql://dbteach2/";
  9. private static String username = "lxe307";
  10. private static String pass = "wrumelib";
  11.  
  12. static Scanner scanny = new Scanner(System.in);
  13.  
  14. public static void main(String[] args) throws SQLException {
  15.  
  16. try {
  17. Class.forName("org.postgresql.Driver");
  18. } catch (ClassNotFoundException ex) {
  19. System.out.println("Driver not found");
  20. }
  21. System.out.println("PostgreSQL driver registered.")
  22. ;
  23. Connection conn = null;
  24. try {
  25. conn = DriverManager.getConnection(url, username, pass);
  26. } catch (SQLException ex) {
  27. ex.printStackTrace();
  28. }
  29. if (conn != null) {
  30. System.out.println("Database accessed!");
  31. } else {
  32. System.out.println("Failed to make connection");
  33. }
  34.  
  35. try
  36. {
  37.  
  38. PreparedStatement dropTable = conn.prepareStatement(
  39. "DROP TABLE IF EXISTS Student CASCADE; DROP TABLE IF EXISTS Lecturer CASCADE;" +
  40. "DROP TABLE IF EXISTS Module CASCADE;DROP TABLE IF EXISTS Marks CASCADE;"+
  41. "DROP TABLE IF EXISTS StudentContact CASCADE; DROP TABLE IF EXISTS NextOfKinContact CASCADE; " +
  42. "DROP TABLE IF EXISTS Titles CASCADE;DROP TABLE IF EXISTS Type CASCADE;");
  43.  
  44. dropTable.executeUpdate();
  45. System.out.println("Tables dropped");
  46.  
  47. try {
  48. conn.setAutoCommit(false);
  49. }
  50. catch (SQLException ex){
  51. ex.printStackTrace();
  52. }
  53.  
  54. PreparedStatement createTitlesT = conn.prepareStatement(
  55. "CREATE TABLE Titles("+
  56. "titleID INTEGER NOT NULL UNIQUE,"+
  57. "titleString CHAR(30), CHECK (titleString ='Mr' OR "+
  58. "titleString ='Mrs' OR "+
  59. "titleString = 'Ms' OR "+
  60. "titleString ='Miss' OR "+
  61. "titleString ='Dr' OR "+
  62. "titleString ='Master' ),"+
  63. "PRIMARY KEY(titleID)"+
  64. ");"
  65. );
  66. createTitlesT.executeUpdate();
  67. System.out.println("Titles Table created");
  68.  
  69. String titlesString = "Insert into Titles VALUES (1, 'Mr');"+
  70. "Insert into Titles VALUES (2, 'Mrs');"+
  71. "Insert into Titles VALUES (3, 'Ms');"+
  72. "Insert into Titles VALUES (4, 'Miss');"+
  73. "Insert into Titles VALUES (5, 'Dr');"+
  74. "Insert into Titles VALUES (6, 'Master');";
  75. PreparedStatement addTitles = conn.prepareStatement(titlesString);
  76. addTitles.executeUpdate();
  77.  
  78. PreparedStatement createTypeT = conn.prepareStatement(
  79. "CREATE TABLE Type("+
  80. "typeID INTEGER NOT NULL UNIQUE,"+
  81. "typeString CHAR(30),"+
  82. "PRIMARY KEY(typeID)"+
  83. ");"
  84. );
  85. createTypeT.executeUpdate();
  86. System.out.println("Type Table created");
  87.  
  88.  
  89. String typeString = "Insert into Type VALUES (0, 'sit');"+
  90. "Insert into Type VALUES (1, 'resit');"+
  91. "Insert into Type VALUES (2, 'repeat');";
  92. PreparedStatement addType = conn.prepareStatement(typeString);
  93. addType.executeUpdate();
  94.  
  95. PreparedStatement createStudentT = conn.prepareStatement(
  96. "CREATE TABLE Student("+
  97. "studentID INTEGER NOT NULL UNIQUE,"+
  98. "titleID INTEGER NOT NULL,"+
  99. "forename CHAR(20),"+
  100. "familyName CHAR(30),"+
  101. "dateOfBirth DATE,"+//maybe check constraints???
  102. "PRIMARY KEY(studentID),"+
  103. "FOREIGN KEY(titleID) REFERENCES Titles(titleID)"+
  104. ");"
  105. );
  106. createStudentT.executeUpdate();
  107. System.out.println("Student Table created");
  108.  
  109.  
  110. Random randy = new Random();
  111. String addStudents="";
  112. for(int i=1;i<101;i++) {
  113.  
  114. addStudents = addStudents+"Insert into Student VALUES (" + i + ", " +
  115. ((Integer)(1+randy.nextInt(5))).toString() + ", '" + RandomName.getForename() +
  116. "', '" + RandomName.getSurname() + "', '10-09-1994');";
  117. }
  118. PreparedStatement addStudentT = conn.prepareStatement(addStudents);
  119. addStudentT.executeUpdate();
  120.  
  121.  
  122. PreparedStatement createLecturerT = conn.prepareStatement(
  123. "CREATE TABLE Lecturer("+
  124. "lecturerID INTEGER NOT NULL UNIQUE,"+
  125. "titleID INTEGER NOT NULL,"+
  126. "foreName CHAR(20),"+
  127. "familyName CHAR(30),"+
  128. "PRIMARY KEY(lecturerID),"+
  129. "FOREIGN KEY(titleID) REFERENCES Titles(titleID)"+
  130. ");"
  131. );
  132. createLecturerT.executeUpdate();
  133. System.out.println("Lecturer Table created");
  134.  
  135. String addLecturers="";
  136. for(int a=1; a<6; a++) {
  137.  
  138. addLecturers = addLecturers+"Insert into Lecturer VALUES (" + a + ", " +
  139. ((Integer)(1+randy.nextInt(5))).toString() + ", '" + RandomName.getForename() +
  140. "', '" + RandomName.getSurname() + "');";
  141. }
  142. PreparedStatement addLecturerT = conn.prepareStatement(addLecturers);
  143. addLecturerT.executeUpdate();
  144.  
  145. PreparedStatement createModuleT = conn.prepareStatement(
  146. "CREATE TABLE Module("+
  147. "moduleID INTEGER NOT NULL UNIQUE,"+
  148. "moduleName CHAR(50),"+
  149. "moduleDescription CHAR(50),"+
  150. "lecturerID INTEGER NOT NULL,"+
  151. "PRIMARY KEY(moduleID),"+
  152. "FOREIGN KEY(lecturerID) REFERENCES Lecturer(lecturerID)"+
  153. ");"
  154. );
  155. createModuleT.executeUpdate();
  156. System.out.println("Module Table created");
  157.  
  158. String addModules="";
  159. for(int i=1; i<101; i++) {
  160. addModules = addModules+"Insert into Module VALUES (" + i + ", 'Module Name "
  161. + i + "', 'Module Description " + i + "', '" +
  162. ((Integer)(1+randy.nextInt(5))).toString() + "');";
  163. }
  164. PreparedStatement addModuleT = conn.prepareStatement(addModules);
  165. addModuleT.executeUpdate();
  166.  
  167. PreparedStatement createMarksT = conn.prepareStatement(
  168. "CREATE TABLE Marks("+
  169. "year CHAR(10),"+
  170. "mark INTEGER,"+
  171. "notes CHAR(100),"+
  172. "typeID INTEGER NOT NULL,"+
  173. "moduleID INTEGER NOT NULL,"+
  174. "studentID INTEGER NOT NULL,"+
  175. "FOREIGN KEY(moduleID) REFERENCES Module(moduleID),"+
  176. "FOREIGN KEY(studentID) REFERENCES Student(studentID)"+
  177. ");"
  178. );
  179. createMarksT.executeUpdate();
  180. System.out.println("Marks Table created");
  181.  
  182. String addMarks="";
  183. for(int b=1; b<101; b++) {
  184. addMarks = addMarks+"Insert into Marks VALUES ('2013-14 ', " +
  185. ((Integer)(1+randy.nextInt(100))).toString() +
  186. ", 'Notes '," + ((Integer)(randy.nextInt(3))).toString() +
  187. ", " + ((Integer)(1+randy.nextInt(100))).toString() +
  188. ", " + ((Integer)(b)).toString() + ");";
  189. }
  190.  
  191. PreparedStatement addMarksT = conn.prepareStatement(addMarks);
  192. addMarksT.executeUpdate();
  193.  
  194. PreparedStatement alterMarksT = conn.prepareStatement(
  195. "ALTER TABLE Marks ADD CONSTRAINT marks_Key UNIQUE (studentID, moduleID);");
  196. alterMarksT.executeUpdate();
  197.  
  198. PreparedStatement createStudentContactT = conn.prepareStatement(
  199. "CREATE TABLE StudentContact("+
  200. "studentID INTEGER NOT NULL UNIQUE,"+
  201. "eMailAddress CHAR(50),"+
  202. "postalAddress CHAR(90),"+
  203. "FOREIGN KEY(studentID) references Student(studentID)"+
  204. ");"
  205. );
  206. createStudentContactT.executeUpdate();
  207. System.out.println("StudentContact table created");
  208.  
  209. String addStuCon="";
  210. for(int c=1; c<101; c++) {
  211. addStuCon=addStuCon+"Insert into StudentContact VALUES (" + c
  212. + ", 'eMailAddress " + c + "', 'postalAddress " + c + "');";
  213. }
  214.  
  215. PreparedStatement addStuConT = conn.prepareStatement(addStuCon);
  216. addStuConT.executeUpdate();
  217.  
  218. PreparedStatement createNextOfKinContactT = conn.prepareStatement(
  219. "CREATE TABLE NextOfKinContact("+
  220. "studentID INTEGER NOT NULL UNIQUE,"+
  221. "eMailAddress CHAR(50),"+
  222. "postalAddress CHAR(90),"+
  223. "FOREIGN KEY(studentID) REFERENCES Student(studentID)"+
  224. ");"
  225. );
  226. createNextOfKinContactT.executeUpdate();
  227. System.out.println("NextOfKinContact table created");
  228.  
  229. String addNextKin="";
  230. for(int d=1;d<101;d++) {
  231. addNextKin=addNextKin+"Insert into NextOfKinContact VALUES (" + d
  232. + ", 'eMailAddress " + d + "', 'postalAddress " + d + "');";
  233. }
  234.  
  235. PreparedStatement addNextKinT = conn.prepareStatement(addNextKin);
  236. addNextKinT.executeUpdate();
  237.  
  238. try {
  239. conn.commit();
  240. conn.setAutoCommit(true);
  241. }
  242. catch (SQLException ex) {
  243. ex.printStackTrace();
  244. }
  245.  
  246. }
  247.  
  248. catch (SQLException e)
  249. {
  250. e.printStackTrace();
  251. }
  252.  
  253.  
  254. //Scanner s = new Scanner(System.in);
  255. String userChoice = "0"; //user's input
  256. int option = 0; //what the program will do
  257. do {
  258. System.out.println("Menu");
  259. System.out.println("Press 1 to register new student");
  260. System.out.println("Press 2 to add marks");
  261. System.out.println("Press 3 to view student transcript");
  262. System.out.println("Press 4 to view report");
  263. System.out.println("Press 5 to quit");
  264. System.out.println("");
  265. userChoice = scanny.nextLine();
  266. System.out.println(userChoice);
  267. if(userChoice.equals("1"))
  268. {
  269. option=1;
  270. System.out.println("1 entered");
  271.  
  272. }
  273. else if(userChoice.equals("2"))
  274. {
  275. option=2;
  276. System.out.println("2 entered");
  277.  
  278. }
  279. else if(userChoice.equals("3"))
  280. {
  281. option=3;
  282. System.out.println("3 entered");
  283.  
  284. }
  285. else if(userChoice.equals("4"))
  286. {
  287. option=4;
  288. System.out.println("4 entered");
  289.  
  290. }
  291. else if(userChoice.equals("5"))
  292. {
  293. option=5;
  294. System.out.println("5 entered");
  295.  
  296. }else {
  297. option=0;
  298. }
  299.  
  300. if(option== 1)
  301. {
  302. System.out.println("A new student is being registered");
  303. newStudent(conn);
  304. }
  305. else if (option== 2)
  306. {
  307. System.out.println("Marks are being added");
  308. addMark(conn);
  309. }
  310. else if (option== 3)
  311. {
  312. System.out.println("A transcript is being produced");
  313. produceTranscript(conn);
  314.  
  315. }
  316. else if (option== 4)
  317. {
  318. System.out.println("A report is being produced");
  319. produceReport(conn);
  320.  
  321. }
  322.  
  323.  
  324. } while(option != 5);
  325. }
  326.  
  327. public static boolean validYear(String year)
  328. {
  329. boolean valid = true;
  330. if(year.length() != 7) //checks length of input is 7, e.g. 2014-15
  331. {
  332. valid = false;
  333. }
  334. else
  335. {
  336. String f1 = year.substring(0, 4); //stores first 4 elements, e.g. 2014
  337. String f2 = year.substring(2, 4); //stores decade date of first year, e.g. 14
  338. String s1 = year.substring(5, 7); //stores decade date of first year, e.g. 15
  339.  
  340. try
  341. {
  342. int if1 = Integer.parseInt(f1);
  343. int if2 = Integer.parseInt(f2);
  344. int is1 = Integer.parseInt(s1);
  345.  
  346. if(if1 < 1900 || if1 > 2014 || is1 != if2 + 1)
  347. {
  348. valid = false;
  349. }
  350. }
  351. catch (NumberFormatException e)
  352. {
  353. valid = false;
  354. }
  355. }
  356. return valid;
  357. }
  358.  
  359. public static boolean vDate(String date)
  360. {
  361. boolean valid = true;
  362.  
  363. String day = date.substring(8, 10);
  364. String month = date.substring(5, 7);
  365. String year = date.substring(0, 4);
  366.  
  367. int iyear = Integer.parseInt(year);
  368. int imonth = Integer.parseInt(month);
  369. int iday = Integer.parseInt(day);
  370.  
  371. if(iday < 1 || (month.equals("02") && iday > 28) || imonth < 1 || imonth > 12 || iyear < 1900 || iyear > 2015 || ((!month.equals("09"))||(!month.equals("04"))
  372. ||(!month.equals("06"))||(!month.equals("11")))&& iday >30 || iday > 31)
  373. {
  374. valid = false;
  375. }
  376.  
  377. return valid;
  378. }
  379.  
  380. public static boolean isInteger(String i)
  381. {
  382. boolean isint = true;
  383.  
  384. try {Integer.parseInt(i);}
  385. catch (NumberFormatException e) {isint = false;}
  386.  
  387. return isint;
  388. }
  389.  
  390. public static void addMark(Connection conn) throws SQLException
  391. {
  392. System.out.println("Enter the student's ID:");
  393. String studentID = scanny.nextLine();
  394.  
  395. while(!isInteger(studentID))
  396. {
  397. System.out.println("Please enter a valid student ID in integer form:");
  398. studentID = scanny.nextLine();
  399. }
  400.  
  401. Statement stmt = conn.createStatement();
  402.  
  403. ResultSet students = stmt.executeQuery("SELECT * FROM Student WHERE studentID = " + studentID + ";");
  404.  
  405. while(!students.next())
  406. {
  407. System.out.println("Student does not exist. Please enter a valid student ID:");
  408. studentID = scanny.nextLine();
  409.  
  410. while(!isInteger(studentID))
  411. {
  412. System.out.println("Please enter a valid student ID in integer form:");
  413. studentID = scanny.nextLine();
  414. }
  415.  
  416. students = stmt.executeQuery("SELECT * FROM Student WHERE studentID = " + studentID + ";");
  417. }
  418.  
  419. System.out.println("Enter the module ID:");
  420. String moduleID = scanny.nextLine();
  421.  
  422. while(!isInteger(moduleID))
  423. {
  424. System.out.println("Please enter a valid module ID in integer form:");
  425. moduleID = scanny.nextLine();
  426. }
  427.  
  428. ResultSet modules = stmt.executeQuery("SELECT * FROM Module WHERE moduleID = " + moduleID + ";");
  429.  
  430. while(!modules.next())
  431. {
  432. System.out.println("Module does not exist. Please enter a valid module ID:");
  433. moduleID = scanny.nextLine();
  434.  
  435. while(!isInteger(moduleID))
  436. {
  437. System.out.println("Please enter a valid module ID in integer form:");
  438. moduleID = scanny.nextLine();
  439. }
  440.  
  441. modules = stmt.executeQuery("SELECT * FROM Module WHERE moduleID = " + moduleID + ";");
  442. }
  443.  
  444. System.out.println("Enter the year in the format 2010-11:");
  445. String year = scanny.nextLine();
  446.  
  447. while(!validYear(year))
  448. {
  449. System.out.println("Please enter a valid year in the form 2010-11:");
  450. year = scanny.nextLine();
  451. }
  452.  
  453. System.out.println("Enter the type ID:");
  454. String typeID = scanny.nextLine();
  455.  
  456. while(!isInteger(typeID))
  457. {
  458. System.out.println("Please enter a valid type ID in integer form:");
  459. typeID = scanny.nextLine();
  460. }
  461.  
  462. ResultSet types = stmt.executeQuery("SELECT * FROM Type WHERE typeID = " + typeID + ";");
  463.  
  464. while(!types.next())
  465. {
  466. System.out.println("Type does not exist. Please enter a valid type ID:");
  467. typeID = scanny.nextLine();
  468.  
  469. while(!isInteger(typeID))
  470. {
  471. System.out.println("Please enter a valid type ID in integer form:");
  472. typeID = scanny.nextLine();
  473. }
  474.  
  475. types = stmt.executeQuery("SELECT * FROM Type WHERE typeID = " + typeID + ";");
  476. }
  477.  
  478. System.out.println("Enter the mark:");
  479. String mark = scanny.nextLine();
  480.  
  481. while(!isInteger(mark) || Integer.parseInt(mark) < 0 || Integer.parseInt(mark) > 100)
  482. {
  483. System.out.println("Please enter an integer mark between 0 and 100:");
  484. mark = scanny.nextLine();
  485. }
  486.  
  487. System.out.println("Enter any notes you wish to make:");
  488. String notes = scanny.nextLine();
  489.  
  490. if(notes.length() > 100)
  491. {
  492. System.out.println("Please do not exceed 100 characters:");
  493. notes = scanny.nextLine();
  494. }
  495.  
  496. int n = -1;
  497. System.out.println("Insert into Marks VALUES ('" + year + "', " + mark + ", '" + notes + "', " + typeID + ", " + moduleID + ", " + studentID + ");");
  498. n = stmt.executeUpdate("Insert into Marks VALUES ('" + year + "', " + mark + ", '" + notes + "', " + typeID + ", " + moduleID + ", " + studentID + ");");
  499. System.out.println("Mark recorded");
  500. }
  501.  
  502. public static void newStudent(Connection conn) throws SQLException
  503. {
  504. System.out.println("Enter the student's forename:");
  505. String forename = scanny.nextLine();
  506.  
  507. while(forename.trim().isEmpty() || forename.length() > 20 || forename.matches(".*\\d.*"))
  508. {
  509. System.out.println("Forename cannot be null, contain integer values or exceed 20 characters. Please enter it again:");
  510. forename = scanny.nextLine();
  511. }
  512.  
  513. System.out.println("Enter the student's family name:");
  514. String surname = scanny.nextLine();
  515.  
  516. while(surname.trim().isEmpty() || surname.length() > 20 || surname.matches(".*\\d.*"))
  517. {
  518. System.out.println("Family name cannot be null, contain integer values or exceed 20 characters. Please enter it again:");
  519. surname = scanny.nextLine();
  520. }
  521.  
  522. System.out.println("Enter the student's date of birth in the format YYYY-MM-DD");
  523. String dob = scanny.nextLine();
  524.  
  525. while(!dob.matches("([0-9]{4})-([0-9]{2})-([0-9]{2})") || !vDate(dob))
  526. {
  527. System.out.println("Date of birth must be in the format YYYY-MM-DD, and must not be in the future. Please enter it again:");
  528. dob = scanny.nextLine();
  529. }
  530.  
  531. System.out.println("Enter the student's corresponding title ID:");
  532. String titleID = scanny.nextLine();
  533.  
  534. while(!isInteger(titleID))
  535. {
  536. System.out.println("Please enter a valid title ID in integer form:");
  537. titleID = scanny.nextLine();
  538. }
  539.  
  540. PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Titles WHERE titleID = " + titleID + ";");
  541.  
  542.  
  543. ResultSet titles = conn.prepareStatement("SELECT * FROM Titles WHERE titleID = " + titleID + ";").executeQuery();
  544.  
  545. while(!titles.next())
  546. {
  547. System.out.println("Title does not exist. Please enter a valid title ID:");
  548. titleID = scanny.nextLine();
  549.  
  550. while(!isInteger(titleID))
  551. {
  552. System.out.println("Please enter a valid title ID in integer form:");
  553. titleID = scanny.nextLine();
  554. }
  555.  
  556. titles = stmt.executeQuery("SELECT * FROM Titles WHERE titleID = " + titleID + ";");
  557. }
  558.  
  559. ResultSet maxID = conn.prepareStatement("SELECT MAX(studentID) FROM Student;").executeQuery();
  560. int max=0;
  561. while(maxID.next()) {
  562. max = maxID.getInt("max");
  563. }
  564.  
  565. stmt = conn.prepareStatement("INSERT INTO Student VALUES(" + (max+1) + ", " + titleID + ", '" + forename + "', '" + surname + "', '" + dob + "');");
  566. stmt.execute();
  567.  
  568. //int n = -1;
  569.  
  570. //int Year = Integer.parseInt(dobYear)
  571. //int Month = Integer.parseInt(month);
  572. //int Day = Integer.parseInt(day);
  573. //String dateofBirth = Year+"-"+Month+"-"+Day;
  574. /*System.out.println("Insert into Student VALUES(" + (max+1) + ", " + titleID + ", '" + forename + "', '" + surname + "', '" + dob + "');");
  575. stmt.execute("INSERT INTO Student VALUES(" + (max+1) + ", " + titleID + ", '" + forename + "', '" + surname + "', '" + dob + "');");
  576. stmt.execute("INSERT INTO StudentContact VALUES (" + (max+1) + ", 'student" + (max+1) + "@bham.student.com', 'student" + (max+1) + " street');");
  577. stmt.execute("INSERT INTO NextOfKinContact VALUES (" + (max+1) + ", 'nextOfKin" + (max+1) + "@bham.kin.com', 'nextOfKin" + (max+1) + " street');");
  578. /*String SQLcommand1 = "INSERT INTO Student ( studentID, titleID, forename, surname, dob) " +
  579. "VALUES ("+ (max+1)+", "+ titleID + ", '" + forename + "', '" + surname + "', '" + dob + "');";*/
  580.  
  581.  
  582. System.out.println("Student added");
  583.  
  584. }
  585.  
  586. public static void produceTranscript(Connection conn) throws SQLException
  587. {
  588. System.out.println("Please enter a student's ID to receive their transcript:");
  589. String studentID = scanny.nextLine();
  590.  
  591. while(!isInteger(studentID))
  592. {
  593. System.out.println("Please enter a valid student ID in integer form:");
  594. studentID = scanny.nextLine();
  595. }
  596.  
  597. Statement stmt = conn.createStatement();
  598.  
  599. ResultSet students = stmt.executeQuery("SELECT * FROM Student WHERE studentID = " + studentID + ";");
  600.  
  601. while(!students.next())
  602. {
  603. System.out.println("Student does not exist. Please enter a valid student ID:");
  604. studentID = scanny.nextLine();
  605.  
  606. while(!isInteger(studentID))
  607. {
  608. System.out.println("Please enter a valid student ID in integer form:");
  609. studentID = scanny.nextLine();
  610. }
  611.  
  612. students = stmt.executeQuery("SELECT * FROM Student WHERE studentID = " + studentID + ";");
  613. }
  614.  
  615. int titleID = students.getInt("titleID");
  616. String forename = students.getString("forename").trim();
  617. String surname = students.getString("familyName").trim();
  618. String dob = students.getString("dateOfBirth");
  619.  
  620. ResultSet titles = stmt.executeQuery("SELECT * FROM Titles WHERE titleID = " + titleID + ";");
  621. titles.next();
  622. String title = titles.getString("titleString").trim();
  623.  
  624. Statement marksstatement = conn.createStatement();
  625.  
  626. ResultSet marks = marksstatement.executeQuery("SELECT * FROM Marks WHERE studentID = " + studentID + ";");
  627. if(marks.next())
  628. {
  629. String year = marks.getString("year").trim();
  630. int moduleID = marks.getInt("moduleID");
  631. int mark = marks.getInt("mark");
  632. String notes = marks.getString("notes");
  633. int typeID = marks.getInt("typeID");
  634.  
  635. ResultSet modules = stmt.executeQuery("SELECT * FROM Module WHERE moduleID = " + moduleID + ";");
  636. modules.next();
  637.  
  638. String moduleName = modules.getString("moduleName").trim();
  639.  
  640. ResultSet types = stmt.executeQuery("SELECT * FROM Type WHERE typeID = " + typeID + ";");
  641. types.next();
  642.  
  643. String type = types.getString("typeString");
  644.  
  645. System.out.println("Student ID: " + studentID);
  646. System.out.println(title + ". " + forename + " " + surname);
  647. System.out.println("Born on " + dob);
  648. System.out.println();
  649. System.out.println(year + ":");
  650. System.out.println(moduleName + ", " + moduleID);
  651. System.out.println("Mark: " + mark);
  652. System.out.println("Type: " + type);
  653.  
  654. while(marks.next())
  655. {
  656. year = marks.getString("year").trim();
  657. moduleID = marks.getInt("moduleID");
  658. mark = marks.getInt("mark");
  659. notes = marks.getString("notes");
  660. typeID = marks.getInt("typeID");
  661.  
  662. modules = stmt.executeQuery("SELECT * FROM Module WHERE moduleID = " + moduleID + ";");
  663. modules.next();
  664.  
  665. moduleName = modules.getString("moduleName").trim();
  666.  
  667. types = stmt.executeQuery("SELECT * FROM Type WHERE typeID = " + typeID + ";");
  668. types.next();
  669.  
  670. type = types.getString("typeString");
  671.  
  672. System.out.println();
  673. System.out.println(year + ":");
  674. System.out.println(moduleName + ", " + moduleID);
  675. System.out.println("Mark: " + mark);
  676. System.out.println("Type: " + type);
  677. }
  678. }
  679. else
  680. {
  681. System.out.println("Student ID: " + studentID);
  682. System.out.println(title + ". " + forename + " " + surname);
  683. System.out.println("Born on " + dob);
  684. System.out.println();
  685. System.out.println("No marks found");
  686. }
  687. }
  688. public static void produceReport(Connection conn) throws SQLException
  689. {
  690. System.out.println("Enter a year in the form 2010-11:");
  691. String year = scanny.nextLine();
  692.  
  693. while(!validYear(year))
  694. {
  695. System.out.println("Please enter a valid year in the form 2010-11:");
  696. year = scanny.nextLine();
  697. }
  698.  
  699. System.out.println("Enter a module ID:");
  700. String moduleID = scanny.nextLine();
  701.  
  702. while(!isInteger(moduleID))
  703. {
  704. System.out.println("Please enter a valid module ID in integer form:");
  705. moduleID = scanny.nextLine();
  706. }
  707.  
  708. Statement mark = conn.createStatement();
  709.  
  710. ResultSet marks = mark.executeQuery("SELECT * FROM Marks WHERE year = '" + year + "' AND moduleID = " + moduleID + ";");
  711.  
  712. if(!marks.next())
  713. {
  714. System.out.println("No marks exist from the given year and module.");
  715. }
  716. else
  717. {
  718. Statement module = conn.createStatement();
  719. ResultSet modules = module.executeQuery("SELECT * FROM Module WHERE moduleID = " + moduleID + ";");
  720. modules.next();
  721. String moduleName = modules.getString("moduleName");
  722. String lecturerID = modules.getString("lecturerID");
  723.  
  724. Statement lecturer = conn.createStatement();
  725. ResultSet lecturers = lecturer.executeQuery("SELECT * FROM Lecturer WHERE lecturerID = " + lecturerID + ";");
  726. lecturers.next();
  727. String titleID = lecturers.getString("titleID");
  728. String foreName = lecturers.getString("foreName");
  729. String familyName = lecturers.getString("familyName");
  730.  
  731. Statement noOfStudent = conn.createStatement();
  732. ResultSet noOfStudents = noOfStudent.executeQuery("SELECT COUNT (DISTINCT studentID) FROM Marks WHERE year ='" + year + "' AND moduleID = " + moduleID + ";");
  733. noOfStudents.next();
  734.  
  735. int number = Integer.parseInt(noOfStudents.getString("count"));
  736.  
  737. Statement marksum = conn.createStatement();
  738. ResultSet marksums = marksum.executeQuery("SELECT SUM(mark) FROM Marks WHERE year = '" + year + "' AND moduleID = " + moduleID + ";");
  739. marksums.next();
  740.  
  741. String sum = marksums.getString("sum");
  742. int average = Integer.parseInt(sum) / number;
  743.  
  744. Statement fail = conn.createStatement();
  745. ResultSet fails = fail.executeQuery("SELECT COUNT(mark) FROM Marks WHERE year = '" + year + "' AND moduleID = " + moduleID + " AND mark < 40;");
  746. fails.next();
  747. int failures = Integer.parseInt(fails.getString("count"));
  748.  
  749. float percentage = (((float)failures / (float)number) * 100);
  750.  
  751. Statement title = conn.createStatement();
  752. ResultSet titles = title.executeQuery("SELECT * FROM Titles WHERE titleID = " + titleID + ";");
  753. titles.next();
  754. String lectTitle = titles.getString("titleString");
  755.  
  756. System.out.println();
  757. System.out.println("Module ID: " + moduleID);
  758. System.out.println("Module name: " + moduleName);
  759. System.out.println("Lecturer: " + lectTitle.trim() + ". " + foreName.trim() + " " + familyName.trim());
  760. System.out.println("Number of students with marks recorded: " + number);
  761. System.out.println("Average mark: " + average);
  762. System.out.println("Number of failures: " + failures);
  763. System.out.println("Percentage of failures: " + percentage + "%");
  764. System.out.println();
  765. }
  766. }
  767. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement