Advertisement
Guest User

Untitled

a guest
May 18th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 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. public class Main5 {
  8.  
  9. //Movie 1
  10. //cinema 1
  11. //cinema 2
  12. //cinema 3
  13. //Mocie 2
  14. //cinema 1
  15. //cinema 2
  16. //cinema 3
  17.  
  18. private static final String GET_MOVIES = "SELECT * FROM movies";
  19.  
  20. private static final String GET_CINEMAS_PLAYING_MOVIE = "SELECT * FROM cinemas JOIN screenings ON cinemas.id=screenings.cinemas_id JOIN movies ON movies.id = screenings.movies_id WHERE movies.id = ?;";
  21.  
  22.  
  23.  
  24. public static void main(String[] args) throws SQLException {
  25. try (Connection connection = createConnection();
  26. PreparedStatement getMovies = connection.prepareStatement(GET_MOVIES);
  27. PreparedStatement getCinemasPlayingMovie = connection.prepareStatement(GET_CINEMAS_PLAYING_MOVIE);
  28. ResultSet resultSet = getMovies.executeQuery();
  29. ) {
  30. while (resultSet.next()) {
  31. int id = resultSet.getInt("id");
  32. String name = resultSet.getString("name");
  33. System.out.println(String.format("Movie id: %d, name: %s", id, name));
  34. printProductsForOrder(id, getCinemasPlayingMovie);
  35. }
  36.  
  37. }
  38. }
  39. private static Connection createConnection() throws SQLException {
  40. return DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex",
  41. "root", "coderslab");
  42. }
  43. private static void printProductsForOrder(int id, PreparedStatement getProductsStatement) throws SQLException {
  44. getProductsStatement.setInt(1, id);
  45. try(ResultSet resultSet = getProductsStatement.executeQuery()){
  46. while (resultSet.next()){
  47. String name = resultSet.getString("name");
  48. int cinemaId = resultSet.getInt("id");
  49. System.out.println(String.format("*\tCinema id: %d, movie: %s", cinemaId, name));
  50. }
  51. }
  52. }
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement