package com.t70.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.t70.entity.Product;
public class ProductDao extends BaseDAO{
public boolean save(Product p){
String sql = "insert into product values(?,?)";
Connection con = super.getConnection();
try {
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, p.getPname());
pstmt.setString(2, p.getPic());
pstmt.executeUpdate();
pstmt.close();
con.close();
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public List<Product> findAll(){
List<Product> list = new ArrayList<Product>();
String sql = "select * from product";
Connection con = super.getConnection();
try {
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
list.add(new Product(rs.getInt(1),rs.getString(2),rs.getString(3)));
}
pstmt.close();
con.close();
return list;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}