1

I have a database Table where I can edit a value. I want to update the same, below is the code that I have tried.

The table code is as below

<table border="1px">
    <tr>
        <td><b>DBID</b></td>
        <td><b>Query Raised</b></td>
        <td><b>Time Raised</b></td>
        <td><b>Query Answered</b></td>
        <td><b>Time Answered</b></td>
    </tr>
<%
try {
    ps = con.prepareStatement("Select DBID, Query_Raised, TR, Query_Answered, TA from Scope1 where TR!='null'");

    rs = ps.executeQuery();

    while(rs.next()) {
%>
    <tr>
        <td><%=rs.getString("DBID")%></td>
        <td><input type="Text" value="<%=rs.getString("Query_Raised")%>" name="Updat"></td>
        <td><%=rs.getString("TR")%> </td>
        <td><%=rs.getString("Query_Answered")%></td>
        <td><%=rs.getString("TA")%></td>
        <td><input type="Submit" value="Update"></td>
    </tr>
<%
    } // while loop ends here

    rs.close();
    con.close();
} catch(Exception e) {
    out.println(e);
}
%>
</table>

and the update query that is used is:

String a = request.getParameter("Updat");

ps = con.prepareStatement("Update Scope1 Set Query_Raised = '" + a + "'");

int i = ps.executeUpdate();

if(i == 1) {
    out.print("Done");
} else{
    out.print("Erro");
}

I wanted to know the where condition that I should use to update the data in same page.

Thanks

Prakash K
  • 11,669
  • 6
  • 51
  • 109
Rakesh
  • 564
  • 1
  • 8
  • 25
  • 2
    Please do not put database code in your JSPs! JSPs are **view** components, and your queries should be encapuled in beans on the **model** side (see [here](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) for the big picture). Once you establish this structure, you will find many problems solved. – f_puras Sep 04 '12 at 13:09
  • Related to your question: Add unique ids to your `` fields, so after submitting you can find out which have been modified. – f_puras Sep 04 '12 at 13:12
  • Often times it's a pure overkill to build an MVC for a simple case like this, as it requires extra code and takes time to build & deploy, while just putting code in a JSP requires you to basically refresh the page. – Endy Sep 04 '12 at 17:52
  • @Endy totally agree with you. But no project in the real-world will be so small or simple. so I guess f_puras is just cautioning (educating) him for the real-world scenario. – Prakash K Sep 05 '12 at 12:56

2 Answers2

4

I understand the first code part in the question is a JSP say update.jsp and the second part it seems is your servlet, say UpdateServlet.

So in your update.jsp on every row you have an <input type="text"> and submit button, but the <form> I think is only one which must be outside the <table>, so here goes my solution (choose any one):

  1. Use multiple <form> tags for each row, like

    <form action="whatever_action_you_have_which_calls_the_servlet"
          name="form<%=rs.getString("DBID")%>"
          id="formID<%=rs.getString("DBID")%>">
       // Including name and id so that the different forms remain unique
        <tr>
            <td><%=rs.getString("DBID")%></td>
            <td><input type="Text" value="<%=rs.getString("Query_Raised")%>" name="Updat"></td>
            <td><%=rs.getString("TR")%> </td>
            <td><%=rs.getString("Query_Answered")%></td>
            <td><%=rs.getString("TA")%></td>
            <td><input type="Submit" value="Update"></td>
        </tr>
    </form>
    

    So when you click on submit it would submit the <form> for which the submit button was clicked and would submit only the input which was edited and voila! your servlet code also works fine.

  2. You can have both the blocks of code in the same JSP, then use the <form> code snippet as described in point#1 and the second code snippet would be:

    String a = request.getParameter("Updat");
    
    if ( (a is not empty) or (a is not null)  ) {
        ps = con.prepareStatement("Update Scope1 Set Query_Raised = '" + a + "'");
    
        int i = ps.executeUpdate();
    
        if(i == 1) {
            out.print("Done");
        } else{
            out.print("Error");
        }
    }
    
    ...
    
    <form action="whatever_action_you_have_which_will_call_this_JSP" ...>
        <tr> ... your code as in point#1
        </tr>
    </form>
    
  3. Use ajax and other javascript DOM manipulative methods to accomplish this as said by Endy.
    For this you will need to have JSP (to display) & Servlet (to update the code).
    This might be a little more effort but you will learn Ajax and will be a little more close to real world.
    By far jQuery ajax is the simplest to use.

Note:
Just for the record, I know you might be practicing but if you are planning to use it in a real project then please read ahead.

It is a bad-practice to use scriptlets in JSP (unless really required and that too if it is for view-level logic and not business or data-level logic) and even worse is to use JDBC code inside a JSP. So it would be really great if you could follow f_puras's advice.

If you are wondering why I should listen to your unsolicited advice then here is some food for thought:

Hope this helps.

Community
  • 1
  • 1
Prakash K
  • 11,669
  • 6
  • 51
  • 109
0

You can either use AJAX to post the data to a page that handles the updating, then return the result as a callback, see http://api.jquery.com/jQuery.post/.

Other solution is to forward user back to the page with the form after you have processed the submission.

Third solution is to submit the data to the same page and parse/update it there.

Endy
  • 698
  • 3
  • 11
  • Hi Endy, can you please let me know how do i do that with 3rd approach u mentioned above . Thanks – Rakesh Sep 04 '12 at 13:41
  • Basically move your "update query" part to the same page where the form is. It's not a very good practice, but works just fine. – Endy Sep 04 '12 at 17:48