Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a_Zadania.a_Dzien_2.a_Zmiana_usuwanie_danych;
- import java.sql.*;
- import java.util.Scanner;
- public class Main2 {
- public static void main(String[] args) throws SQLException{
- Scanner scanner = new Scanner(System.in);
- try(
- Connection connection = createConnection("cinemas_ex");
- PreparedStatement getTicketsStatement = connection.prepareStatement(GET_TICKETS_QUERY);
- PreparedStatement setQtyStatement = connection.prepareStatement(SET_TICKET_QTY);
- ResultSet resultSet = getTicketsStatement.executeQuery();
- ) {
- while (resultSet.next()){
- int id = resultSet.getInt("id");
- int quantity = resultSet.getInt("quantity");
- int price = resultSet.getInt("price");
- System.out.println(String.format("Ticket: %d, quantity: %d, price: %d", id, quantity, price));
- }
- System.out.println("Podaj id biletu:");
- int ticketId = scanner.nextInt();
- System.out.println("Podaj ilość sprzedanych biletów:");
- int ticketQty = scanner.nextInt();
- setQtyStatement.setInt(1, ticketQty);
- setQtyStatement.setInt(2, ticketId);
- System.out.println("Ilość zmienionych wartości w tabeli:" +setQtyStatement.executeUpdate());
- }
- }
- private static final String GET_TICKETS_QUERY = "SELECT id, quantity, price FROM tickets";
- private static final String SET_TICKET_QTY = "UPDATE tickets SET quantity = ? WHERE id = ?";
- private static Connection createConnection(String databaseName) throws SQLException{
- return DriverManager.getConnection("jdbc:mysql://localhost:3306/"+databaseName, "root", "coderslab");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement