Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package relations;
- import java.sql.*;
- import java.util.Scanner;
- public class MainAppUpdate {
- private static final String GET_MOVIES_QUERY =
- "SELECT id, name FROM movies";
- private static final String DELETE_MOVIE_QUERY =
- "DELETE FROM movies WHERE id = ?";
- public static void main(String[] args) throws SQLException {
- Scanner scanner = new Scanner(System.in);
- try(Connection connection = createConnection("cinemas_ex");
- PreparedStatement getMoviesStatement = connection.prepareStatement(GET_MOVIES_QUERY);
- PreparedStatement deleteMovieStatement = connection.prepareStatement(DELETE_MOVIE_QUERY);
- ResultSet resultSet = getMoviesStatement.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));
- }
- System.out.println("Select movie to delete (insert id): ");
- int movieId = scanner.nextInt();
- deleteMovieStatement.setInt(1, movieId);
- System.out.println("Number of deleted movies: " + deleteMovieStatement.executeUpdate());
- }
- }
- private static Connection createConnection(String databaseName) throws SQLException {
- return DriverManager.getConnection("jdbc:mysql://localhost:3306/"+databaseName,
- "root","admin");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement