Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1.  
  2. import java.sql.*; // JDBC stuff.
  3. import java.util.Properties;
  4.  
  5. public class PortalConnection {
  6.  
  7. // For connecting to the portal database on your local machine
  8. static final String DATABASE = "jdbc:postgresql://localhost/portal";
  9. static final String USERNAME = "postgres";
  10. static final String PASSWORD = "postgres";
  11.  
  12. // For connecting to the chalmers database server (from inside chalmers)
  13. // static final String DATABASE = "jdbc:postgresql://ate.ita.chalmers.se/";
  14. // static final String USERNAME = "tda357_nnn";
  15. // static final String PASSWORD = "yourPasswordGoesHere";
  16.  
  17.  
  18. // This is the JDBC connection object you will be using in your methods.
  19. private Connection conn;
  20.  
  21. public PortalConnection() throws SQLException, ClassNotFoundException {
  22. this(DATABASE, USERNAME, PASSWORD);
  23. }
  24.  
  25. // Initializes the connection, no need to change anything here
  26. public PortalConnection(String db, String user, String pwd) throws SQLException, ClassNotFoundException {
  27. Class.forName("org.postgresql.Driver");
  28. Properties props = new Properties();
  29. props.setProperty("user", user);
  30. props.setProperty("password", pwd);
  31. conn = DriverManager.getConnection(db, props);
  32. }
  33.  
  34. // Register a student on a course, returns a tiny JSON document (as a String)
  35. public String register(String student, String courseCode){
  36.  
  37. try {
  38. PreparedStatement ps = conn.prepareStatement("INSERT INTO Registrations VALUES(?, ?)");
  39. ps.setString(1,student);
  40. ps.setString(2,courseCode);
  41. ps.executeUpdate();
  42. } catch (SQLException e) {
  43. return "{\"success\":false, \"error\":\""+getError(e)+"\"}";
  44. }
  45. //what does success \false mean? Make this statement shorter
  46. return "{\"success\":true, \"error\":\"Student registered successfully :)\"}";
  47. }
  48. /*
  49. // Unregister a student from a course, returns a tiny JSON document (as a String)
  50. public String unregister(String student, String courseCode){
  51.  
  52. try{
  53. PreparedStatement ps = conn.prepareStatement("SELECT COUNT(*) FROM Registrations WHERE student = ? AND course = ?");
  54. ps.setString(1, student);
  55. ps.setString(2, courseCode);
  56. ResultSet rs = ps.executeQuery();
  57.  
  58. if(rs.next() && rs.getInt(1) >= 1){
  59. try {
  60. PreparedStatement pps = conn.prepareStatement("DELETE FROM Registrations WHERE student = ? AND course = ?");
  61. pps.setString(1,student);
  62. pps.setString(2,courseCode);
  63. pps.executeUpdate();
  64. } catch (SQLException e) {
  65. return "{\"success\":false, \"error\":\""+getError(e)+"\"}";
  66. }
  67. } else {
  68. return "{\"success\":false, \"error\":\"Student is not registered :)\"}";
  69. }
  70. } catch (SQLException e) {
  71. return "{\"success\":false, \"error\":\""+getError(e)+"\"}";
  72. }
  73. //no error msg with incomplete unregistrations
  74. return "{\"success\":true, \"error\":\"Student unregistered successfully :)\"}";
  75. }
  76. */
  77. // Unregister a student from a course, HACK EDITION
  78. public String unregister(String student, String courseCode){
  79.  
  80. String sid = student;
  81. String code = courseCode;
  82. String query = "DELETE FROM Registered WHERE student ='"+sid+"' AND course = '"+code+"'";
  83.  
  84. try{
  85. Statement s = conn.createStatement();
  86. int r = s.executeUpdate(query);
  87. System.out.println("Deleted " + r + " registrations");
  88.  
  89. } catch (SQLException e) {
  90. return "{\"success\":false, \"error\":\""+getError(e)+"\"}";
  91. }
  92. //no error msg with incomplete unregistrations
  93. return "{\"success\":true, \"error\":\"Student unregistered successfully :)\"}";
  94. }
  95.  
  96. // Return a JSON document containing lots of information about a student, it should validate against the schema found in information_schema.json
  97. public String getInfo(String student) throws SQLException{
  98.  
  99. try(PreparedStatement st = conn.prepareStatement(
  100. "SELECT jsonb_build_object(" +
  101. " 'student', idnr," +
  102. " 'name', name," +
  103. " 'login', login," +
  104. " 'program', program," +
  105. " 'branch', branch," +
  106. " 'finished', (SELECT COALESCE(jsonb_agg(jsonb_build_object( " +
  107. " 'course', (SELECT name FROM Courses WHERE code = course)," +
  108. " 'code', course," +
  109. " 'credits', (SELECT credits FROM Courses WHERE code = course)," +
  110. " 'grade', grade)" +
  111. " ), '[]') FROM Taken WHERE (student = BasicInformation.idnr))," +
  112. " 'registered', (SELECT COALESCE(jsonb_agg(jsonb_build_object(" +
  113. " 'course', (SELECT name FROM Courses WHERE code = course)," +
  114. " 'code', course," +
  115. " 'status', status," +
  116. " 'position', (SELECT position FROM WaitingList WHERE course = Registrations.course AND student = Registrations.student))" +
  117. " ), '[]') FROM Registrations WHERE (student = BasicInformation.idnr))," +
  118. " 'seminarCourses', (SELECT seminarCourses FROM PathtoGraduation WHERE student = BasicInformation.idnr)," +
  119. " 'mathCredits', (SELECT mathCredits FROM PathtoGraduation WHERE student = BasicInformation.idnr)," +
  120. " 'researchCredits', (SELECT researchCredits FROM PathtoGraduation WHERE student = BasicInformation.idnr)," +
  121. " 'totalCredits', (SELECT totalCredits FROM PathtoGraduation WHERE student = BasicInformation.idnr)," +
  122. " 'canGraduate', (SELECT qualified FROM PathtoGraduation WHERE student = BasicInformation.idnr) " +
  123. " ) as jsondata" +
  124. " FROM BasicInformation WHERE idnr = ?;"
  125.  
  126. );){
  127.  
  128. st.setString(1, student);
  129.  
  130. ResultSet rs = st.executeQuery();
  131.  
  132. if(rs.next())
  133. return rs.getString("jsondata");
  134. else
  135. return "{\"student\":\"does not exist :(\"}";
  136. } catch (SQLException e) {
  137. return "{\"success\":false, \"error\":\""+getError(e)+"\"}";
  138. }
  139.  
  140. }
  141.  
  142. // This is a hack to turn an SQLException into a JSON string error message. No need to change.
  143. public static String getError(SQLException e){
  144. String message = e.getMessage();
  145. int ix = message.indexOf('\n');
  146. if (ix > 0) message = message.substring(0, ix);
  147. message = message.replace("\"","\\\"");
  148. return message;
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement