Guest User

Untitled

a guest
Jul 15th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. package pl.coderslab;
  2.  
  3. import com.mysql.jdbc.Connection;
  4.  
  5. import java.math.BigDecimal;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.SQLException;
  9.  
  10. public class Main {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. addNewProduct("Nowy produkt 2", "Opis nr 2", 4.4);
  15.  
  16. }
  17.  
  18. public static void addNewProduct(String productName, String productDesc, double price) {
  19. try {
  20. Connection connection = (Connection) DriverManager.getConnection(
  21. "jdbc:mysql://localhost:3306/products_ex?useSSL=false",
  22. "root",
  23. "tajne123"
  24. );
  25.  
  26. String queryToAdd = "insert into products(name, description, price) values(?,?,?)";
  27. PreparedStatement preparedStatement = connection.prepareStatement(queryToAdd);
  28. preparedStatement.setString(1, productName);
  29. preparedStatement.setString(2, productDesc);
  30.  
  31. BigDecimal bigDecimal = new BigDecimal(price);
  32. preparedStatement.setBigDecimal(3, bigDecimal);
  33.  
  34. preparedStatement.executeUpdate();
  35.  
  36. } catch (SQLException ex) {
  37. System.out.println(ex.getMessage());
  38. }
  39. }
  40. }
Add Comment
Please, Sign In to add comment