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.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Scanner;
- /**
- * W pliku `Main2.java`:
- 1. Pobierz z bazy listę biletów i wyświetl na konsoli.
- 2. Pobierz od użytkownika identyfikator biletu z konsoli.
- 3. Pobierz z konsloli ilość sprzedanych biletów.
- 4. Zmodyfikuj odpowiedni wiersz w bazie danych.
- * @author tomek
- *
- */
- public class Main2 {
- private static final String GET_TICKETS_QUERY =
- "SELECT * FROM tickets";
- private static final String GET_QUANTITY_QUERY =
- "UPDATE tickets SET quantity = quantity + ? WHERE id = ?";
- 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 updateQuantityStatement = connection.prepareStatement(GET_QUANTITY_QUERY);
- ResultSet resultSet = getTicketsStatement.executeQuery();){
- while (resultSet.next()){
- int id = resultSet.getInt("id");
- System.out.println(String.format("Ticket: id: %d", id));
- }
- System.out.println("Select ticket to view quantity (insert id): ");
- int ticketsId = scanner.nextInt();
- int updateTicketsQuantity = scanner.nextInt();
- updateQuantityStatement.setInt(2, ticketsId);
- updateQuantityStatement.setInt(1, updateTicketsQuantity);
- System.out.println("Ilość zmienionych rekordów: " + updateQuantityStatement.executeUpdate());
- }
- }
- private static Connection createConnection(String databaseName) throws SQLException {
- return DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex",
- "root","coderslab");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement