Advertisement
Guest User

Untitled

a guest
May 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package a_Zadania.a_Dzien_2.a_Zmiana_usuwanie_danych;
  2.  
  3.  
  4. import java.sql.*;
  5. import java.util.Scanner;
  6.  
  7. public class Main2 {
  8.  
  9.  
  10.  
  11. public static void main(String[] args) throws SQLException{
  12.  
  13.  
  14. Scanner scanner = new Scanner(System.in);
  15.  
  16. try(
  17. Connection connection = createConnection("cinemas_ex");
  18. PreparedStatement getTicketsStatement = connection.prepareStatement(GET_TICKETS_QUERY);
  19. PreparedStatement setQtyStatement = connection.prepareStatement(SET_TICKET_QTY);
  20. ResultSet resultSet = getTicketsStatement.executeQuery();
  21. ) {
  22. while (resultSet.next()){
  23. int id = resultSet.getInt("id");
  24. int quantity = resultSet.getInt("quantity");
  25. int price = resultSet.getInt("price");
  26. System.out.println(String.format("Ticket: %d, quantity: %d, price: %d", id, quantity, price));
  27. }
  28. System.out.println("Podaj id biletu:");
  29. int ticketId = scanner.nextInt();
  30. System.out.println("Podaj ilość sprzedanych biletów:");
  31. int ticketQty = scanner.nextInt();
  32. setQtyStatement.setInt(1, ticketQty);
  33. setQtyStatement.setInt(2, ticketId);
  34. System.out.println("Ilość zmienionych wartości w tabeli:" +setQtyStatement.executeUpdate());
  35.  
  36. }
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. private static final String GET_TICKETS_QUERY = "SELECT id, quantity, price FROM tickets";
  45. private static final String SET_TICKET_QTY = "UPDATE tickets SET quantity = ? WHERE id = ?";
  46.  
  47.  
  48.  
  49. private static Connection createConnection(String databaseName) throws SQLException{
  50. return DriverManager.getConnection("jdbc:mysql://localhost:3306/"+databaseName, "root", "coderslab");
  51. }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement