0

Hi When i'm trying to update a jdbc table only first row gets updated below is the code for it. though i have an individual button for every row any button i clicks takes input values as first row's.

<tr>
           <td><%=rs.getString("DBID")%></td>
           <td><input type="text" name="prev" id="prev" value="<%=rs.getString("Query_Raised")%>" border=''></td>
           <td><%=rs.getString("TR")%> </td>
           <td><%=rs.getString("Query_Answered")%></td>
           <td><%=rs.getString("TA")%></td>
           <td><input type="submit" value="Edit">
       </tr>

and for comparing i used the below(for where condition) and it is also taking only the first row value

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body><form method="post" action="Up_Query_DB.jsp">
        <table><tr><td> <input type="text" id="xyz" name="xyz" value="<%=request.getParameter("prev")%>"></td></tr>
            <tr><td><INPUT TYPE="TEXT" NAME="updat" id="updat"></td></tr>
            <tr><td><input type="submit" value="Update"></td></tr></table></form>
</body>

and the update i used is

<% try
                 {
   String sc=request.getParameter("xyz");
   String upd=request.getParameter("updat");
   ps=con.prepareStatement("Update Scope1 Set Query_Raised='"+upd+"' where Query_Raised='"+sc+"'");
   int i=ps.executeUpdate();
   if(i==1)
                 {
       String redirectURL= "View Queries.jsp";
       response.sendRedirect(redirectURL);

   }
         else{
       out.print("Erro");
         }
 }
   catch(Exception e)
                   {
    out.println("error");

   }%>

Thanks

Rakesh
  • 564
  • 1
  • 8
  • 25
  • Hi Rakesh, I regret you [still](http://stackoverflow.com/questions/12264011) haven't rethought your idea of putting JDBC code in a JSP. I already [commented](http://stackoverflow.com/questions/12264011/edit-and-update-same-textbox#comment16443554_12264011) on your `s` having no `id` attribute. It does not help if you add the same id for all of them now... – f_puras Sep 05 '12 at 10:25

1 Answers1

3

1) It helps to write valid HTML
<td><input type="submit" value="Edit"></td>
2) Even though you have an Edit button for every row, you have no way of knowing which of the buttons was actually pressed when submitting the form - they are all the same.
Most common way of implementing multi-row edit is to have a separate form for each row. Unless you want to be able to edit multiple rows and then submit all changes at once in which case you will need to distinguish between rows' data - by giving your data input fields unique names.

Later edit:
3) updating table rows based on value that you intend to change is bad idea for various reasons, one of them is that your query may update the rows that you never knew even existed.

<tr>
<form ... >
<td> <input type="hidden" name="recordID" value="%dynamicRecordIDFromYourDB%" /> </td>
...
<td> <input type="text" name="data" value="%updatableDataFromYourDB" />
<td> <input type="submit" ... />
</form>
</tr>

Above code is common for single row update option. Then your update SQL will look something like:

sqlString = "Update tableName Set updatableFiled ='" + data + "' where recordID = " + recordID;
Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
  • hi Arlington, thanks for the answer, if you don't mind could you please edit my above code and show. please. Thanks – Rakesh Sep 05 '12 at 10:07
  • And please don't wait for us to write your complete code for you - you will wait for a **very long time**. – Germann Arlington Sep 05 '12 at 10:31
  • thanks for the code, but my code is working fine for 1 row where as you stated that i should be using multiple forms please help me how do i do it? Thanks – Rakesh Sep 05 '12 at 10:45
  • Did you not notice that in my version the form is **inside individual row**? The effect of this will be that only the data from that row will be submitted to your form processing page. This way you don't need to have unique field names for different rows. If you want to be able to edit many rows and submit **all** changes together, than you will need to have unique field names for each row (most common way is appending recordID to each field name). – Germann Arlington Sep 05 '12 at 10:52
  • ok thank you and i'm sorry for not noticing it. but in my IDE(NetBeans) it is saying that form cant be a child of – Rakesh Sep 05 '12 at 10:57
  • OK, try it around than. Though, what IDE tells you in this case is just a warning because you are writing HTML here. – Germann Arlington Sep 05 '12 at 10:59
  • could you please look at this post as i wasnot getting a correct answer from any one else please. here all are suggesting me to use AJAX but infact i'm entirely to this technology side, i'm learning from the solutions given by guys like you. http://stackoverflow.com/questions/12197885/remain-in-same-page-even-after-submitting-jsp-form thanks – Rakesh Sep 05 '12 at 11:07