-2

Friends i have a jsp page which retrieves data from mysql table and displays them in form of table. I will also display another column consisting of a button to give Status to 1 . i need to update the database table when i click the status button.

following is the code for jsp

<form action="Edit" method="post">
<table cellspacing="8">
<tr>
    <td><b>Name</b>
    </td>
    <td><b>Place</b>
    </td>
    <td><b>Gender</b>
    </td>
    <td><b>UserName</b>
    </td>
    <td><b>Password</b>
    </td>
    <td><b>Status</b>
    </td>
    <td></td>
</tr>
<%
ViewService vs = new ViewService();
ResultSet rs = vs.getRecords();
while(rs.next())
{
    %>
    <tr>
    <td><%=rs.getString(2) %></td>
    <td><%=rs.getString(3) %></td>
    <td><%=rs.getString(4) %></td>
    <td><%=rs.getString(5) %></td>
    <td><%=rs.getString(6) %></td>
    <td><%=rs.getString(7) %></td>
    <td></td>
     <td><input type="text" name="userid" value="<%=rs.getString(2) %>"                                        

             style="visibility: hidden;" > <input type="submit" value="Active"></td>
       </tr>
     <%     }
     %>
     </table>
    </form>

And my edit servlet is

package Admin;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DBcon.Dbcon;
@WebServlet("/Edit")
public class Edit extends HttpServlet {
private static final long serialVersionUID = 1L;
public Edit() {
    super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)     

throws ServletException, IOException {

}

protected void doPost(HttpServletRequest request, HttpServletResponse response)  

throws ServletException, IOException {
    Connection con;
    PreparedStatement ps;
    Dbcon db = new Dbcon();
    con = db.getCon();
    int status=1;
    int userid =Integer.parseInt(request.getParameter("userid"));
    try {
        ps=con.prepareStatement("update tbluser set status = ? where userid 

= ?");
        ps.setInt(7,status);
        ps.setInt(1, userid);
        ps.executeUpdate();
        con.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    response.sendRedirect("ViewUser.jsp");
}

}
4
  • so whats the problem ..if you got error then explain it so we can go through it solve it Jun 12, 2014 at 6:09
  • is it that we should continue the code for u????it would be better u try and let us know when u get stuck...**Hint for your question ** use AJAX Jun 12, 2014 at 6:10
  • You have mentioned what you are trying to do, please specify what problem you are facing? Jun 12, 2014 at 6:10
  • And what is the problem?
    – Jens
    Jun 12, 2014 at 6:10

2 Answers 2

0

inside while loop

put the form tag inside while loop ( < td > ) not around the < table >

....
<td><%=rs.getString(7) %></td>
<td></td>
 <td>
   <form action="Edit" method="post">
       <input type="text" name="userid" value="<%=rs.getString(2) %>" style="visibility: hidden;" > <input type="submit" value="Active">
    </form>
 </td>
   </tr>
 <%     }
 %>
 </table>

OR

Use Hyperlink instead of form and button

<td>
   <a href="Edit?userd=<%=rs.getString(2) %>"> Activate </a>
<td>
0

try this bro i modified your codes.

protected void doPost(HttpServletRequest request, HttpServletResponse response)  

throws ServletException, IOException {
    Connection con;
    PreparedStatement ps;
    Dbcon db = new Dbcon();
    con = db.getCon();
    int status=1;
    int userid =Integer.parseInt(request.getParameter("userid"));
    try {
        ps=con.prepareStatement("update tbluser set status = '"+status+"' where userid = '"+userid+"'");
        ps.executeUpdate();
        int updateQuery = ps.executeUpdate();
        con.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    response.sendRedirect("ViewUser.jsp");
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.