Guest User

Untitled

a guest
Jun 18th, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. public class ProductDaoImpl implements ProductDao {
  2. String driver="com.mysql.jdbc.Driver";
  3. String url="jdbc:mysql://localhost:3306/mysql";
  4. String user="root";
  5. String password="root";
  6. public boolean deleteProduct(int pno) {
  7.  
  8. /* String driver="com.mysql.jdbc.Driver";
  9. String url="jdbc:mysql://localhost:3306/mysql";
  10. String user="root";
  11. String password="root";
  12. */ Connection con=JdbcConnection.getConnection(driver, url, user, password);
  13. try {
  14. PreparedStatement pst=con.prepareStatement("delete from product where pno=?");
  15. pst.setInt(1, pno);
  16. int rec=pst.executeUpdate();
  17. if(rec==1){
  18.  
  19. return true;
  20. }
  21. } catch (SQLException e) {
  22. e.printStackTrace();
  23. }finally{
  24. JdbcConnection.close(con);
  25. }
  26. return false;
  27. }
  28.  
  29. public boolean insertProduct(Product product) {
  30. Connection con=JdbcConnection.getConnection(driver, url, user, password);
  31. try {
  32. PreparedStatement pst=con.prepareStatement("insert into product values(?,?,?)");
  33. pst.setInt(1, product.getPno());
  34. pst.setString(2, product.getPname());
  35. pst.setInt(3, product.getPrice());
  36. int rec=pst.executeUpdate();
  37. if(rec==1){
  38.  
  39. return true;
  40. }
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. }finally{
  44. JdbcConnection.close(con);
  45. }
  46. return false;
  47.  
  48.  
  49.  
  50. }
  51.  
  52. public ArrayList<Product> showAll() {
  53.  
  54. Connection con=JdbcConnection.getConnection(driver, url, user, password);
  55. ArrayList<Product>pList=new ArrayList();
  56. try {
  57. Statement st=con.createStatement();
  58. ResultSet rs=st.executeQuery("select * from product");
  59.  
  60. while(rs.next()){
  61.  
  62. int pno=rs.getInt(1);
  63. String pname=rs.getString(2);
  64. int price=rs.getInt(3);
  65. Product p= new Product(pno, pname, price);
  66. pList.add(p);
  67. }
  68.  
  69.  
  70.  
  71. } catch (SQLException e) {
  72. e.printStackTrace();
  73. }finally{
  74. JdbcConnection.close(con);
  75. }
  76.  
  77.  
  78. return pList;
  79. }
  80.  
  81. }
Add Comment
Please, Sign In to add comment