Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Scanner;
  8.  
  9. public class Main5 {
  10.  
  11. private static final String GET_CINEMAS_QUERY="SELECT*FROM cinemas";
  12. private static final String UPDATE_CINEMA_QUERY="UPDATE cinemas SET name=?, address=? WHERE id=?";
  13. private static final String DELETE_CINEMA_QUERY="DELETE FROM cinemas WHERE id=?";
  14. private static final String MOVIE_CINEMAS_TEMPLATE = "Cinema: id:%d name:%s, address:%s";
  15.  
  16. public static void main(String[] args) {
  17. String url = "jdbc:mysql://localhost:3306/cinemas_ex";
  18. Scanner scan=new Scanner(System.in);
  19. try(Connection connection=getConnection(url)){
  20. printCinemas(connection);
  21. System.out.println("Edycja: wpisz e, usunięcie: wpisz u, wyjście z programu: wpisz x");
  22. String input=scan.next();
  23.  
  24. while(!input.equals("x")) {
  25. if(input.equals("e")) {
  26.  
  27. System.out.println("podaj numer wiersza:");
  28. int toEdit=scan.nextInt();
  29. System.out.println("podaj nową nazwę:");
  30. scan.nextLine();
  31. String name=scan.nextLine();
  32. System.out.println("podaj nowy adres");
  33. String address=scan.nextLine();
  34. System.out.println( toEdit+ " "+ name+" "+address);
  35. updateCinema(connection, toEdit,name,address);
  36. printCinemas(connection);
  37. }else if(input.equals("u")) {
  38. System.out.println("czy na pewno chcesz usunąć wiersz? tak wpisz T, nie wpisz N");
  39. if(scan.next().equals("T")){
  40. System.out.println("podaj numer wiersza:");
  41. int toDelete=scan.nextInt();
  42. deleteMovie(connection,toDelete);
  43. printCinemas(connection);
  44.  
  45. }else {
  46. System.out.println("Edycja: wpisz e, usunięcie: wpisz u, wyjście z programu: wpisz x");
  47. scan.next();
  48.  
  49. }
  50. }else {
  51. System.out.println("Edycja: wpisz e, usunięcie: wpisz u, wyjście z programu: wpisz x");
  52. }
  53. input=scan.next();
  54. }
  55. if(input.equals("x")) {
  56. System.out.println("exit");
  57. }
  58.  
  59.  
  60. }catch (SQLException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64.  
  65. private static Connection getConnection(String url) throws SQLException {
  66. return DriverManager.getConnection(url, "root", "coderslab");
  67. }
  68.  
  69. private static void updateCinema(Connection connection, int id, String name, String address) throws SQLException {
  70. try (PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_CINEMA_QUERY);) {
  71. preparedStatement.setString(1,name);
  72. preparedStatement.setString(2,address);
  73. preparedStatement.setInt(3,id);
  74. int updatedRecords = preparedStatement.executeUpdate();
  75. if (updatedRecords == 1) {
  76. System.out.println("Record with id " + id + " was updated");
  77. } else {
  78. System.out.println("Record was not updated");
  79. }
  80. }
  81. }
  82.  
  83. private static void deleteMovie(Connection connection, int id) throws SQLException {
  84. try (PreparedStatement preparedStatement = connection.prepareStatement(DELETE_CINEMA_QUERY);) {
  85. preparedStatement.setInt(1, id);
  86. int deletedRecords = preparedStatement.executeUpdate();
  87. if (deletedRecords == 1) {
  88. System.out.println("Record with id " + id + " was deleted");
  89. } else {
  90. System.out.println("Record was not deleted");
  91. }
  92. }
  93. }
  94.  
  95. public static void printCinemas(Connection connection) throws SQLException {
  96. try(PreparedStatement stat=connection.prepareStatement(GET_CINEMAS_QUERY)){
  97. try (ResultSet resultSet = stat.executeQuery()) {
  98. while (resultSet.next()) {
  99. int id = resultSet.getInt("id");
  100. String name = resultSet.getString("name");
  101. String address = resultSet.getString("address");
  102. System.out.println(String.format(MOVIE_CINEMAS_TEMPLATE, id, name, address));
  103. }
  104. }
  105.  
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement