Advertisement
Guest User

Untitled

a guest
May 18th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6.  
  7.  
  8. //Cinema 1
  9. //movie 1
  10. //movie 2
  11. //Cinema 2
  12. //movie 1
  13. //movie 2
  14. public class Main4 {
  15.  
  16. public static final String GET_CINEMAS = "SELECT * FROM cinemas";
  17.  
  18. public static final String GET_MOVIES_IN_CINEMA = "SELECT * FROM cinemas JOIN screenings ON cinemas.id=screenings.cinemas_id JOIN movies ON movies.id = screenings.movies_id WHERE cinemas.id = ?;";
  19.  
  20. public static void main(String[] args) throws SQLException {
  21. try (Connection connection = createConnection();
  22. PreparedStatement getCinemas = connection.prepareStatement(GET_CINEMAS);
  23. PreparedStatement getMoviesPlayed = connection.prepareStatement(GET_MOVIES_IN_CINEMA);
  24. ResultSet resultSet = getCinemas.executeQuery();
  25. ) {
  26. while (resultSet.next()) {
  27. int id = resultSet.getInt("id");
  28. String name = resultSet.getString("name");
  29. System.out.println(String.format("Cinema id: %d, description: %s", id, name));
  30. printMoviesInCinema(id, getMoviesPlayed);
  31. }
  32.  
  33. }
  34. }
  35.  
  36. private static Connection createConnection() throws SQLException {
  37. return DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex",
  38. "root", "coderslab");
  39. }
  40.  
  41. private static void printMoviesInCinema(int id, PreparedStatement getProductsStatement) throws SQLException {
  42. getProductsStatement.setInt(1, id);
  43. try(ResultSet resultSet = getProductsStatement.executeQuery()){
  44. while (resultSet.next()){
  45. String name = resultSet.getString("movies.name"); //movies.name, a nie samo name, bo w cienemas tez jest naem i nie lapie tego co na zlapac
  46. int movieId = resultSet.getInt("id");
  47. System.out.println(String.format("*\tMovie id: %d, name: %s", movieId, name));
  48. }
  49. }
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement