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;
- //Cinema 1
- //movie 1
- //movie 2
- //Cinema 2
- //movie 1
- //movie 2
- public class Main4 {
- public static final String GET_CINEMAS = "SELECT * FROM cinemas";
- 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 = ?;";
- public static void main(String[] args) throws SQLException {
- try (Connection connection = createConnection();
- PreparedStatement getCinemas = connection.prepareStatement(GET_CINEMAS);
- PreparedStatement getMoviesPlayed = connection.prepareStatement(GET_MOVIES_IN_CINEMA);
- ResultSet resultSet = getCinemas.executeQuery();
- ) {
- while (resultSet.next()) {
- int id = resultSet.getInt("id");
- String name = resultSet.getString("name");
- System.out.println(String.format("Cinema id: %d, description: %s", id, name));
- printMoviesInCinema(id, getMoviesPlayed);
- }
- }
- }
- private static Connection createConnection() throws SQLException {
- return DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex",
- "root", "coderslab");
- }
- private static void printMoviesInCinema(int id, PreparedStatement getProductsStatement) throws SQLException {
- getProductsStatement.setInt(1, id);
- try(ResultSet resultSet = getProductsStatement.executeQuery()){
- while (resultSet.next()){
- 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
- int movieId = resultSet.getInt("id");
- System.out.println(String.format("*\tMovie id: %d, name: %s", movieId, name));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement