Guest User

Untitled

a guest
Nov 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. package a_Zadania.a_Dzien_1.c_Pobieranie_danych;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.Scanner;
  8.  
  9. import com.mysql.jdbc.Connection;
  10.  
  11. public class Main5 {
  12.  
  13. public static void main(String[] args) {
  14. Scanner scan = new Scanner(System.in);
  15. showCinemas();
  16. System.out.println();
  17. System.out.println("Jeśli chcesz edytować wpisz E, jeśli chcesz usunąć wpisz U, jeśli chcesz usunąć wpisz X: ");
  18. String decision = scan.nextLine();
  19.  
  20. if(decision.equalsIgnoreCase("e")) {
  21. changeRecord();
  22. } else if(decision.equalsIgnoreCase("u")) {
  23. deleteRecord();
  24. } else if (decision.equalsIgnoreCase("x")) {
  25. System.out.println("Do widzenia!!!");
  26. }
  27.  
  28.  
  29.  
  30. }
  31.  
  32. public static void showCinemas() {
  33. try (Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex?useSSL=false", "root", "coderslab")){
  34.  
  35. String str = "SELECT * FROM cinemas";
  36. PreparedStatement ps = conn.prepareStatement(str);
  37. ResultSet rs = ps.executeQuery();
  38.  
  39. while (rs.next()) {
  40. String name = rs.getString("name");
  41. int id = rs.getInt("id");
  42. String adres = rs.getString("adress");
  43. System.out.println(id + " " + name + " " + adres);
  44. }
  45.  
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50.  
  51. public static void changeRecord() {
  52. Scanner scan = new Scanner(System.in);
  53. System.out.println();
  54. System.out.print("Podaj id kina które chcesz zmienić: ");
  55. int id = scan.nextInt();
  56. System.out.println();
  57.  
  58. System.out.print("Podaj nazwę kina: ");
  59. String name1 = scan.next();
  60. System.out.println();
  61.  
  62. System.out.print("Podaj nowy adres kina: ");
  63. String adress1 = scan.next();
  64. System.out.println();
  65.  
  66. try (Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex?useSSL=false", "root", "coderslab")){
  67.  
  68. String sql = "UPDATE cinemas SET name = ?, adress = ? WHERE id = ?";
  69. PreparedStatement ps = conn.prepareStatement(sql);
  70. ps.setString(1, name1);
  71. ps.setString(2, adress1);
  72. ps.setInt(3, id);
  73.  
  74. ps.executeUpdate();
  75.  
  76. }catch (SQLException e) {
  77. e.printStackTrace();
  78. }
  79.  
  80. }
  81. public static void deleteRecord() {
  82. Scanner scan = new Scanner(System.in);
  83. System.out.print("Podaj id kina które chcesz usunąć: ");
  84.  
  85. int x = scan.nextInt();
  86.  
  87. try (Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex?useSSL=false", "root", "coderslab")){
  88.  
  89. String sql = "DELETE FROM cinemas WHERE id = ?";
  90. PreparedStatement ps = conn.prepareStatement(sql);
  91. ps.setInt(1, x);
  92.  
  93. ps.executeUpdate();
  94.  
  95. }catch (SQLException e) {
  96. e.printStackTrace();
  97. }
  98.  
  99. }
  100. }
Add Comment
Please, Sign In to add comment