Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright ActiveLearning, Inc.
- * http://www.activelearning.ph
- * @author Gavin C. Lim
- */
- package ph.grocerific.estore.service;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.ArrayList;
- import ph.grocerific.estore.domain.Product;
- /**
- *
- * @author Gavin Lim
- */
- public class ProductsService {
- public ArrayList<Product> getProducts() throws Exception {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/grocerific";
- String username = "root";
- String password = "secret";
- try (
- Connection conn = DriverManager.getConnection(
- url, username, password
- );
- Statement stmt = conn.createStatement();
- ) {
- ResultSet rs = stmt.executeQuery("SELECT * FROM products");
- ArrayList<Product> products = new ArrayList<>();
- while (rs.next()) {
- Product product = new Product();
- product.setId(rs.getInt("id"));
- product.setDescription(rs.getString("description"));
- product.setSize(rs.getString("size"));
- product.setPrice(rs.getFloat("price"));
- products.add(product);
- }
- if (products.size() == 0) {
- return null;
- }
- return products;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement