package org.fanlychie.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.fanlychie.model.Product;
public class ProductDao {
public static List<Product> getAll() {
Connection conn = null;
try {
String username = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3306/product_repo";
conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement("select * from product");
ResultSet rs = pstmt.executeQuery();
List<Product> products = new ArrayList<Product>();
while (rs.next()) {
Product product = new Product();
product.setId(rs.getInt("id"));
product.setSales(rs.getInt("sales"));
product.setArea(rs.getString("area"));
product.setName(rs.getString("name"));
product.setPrice(rs.getDouble("price"));
product.setSortName(rs.getString("sort_name"));
product.setSubSortName(rs.getString("sub_sort_name"));
products.add(product);
}
return products;
} catch (Throwable e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {}
}
}
}
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}