Advertisement
Guest User

Untitled

a guest
May 18th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 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. /**
  11. * W pliku `Main2.java`:
  12.  
  13. 1. Pobierz z bazy listę biletów i wyświetl na konsoli.
  14. 2. Pobierz od użytkownika identyfikator biletu z konsoli.
  15. 3. Pobierz z konsloli ilość sprzedanych biletów.
  16. 4. Zmodyfikuj odpowiedni wiersz w bazie danych.
  17. * @author tomek
  18. *
  19. */
  20.  
  21. public class Main2 {
  22.  
  23. private static final String GET_TICKETS_QUERY =
  24. "SELECT * FROM tickets";
  25. private static final String GET_QUANTITY_QUERY =
  26. "UPDATE tickets SET quantity = quantity + ? WHERE id = ?";
  27. public static void main(String[] args) throws SQLException {
  28. Scanner scanner = new Scanner(System.in);
  29. try(Connection connection = createConnection("cinemas_ex");
  30. PreparedStatement getTicketsStatement = connection.prepareStatement(GET_TICKETS_QUERY);
  31. PreparedStatement updateQuantityStatement = connection.prepareStatement(GET_QUANTITY_QUERY);
  32. ResultSet resultSet = getTicketsStatement.executeQuery();){
  33. while (resultSet.next()){
  34. int id = resultSet.getInt("id");
  35. System.out.println(String.format("Ticket: id: %d", id));
  36. }
  37. System.out.println("Select ticket to view quantity (insert id): ");
  38. int ticketsId = scanner.nextInt();
  39. int updateTicketsQuantity = scanner.nextInt();
  40. updateQuantityStatement.setInt(2, ticketsId);
  41. updateQuantityStatement.setInt(1, updateTicketsQuantity);
  42. System.out.println("Ilość zmienionych rekordów: " + updateQuantityStatement.executeUpdate());
  43.  
  44. }
  45. }
  46.  
  47. private static Connection createConnection(String databaseName) throws SQLException {
  48. return DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex",
  49. "root","coderslab");
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement