Advertisement
Guest User

Untitled

a guest
May 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. package relations;
  2.  
  3. import java.sql.*;
  4. import java.util.Scanner;
  5.  
  6. public class MainAppUpdate {
  7.  
  8. private static final String GET_MOVIES_QUERY =
  9. "SELECT id, name FROM movies";
  10. private static final String DELETE_MOVIE_QUERY =
  11. "DELETE FROM movies WHERE id = ?";
  12. public static void main(String[] args) throws SQLException {
  13. Scanner scanner = new Scanner(System.in);
  14. try(Connection connection = createConnection("cinemas_ex");
  15. PreparedStatement getMoviesStatement = connection.prepareStatement(GET_MOVIES_QUERY);
  16. PreparedStatement deleteMovieStatement = connection.prepareStatement(DELETE_MOVIE_QUERY);
  17. ResultSet resultSet = getMoviesStatement.executeQuery();){
  18. while (resultSet.next()){
  19. int id = resultSet.getInt("id");
  20. String name = resultSet.getString("name");
  21. System.out.println(String.format("Movie: id: %d, name: %s", id, name));
  22. }
  23. System.out.println("Select movie to delete (insert id): ");
  24. int movieId = scanner.nextInt();
  25. deleteMovieStatement.setInt(1, movieId);
  26. System.out.println("Number of deleted movies: " + deleteMovieStatement.executeUpdate());
  27. }
  28. }
  29.  
  30. private static Connection createConnection(String databaseName) throws SQLException {
  31. return DriverManager.getConnection("jdbc:mysql://localhost:3306/"+databaseName,
  32. "root","admin");
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement