Advertisement
Guest User

Untitled

a guest
May 18th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. package a_Zadania.a_Dzien_2.a_Zmiana_usuwanie_danych;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Scanner;
  9.  
  10. public class Main5 {
  11.  
  12. private static final String GET_CINEMAS_QUERY =
  13. "SELECT * FROM cinemas";
  14.  
  15. private static final String GET_EDIT_QUERY =
  16. "UPDATE cinemas SET name = ?, adress = ? WHERE id = ?";
  17.  
  18. private static final String GET_DELETE_QUERY =
  19. "DELETE FROM cinemas where id = ?";
  20.  
  21. public static void main(String[] args) throws SQLException {
  22. Scanner scanner = new Scanner(System.in);
  23. try(Connection connection = createConnection("cinemas_ex");
  24. PreparedStatement getCinemasStatement = connection.prepareStatement(GET_CINEMAS_QUERY);
  25. PreparedStatement updateEditStatement = connection.prepareStatement(GET_EDIT_QUERY);
  26. PreparedStatement updateDeleteStatement = connection.prepareStatement(GET_DELETE_QUERY);
  27. ResultSet resultSet = getCinemasStatement.executeQuery();){
  28. while (resultSet.next()){
  29. int id = resultSet.getInt("id");
  30. String name = resultSet.getString("name");
  31. String adress = resultSet.getString("adress");
  32. System.out.println(String.format("Ticket: id: %d name: %s adress:%s", id, name, adress));
  33. }
  34. System.out.println("Jeśli edytować wiersz kliknij e, jeśli usunąć wiersz kliknij u, wyjście x");
  35.  
  36. String choice = scanner.next();
  37.  
  38. while(true){
  39. if(choice.equals("x")){
  40. scanner.close();
  41. break;
  42. } else if(choice.equals("u")){
  43. System.out.println("Który wiersz chcesz usunąć? Podaj id");
  44. int choiceRow = scanner.nextInt();
  45. updateDeleteStatement.setInt(1, choiceRow);
  46. updateDeleteStatement.executeUpdate();
  47. scanner.close();
  48. break;
  49. } else if(choice.equals("e")){
  50. System.out.println("Który wiersz chcesz edytować? Podaj id");
  51. int idToChange = scanner.nextInt();
  52. System.out.println("Podaj nową nazwę a następnie nowy adres");
  53. String newName = scanner.next();
  54. String newAdress = scanner.next();
  55. updateEditStatement.setString(1, newName);
  56. updateEditStatement.setString(2, newAdress);
  57. updateEditStatement.setInt(3, idToChange);
  58. updateEditStatement.executeUpdate();
  59. scanner.close();
  60. break;
  61. }
  62. }
  63.  
  64. }
  65.  
  66.  
  67. }
  68.  
  69.  
  70. private static Connection createConnection(String databaseName) throws SQLException {
  71. return DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex",
  72. "root","coderslab");
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement