Rakesh
Rakesh

Reputation: 594

Edit and update same textbox

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

Upvotes: 1

Views: 2640

Answers (2)

Prakash K
Prakash K

Reputation: 11698

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.

Upvotes: 4

Endy
Endy

Reputation: 696

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.

Upvotes: 0

Related Questions