Guest User

Untitled

a guest
Apr 26th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 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.  
  9. public class Main1 {
  10.  
  11. public static String query = "INSERT INTO products (name, description, price) VALUES (\"vw Golf\", \"Kebabwagen\", 1500.00);";
  12.  
  13. public static String[] products = {
  14. "bambino mydelko",
  15. "fa",
  16. "kalasznikow",
  17. "lodowka"};
  18.  
  19. public static void main(String[] args) {
  20.  
  21. try (Connection conn = DriverManager
  22. .getConnection("jdbc:mysql://localhost:3306/products_ex?useSSL=false",
  23. "root", "coderslab")) {
  24. // Statement stmt = conn.createStatement();
  25. PreparedStatement pstmt = conn.prepareStatement("INSERT INTO products (name) VALUES (?);");
  26. for(String product_name : products) {
  27. // query = "INSERT INTO products (name) VALUES ('" + product_name + "');";
  28. // System.out.println(query);
  29. // int result = stmt.executeUpdate(query);
  30. pstmt.setString(1, product_name);
  31. int result = pstmt.executeUpdate();
  32. System.out.println(result);
  33. }
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37.  
  38. }
  39. }
Add Comment
Please, Sign In to add comment