Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 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.  
  35. // Register a student on a course, returns a tiny JSON document (as a String)
  36. public String register(String student, String courseCode){
  37.  
  38. try (PreparedStatement s = conn.prepareStatement(
  39. "SELECT idnr,name FROM BasicInformation");){
  40. ResultSet rs = s.executeQuery();
  41.  
  42. while(rs.next()){
  43. String id = rs.getString(1);
  44. String name = rs.getString(2);
  45. System.out.println(id + " " + name);
  46. }
  47. return "{\"success\":true}";
  48. }
  49. catch (SQLException e) {
  50. return "{\"success\":false, \"error\":\""+getError(e)+"\"}";
  51. }
  52.  
  53. }
  54.  
  55. public String RegistrationCheck() {
  56. try (PreparedStatement ps = conn.prepareStatement(
  57. "SELECT * FROM Registered WHERE student=? AND course=?");){
  58. ps.setString(1, "1111111111");
  59. ps.setString(2, "CCC111");
  60. ResultSet rs = ps.executeQuery();
  61. if(rs.next()) {
  62. System.out.println("Yes, you are registered :)");
  63. return "{\"success\":true}";
  64. }else {
  65. System.out.println("No, you are not registered :(");
  66. return "{\"success\":false}";
  67. }
  68. }
  69. catch (SQLException e) {
  70. return "{\"success\":false, \"error\":\""+getError(e)+"\"}";
  71. }
  72. }
  73.  
  74. // Unregister a student from a course, returns a tiny JSON document (as a String)
  75. public String unregister(String student, String courseCode){
  76.  
  77. try(PreparedStatement ps = conn.prepareStatement(
  78. "DELETE FROM Registered WHERE student=? AND course=?");){
  79. System.out.println(ps);
  80.  
  81. ps.setString(1,student);
  82. ps.setString(2,courseCode); // Denna och den över kan ersättas med user input.
  83.  
  84. System.out.println("query is: " + ps);
  85. int r = ps.executeUpdate();
  86. }
  87. catch (SQLException e) {
  88. return "{\"success\":false, \"error\":\""+getError(e)+"\"}";
  89. }
  90. return "{\"success\":false, \"error\":\"Unregistration is not implemented yet :(\"}";
  91. }
  92.  
  93. // Return a JSON document containing lots of information about a student, it should validate against the schema found in information_schema.json
  94. public String getInfo(String student) throws SQLException{
  95.  
  96. try(PreparedStatement st = conn.prepareStatement(
  97. // replace this with something more useful
  98. "SELECT jsonb_build_object('student',idnr,'name',name) AS jsondata FROM BasicInformation WHERE idnr=?"
  99. );){
  100.  
  101. st.setString(1, student);
  102.  
  103. ResultSet rs = st.executeQuery();
  104.  
  105. if(rs.next())
  106. return rs.getString("jsondata");
  107. else
  108. return "{\"student\":\"does not exist :(\"}";
  109.  
  110. }
  111. }
  112.  
  113. // This is a hack to turn an SQLException into a JSON string error message. No need to change.
  114. public static String getError(SQLException e){
  115. String message = e.getMessage();
  116. int ix = message.indexOf('\n');
  117. if (ix > 0) message = message.substring(0, ix);
  118. message = message.replace("\"","\\\"");
  119. return message;
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement