Advertisement
Guest User

Untitled

a guest
May 17th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. package a_Zadania.a_Dzien_1.b_Dodawanie_danych;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Scanner;
  9.  
  10. public class Main4 {
  11. private static Connection createConnection() throws SQLException {
  12. return DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex", "root", "coderslab");
  13. }
  14.  
  15. public static void main(String[] args) throws SQLException {
  16.  
  17.  
  18. Scanner scan = new Scanner(System.in);
  19.  
  20. System.out.println("Proszę o podanien numeru ID");
  21. while (true) {
  22. try {
  23. String temp = scan.nextLine();
  24. int id = Integer.parseInt(temp);
  25. System.out.println("Proszę podać nazwę");
  26. String name = scan.nextLine();
  27. System.out.println("Proszę podać adres");
  28. String adress = scan.nextLine();
  29. addCinema(id, name, adress);
  30. break;
  31. } catch (Exception e) {
  32. System.out.println("Podano nieprawidłowy numer, podaj nr jeszcze raz.");
  33. }
  34. }
  35.  
  36.  
  37. }
  38.  
  39. static void addCinema(int id, String name, String address) {
  40. String query1 = "INSERT INTO cinemas(id, name, adress) VALUES(?, ?, ?);";
  41.  
  42. try (Connection connection = createConnection();
  43. PreparedStatement statement = connection.prepareStatement(query1);) {
  44. statement.setInt(1, id);
  45. statement.setString(2, name);
  46. statement.setString(3, address);
  47. System.out.println(statement.executeUpdate());
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement