Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. package dbHelpers;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7.  
  8. import model.Grocery;
  9.  
  10. public class AddQuery {
  11.  
  12. public Connection connection;
  13.  
  14. public AddQuery(String dbName, String uname, String pwd) {
  15. String url = "jdbc:mysql://localhost:3306/" + dbName;
  16.  
  17. try {
  18. Class.forName("com.mysql.jdbc.Driver").newInstance();
  19. this.connection = DriverManager.getConnection(url, uname, pwd);
  20. } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
  21. // TODO Auto-generated catch block
  22. e.printStackTrace();
  23. }
  24. }
  25.  
  26. public void doAdd(Grocery grocery) {
  27. String query = "insert into products (SKU, ProductType, Flavor, Cost, Price, Quantity) values (?, ?, ?, ?, ?, ?)";
  28.  
  29. try {
  30. PreparedStatement ps = connection.prepareStatement(query);
  31. ps.setString(1, grocery.getSku());
  32. ps.setString(2, grocery.getProductType());
  33. ps.setString(3, grocery.getFlavor());
  34. ps.setDouble(4, grocery.getCost());
  35. ps.setDouble(5, grocery.getPrice());
  36. ps.setInt(6, grocery.getQuantity());
  37.  
  38. ps.executeUpdate();
  39.  
  40. } catch (SQLException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement