Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*; // JDBC stuff.
- import java.util.Properties;
- public class PortalConnection {
- // For connecting to the portal database on your local machine
- static final String DATABASE = "jdbc:postgresql://localhost/portal";
- static final String USERNAME = "postgres";
- static final String PASSWORD = "postgres";
- // For connecting to the chalmers database server (from inside chalmers)
- // static final String DATABASE = "jdbc:postgresql://ate.ita.chalmers.se/";
- // static final String USERNAME = "tda357_nnn";
- // static final String PASSWORD = "yourPasswordGoesHere";
- // This is the JDBC connection object you will be using in your methods.
- private Connection conn;
- public PortalConnection() throws SQLException, ClassNotFoundException {
- this(DATABASE, USERNAME, PASSWORD);
- }
- // Initializes the connection, no need to change anything here
- public PortalConnection(String db, String user, String pwd) throws SQLException, ClassNotFoundException {
- Class.forName("org.postgresql.Driver");
- Properties props = new Properties();
- props.setProperty("user", user);
- props.setProperty("password", pwd);
- conn = DriverManager.getConnection(db, props);
- }
- // Register a student on a course, returns a tiny JSON document (as a String)
- public String register(String student, String courseCode){
- try (PreparedStatement s = conn.prepareStatement(
- "SELECT idnr,name FROM BasicInformation");){
- ResultSet rs = s.executeQuery();
- while(rs.next()){
- String id = rs.getString(1);
- String name = rs.getString(2);
- System.out.println(id + " " + name);
- }
- return "{\"success\":true}";
- }
- catch (SQLException e) {
- return "{\"success\":false, \"error\":\""+getError(e)+"\"}";
- }
- }
- public String RegistrationCheck() {
- try (PreparedStatement ps = conn.prepareStatement(
- "SELECT * FROM Registered WHERE student=? AND course=?");){
- ps.setString(1, "1111111111");
- ps.setString(2, "CCC111");
- ResultSet rs = ps.executeQuery();
- if(rs.next()) {
- System.out.println("Yes, you are registered :)");
- return "{\"success\":true}";
- }else {
- System.out.println("No, you are not registered :(");
- return "{\"success\":false}";
- }
- }
- catch (SQLException e) {
- return "{\"success\":false, \"error\":\""+getError(e)+"\"}";
- }
- }
- // Unregister a student from a course, returns a tiny JSON document (as a String)
- public String unregister(String student, String courseCode){
- try(PreparedStatement ps = conn.prepareStatement(
- "DELETE FROM Registered WHERE student=? AND course=?");){
- System.out.println(ps);
- ps.setString(1,student);
- ps.setString(2,courseCode); // Denna och den över kan ersättas med user input.
- System.out.println("query is: " + ps);
- int r = ps.executeUpdate();
- }
- catch (SQLException e) {
- return "{\"success\":false, \"error\":\""+getError(e)+"\"}";
- }
- return "{\"success\":false, \"error\":\"Unregistration is not implemented yet :(\"}";
- }
- // Return a JSON document containing lots of information about a student, it should validate against the schema found in information_schema.json
- public String getInfo(String student) throws SQLException{
- try(PreparedStatement st = conn.prepareStatement(
- // replace this with something more useful
- "SELECT jsonb_build_object('student',idnr,'name',name) AS jsondata FROM BasicInformation WHERE idnr=?"
- );){
- st.setString(1, student);
- ResultSet rs = st.executeQuery();
- if(rs.next())
- return rs.getString("jsondata");
- else
- return "{\"student\":\"does not exist :(\"}";
- }
- }
- // This is a hack to turn an SQLException into a JSON string error message. No need to change.
- public static String getError(SQLException e){
- String message = e.getMessage();
- int ix = message.indexOf('\n');
- if (ix > 0) message = message.substring(0, ix);
- message = message.replace("\"","\\\"");
- return message;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement