Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class Main5 {
- //Movie 1
- //cinema 1
- //cinema 2
- //cinema 3
- //Mocie 2
- //cinema 1
- //cinema 2
- //cinema 3
- private static final String GET_MOVIES = "SELECT * FROM movies";
- 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 = ?;";
- public static void main(String[] args) throws SQLException {
- try (Connection connection = createConnection();
- PreparedStatement getMovies = connection.prepareStatement(GET_MOVIES);
- PreparedStatement getCinemasPlayingMovie = connection.prepareStatement(GET_CINEMAS_PLAYING_MOVIE);
- ResultSet resultSet = getMovies.executeQuery();
- ) {
- while (resultSet.next()) {
- int id = resultSet.getInt("id");
- String name = resultSet.getString("name");
- System.out.println(String.format("Movie id: %d, name: %s", id, name));
- printProductsForOrder(id, getCinemasPlayingMovie);
- }
- }
- }
- private static Connection createConnection() throws SQLException {
- return DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex",
- "root", "coderslab");
- }
- private static void printProductsForOrder(int id, PreparedStatement getProductsStatement) throws SQLException {
- getProductsStatement.setInt(1, id);
- try(ResultSet resultSet = getProductsStatement.executeQuery()){
- while (resultSet.next()){
- String name = resultSet.getString("name");
- int cinemaId = resultSet.getInt("id");
- System.out.println(String.format("*\tCinema id: %d, movie: %s", cinemaId, name));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement