Guest User

Untitled

a guest
Jan 13th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. @Override
  2. public Response getAllStaff() {
  3.  
  4. ArrayList<StaffInfo> staffArray = new ArrayList<>();
  5. Response response = null;
  6. String url = null;
  7.  
  8. try {
  9.  
  10. if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
  11. Class.forName("com.mysql.jdbc.GoogleDriver").newInstance();
  12. url = "jdbc:google:mysql://{project-id}:staff-database/staffDatabase";
  13. }
  14.  
  15. // Connection conn = DriverManager.getConnection(
  16. // "jdbc:google:mysql://your-project-id:your-instance- name/database",
  17. // "user", "password");
  18.  
  19. Connection conn = DriverManager.getConnection(url, "michael", "password");
  20.  
  21. Statement st = conn.createStatement();
  22. String sql = ("SELECT * FROM StaffTable;");
  23. ResultSet rs = st.executeQuery(sql);
  24.  
  25.  
  26. while(rs.next()){
  27. staffArray.add(new StaffInfo(rs.getInt(0),
  28. rs.getString(1),
  29. rs.getString(2),
  30. rs.getString(3),
  31. rs.getString(4),
  32. rs.getString(5)));
  33. }
  34.  
  35. GenericEntity<ArrayList<StaffInfo>> entity =
  36. new GenericEntity<ArrayList<StaffInfo>>(staffArray) {};
  37.  
  38. response = Response.ok(entity).build();
  39. // response = Response.ok(entity, MediaType)
  40.  
  41. conn.close();
  42. } catch (ClassNotFoundException e) {
  43. // Could not find the database driver
  44. } catch (SQLException e) {
  45. // Could not connect to the database
  46. } catch (InstantiationException e) {
  47. // TODO Auto-generated catch block
  48. e.printStackTrace();
  49. } catch (IllegalAccessException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53.  
  54.  
  55. return response;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment