Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DB {
- ComboPooledDataSource dataSource;
- HttpServletResponse response;
- public Connection conn;
- public static DB db = new DB();
- private DB(){
- ComboPooledDataSource dataSource = new ComboPooledDataSource();
- try {
- dataSource.setDriverClass("com.mysql.jdbc.Driver");
- String db = System.getProperty("RDS_DB_NAME");
- String username = System.getProperty("RDS_USERNAME");
- String password = System.getProperty("RDS_PASSWORD");
- String hostname = System.getProperty("RDS_HOSTNAME");
- String port = System.getProperty("RDS_PORT");
- String jdbcURL = "jdbc:mysql://" + hostname + ":" + port + "/" + db + "?user=" + username + "&password=" + password;
- dataSource.setJdbcUrl(jdbcURL);
- this.dataSource = dataSource;
- } catch (PropertyVetoException e) {
- e.printStackTrace();
- }
- }
- /**
- * Get database name
- * @return Databse name
- */
- public static String dbN(){
- return DATABASE;
- }
- /**
- * Gets the database connection
- * @return Database connection
- * @throws SQLException
- */
- public Connection getDatabase() throws SQLException{
- return dataSource.getConnection();
- }}
- @GET
- @Path("/{session}")
- @Produces("application/json")
- public Response getAll(@PathParam("session") String session){
- JSONArray response = new JSONArray();
- Connection conn = null;
- PreparedStatement prepared = null;
- ResultSet rs = null;
- try {
- conn = DB.db.getDatabase();
- prepared = conn.prepareStatement("SQL STATEMENT");
- prepared.setString(1, session);
- prepared.setTimestamp(2, new Timestamp(Time.current()));
- rs = prepared.executeQuery();
- while(rs.next()){
- JSONObject obj = new JSONObject();
- obj.put("id", rs.getInt("id"));
- obj.put("name", rs.getString("name"));
- obj.put("color", rs.getString("color"));
- response.put(obj);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- return Response.status(500).build();
- } finally{
- try{
- prepared.close();
- rs.close();
- conn.close();
- }
- catch (SQLException e){
- e.printStackTrace();
- }
- }
- return Response.status(200).entity(response.toString()).build();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement