Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ProductDaoImpl implements ProductDao {
- String driver="com.mysql.jdbc.Driver";
- String url="jdbc:mysql://localhost:3306/mysql";
- String user="root";
- String password="root";
- public boolean deleteProduct(int pno) {
- /* String driver="com.mysql.jdbc.Driver";
- String url="jdbc:mysql://localhost:3306/mysql";
- String user="root";
- String password="root";
- */ Connection con=JdbcConnection.getConnection(driver, url, user, password);
- try {
- PreparedStatement pst=con.prepareStatement("delete from product where pno=?");
- pst.setInt(1, pno);
- int rec=pst.executeUpdate();
- if(rec==1){
- return true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }finally{
- JdbcConnection.close(con);
- }
- return false;
- }
- public boolean insertProduct(Product product) {
- Connection con=JdbcConnection.getConnection(driver, url, user, password);
- try {
- PreparedStatement pst=con.prepareStatement("insert into product values(?,?,?)");
- pst.setInt(1, product.getPno());
- pst.setString(2, product.getPname());
- pst.setInt(3, product.getPrice());
- int rec=pst.executeUpdate();
- if(rec==1){
- return true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }finally{
- JdbcConnection.close(con);
- }
- return false;
- }
- public ArrayList<Product> showAll() {
- Connection con=JdbcConnection.getConnection(driver, url, user, password);
- ArrayList<Product>pList=new ArrayList();
- try {
- Statement st=con.createStatement();
- ResultSet rs=st.executeQuery("select * from product");
- while(rs.next()){
- int pno=rs.getInt(1);
- String pname=rs.getString(2);
- int price=rs.getInt(3);
- Product p= new Product(pno, pname, price);
- pList.add(p);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }finally{
- JdbcConnection.close(con);
- }
- return pList;
- }
- }
Add Comment
Please, Sign In to add comment