Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a_Zadania.a_Dzien_1.b_Dodawanie_danych;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Main1 {
- public static String query = "INSERT INTO products (name, description, price) VALUES (\"vw Golf\", \"Kebabwagen\", 1500.00);";
- public static String[] products = {
- "bambino mydelko",
- "fa",
- "kalasznikow",
- "lodowka"};
- public static void main(String[] args) {
- try (Connection conn = DriverManager
- .getConnection("jdbc:mysql://localhost:3306/products_ex?useSSL=false",
- "root", "coderslab")) {
- // Statement stmt = conn.createStatement();
- PreparedStatement pstmt = conn.prepareStatement("INSERT INTO products (name) VALUES (?);");
- for(String product_name : products) {
- // query = "INSERT INTO products (name) VALUES ('" + product_name + "');";
- // System.out.println(query);
- // int result = stmt.executeUpdate(query);
- pstmt.setString(1, product_name);
- int result = pstmt.executeUpdate();
- System.out.println(result);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment