Data Access object method not working

175 Views Asked by At

I am a student learning JSP, and I seem to have this issue in executing a method via an object of a DAO class. When the database connectivity and SQL query is given on the servlet itself it, it will work. But when given in the DAO class and a object is used, it doesn't work. Please help.

import dataaccessobjects.cartDAO1;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class addtoCartServ extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    cartDAO1 newcart = new cartDAO1();
    PrintWriter out = response.getWriter();

    if (request.getParameter("submit") != null){

                //out.println("added to cart");
             try {
            //out.println("submit not null");
            String Uname = (String) request.getSession().getAttribute("Welcome");
            String ino = request.getParameter("ino");
            String iqnty = request.getParameter("quantity");
            String iname = request.getParameter("iname");

          if(newcart.addToCart(iname,Uname,ino,iqnty)){

          out.println("added to cart");
          }
            } catch (SQLException ex) {
            Logger.getLogger(addtoCartServ.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(addtoCartServ.class.getName()).log(Level.SEVERE, null, ex);
        }


        }
}

}

The DAO class

public cartDAO1(){
}
public boolean addToCart(String iname,String username, String ino,String       iqnty) throws SQLException, ClassNotFoundException{
 boolean flag = false;

Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn =       DriverManager.getConnection("jdbc:mysql://localhost:3306/styleomega","root","");
        PreparedStatement ps = conn.prepareStatement("INSERT INTO   cart(iname,uname,ino,iqnty) VALUES (?,?,?,?)");


        // set the values for parameters
            ps.setString(1,iname);
           ps.setString(2,username);
            ps.setString(3,ino);
            ps.setString(4,iqnty);
            int rs = ps.executeUpdate();

        if (rs==1){
        flag = true;
        }
        return flag;


} 

}

2

There are 2 best solutions below

2
On

You should be import the DAO class package in servlet then access it it will work like

import DAO.cartDao; 

If you will not import then how to acces it

0
On

I don't understand exactly what is not working? But I have noticed, you're not closing statement and the database connection in your DAO class.

FYI: An example

Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
 conn = // Retrieve connection
 stmt = conn.prepareStatement(// Some SQL);
 rs = stmt.executeQuery();
} catch(Exception e) {
  // Error Handling
} finally {
 try { if (rs != null) rs.close(); } catch (Exception e) {};
 try { if (stmt != null) stmt.close(); } catch (Exception e) {};
 try { if (conn != null) conn.close(); } catch (Exception e) {};
}