Advertisement
Guest User

Untitled

a guest
Oct 13th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. /*
  2. * Copyright ActiveLearning, Inc.
  3. * http://www.activelearning.ph
  4. * @author Gavin C. Lim
  5. */
  6. package ph.grocerific.estore.service;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.Statement;
  12. import java.util.ArrayList;
  13. import ph.grocerific.estore.domain.Product;
  14.  
  15. /**
  16. *
  17. * @author Gavin Lim
  18. */
  19. public class ProductsService {
  20.  
  21. public ArrayList<Product> getProducts() throws Exception {
  22.  
  23. Class.forName("com.mysql.jdbc.Driver");
  24.  
  25. String url = "jdbc:mysql://localhost:3306/grocerific";
  26. String username = "root";
  27. String password = "secret";
  28.  
  29. try (
  30.  
  31. Connection conn = DriverManager.getConnection(
  32. url, username, password
  33. );
  34.  
  35. Statement stmt = conn.createStatement();
  36.  
  37. ) {
  38. ResultSet rs = stmt.executeQuery("SELECT * FROM products");
  39. ArrayList<Product> products = new ArrayList<>();
  40. while (rs.next()) {
  41. Product product = new Product();
  42. product.setId(rs.getInt("id"));
  43. product.setDescription(rs.getString("description"));
  44. product.setSize(rs.getString("size"));
  45. product.setPrice(rs.getFloat("price"));
  46. products.add(product);
  47. }
  48. if (products.size() == 0) {
  49. return null;
  50. }
  51. return products;
  52. }
  53.  
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement