Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. package server;
  2.  
  3. import static spark.Spark.*;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.Arrays;
  11.  
  12. public class Server {
  13.    
  14.     private static final String url = "jdbc:postgresql://localhost/test";
  15.     private static final String login = "postgres";
  16.     private static final String password = "12345";
  17.    
  18.     public static void main(String[] args) throws SQLException {
  19.                
  20.         get("/hello/:name", (request, response) -> {
  21.             Connection connection = DriverManager.getConnection(url, login, password);
  22.            
  23.             PreparedStatement ps = connection.prepareStatement(
  24.                     "SELECT g.name, g.price, g.description, g.path_to_image, c.name as category_name "
  25.                     + "FROM goods g "
  26.                     + "LEFT JOIN categories c on g.category_id = c.id "
  27.                     + "where g.id = " +  Integer.valueOf(request.params(":name")));
  28.            
  29.             ResultSet result = ps.executeQuery();
  30.             result.next();
  31.            
  32.             String[] strings = new String[] {
  33.                     result.getString("name"),
  34.                     result.getString("price"),
  35.                     result.getString("description"),
  36.                     result.getString("path_to_image"),
  37.                     result.getString("category_name")};
  38.    
  39.             connection.close();
  40.             return Arrays.toString(strings);
  41.         });
  42.        
  43.         stop();
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement