0

I have made a servlet file iin which I have added insertion, deletion and viewingthe data from database.

Now I want to add updation of inserted data but I am not able to complete it. As I have added whole code in single file I dont know how to add it. can any one help me with it ?? Waiting for reply.

        if(req.getParameter("choise")!=null)    {
            nm = req.getParameter("choise");

            idr = "UPDATE student SET rollno = ?, class = ?, mobileno = ? WHERE name ='"+nm+"'";

            try {
                st=con.createStatement();
                int rs1 = st.executeUpdate(idr);
                pwinsert.println(rs1);
            }
            catch (Exception e) {
                e.getMessage();
                System.out.println("Error " +e);
            }

            System.out.println("Data Edited...");

        }
Raman
  • 1
  • 2
  • 3
  • 5
  • How about you give us the update method you have written so far, and the html form that invokes it. – BevynQ Jun 10 '13 at 04:36
  • it looks like you are using PreparedStatment syntax with a normal Statement. where do rollNo, class and mobileNo come from? – BevynQ Jun 10 '13 at 05:45
  • @BevynQ Sir, this is my code, can you please help me to add update feaure ?? http://stackoverflow.com/questions/16998126/update-edit-entered-data-from-database-servlet – Raman Jun 10 '13 at 05:52

3 Answers3

0

You can create different Servlet's for each CRUD Operation. Either using flag variable also you can recognize which CRUD operation to follow, but this is not good practice. Check this link

Sunil Gulabani
  • 878
  • 1
  • 8
  • 21
0
 if(req.getParameter("choise")!=null)    {
            nm = req.getParameter("choise");
            if(nm.equals("UPDATE")){ idr = "UPDATE student SET rollno = ?, class = ?, mobileno = ? WHERE name ='"+nm+"'";

            try {
                st=con.createStatement();
                int rs1 = st.executeUpdate(idr);
                pwinsert.println(rs1);
            }
            catch (Exception e) {
                e.getMessage();
                System.out.println("Error " +e);
            }

            System.out.println("Data Edited...");



}else if(nm.equals("ADD")){

idr = "<INSERT QUERY>";

            try {
                st=con.createStatement();
                int rs1 = st.executeQuery(idr);
                pwinsert.println(rs1);
            }
            catch (Exception e) {
                e.getMessage();
                System.out.println("Error " +e);
            }

            System.out.println("Data inserted...");


}


        }
shreyansh jogi
  • 2,082
  • 12
  • 20
  • nice but sir can you please help me on this ?? http://stackoverflow.com/questions/16998126/update-edit-entered-data-from-database-servlet Wanna add the EDIT funcctionality...... – Raman Jun 10 '13 at 06:08
0

this should do what you want

String nm = req.getParameter("name");
String roll = req.getParameter("roll");
String clas = req.getParameter("clas");
String mono = req.getParameter("mono");

if(req.getParameter("choise")!=null)    {
    nm = req.getParameter("choise");

    idr = "UPDATE student SET rollno = ?, class = ?, mobileno = ? WHERE name = ?";

    try {
        PreparedStatement pst=con.prepareStatement(idr);
        pst.setString(1,roll);
        pst.setString(2,class);
        pst.setString(3,mono);
        pst.setString(4,nm);

        int rs1 = st.executeUpdate(idr);
        pwinsert.println(rs1);
    } catch (Exception e) {
        e.getMessage();
        System.out.println("Error " +e);
    }

    System.out.println("Data Edited...");

}

I suggest you start breaking you code into methods for instance.

public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException    {

    String nm = req.getParameter("name");
    String roll = req.getParameter("roll");
    String clas = req.getParameter("clas");
    String mono = req.getParameter("mono");

    Connection conn = getDatabaseConnection();
    Statement sql = createSqlStatement(conn, nm ,roll, clas, mono);

    processSqlStatement(conn,sql);

    createOutput(req,res);

}

It will make it a lot easier for you to maintain and add new things.

BevynQ
  • 8,089
  • 4
  • 25
  • 37