<html>
<body>
<%@page import="java.sql.*"%>
<form method="post" action="csea.jsp">
<table border="1">
<tr><th>Reg No</th><th>Name</th><th>Present/Absent</th></tr>
<%
      String pa = request.getParameter("pa");
      try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      Connection con = DriverManager.getConnection("jdbc:odbc:Student");
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("select * from student_details");
      while (rs.next()) {
%>
<tr><th><%=rs.getString("reg_no")%></th><th><%=rs.getString("s_name")%></th>
     <th><input type="text" name="pa"/></th>
</tr>
<%
       }
       st.executeUpdate("update csea  set pa='"+pa+"'");
       con.close();
       st.close();
       rs.close();
    } catch (Exception e) {
 }%>
</table>
<input type="submit" name="submit" value="SUBMIT"/>
</form>
</body>
</html>

In this code the problem am facing is that when am insert the student status as "present" then it the status is updating to all s_name column

example: suppose they are 3 student are their in msaccess table
reg_no s_name present/absent
1 x present
2 Y present
3 z present
now i want put status as present, absent and present but by this code the all 3 student is showing all present.

Regards,
Abrar.

Dani AI

Generated

Short diagnosis: the page both renders the form and runs an unconditional UPDATE using a single status value, so every row ends up with the same value. As noted, the update must happen only after the form is submitted and each student row needs a way to be identified when the server processes the form. The current code updates the whole table because the UPDATE lacks a row selector and the handler is using one value for all rows.

Recommended approach (minimal, reliable changes):

  • Render the list so each displayed student also emits that student id (hidden input) and a status input. Use identical names for the pairs so the servlet/JSP can receive parallel arrays (e.g., ids and statuses) where the same index corresponds to the same student.
  • Only run update logic when the request is a POST (or when a submit flag is present).
  • Use a PreparedStatement with a WHERE clause to update each student individually, and execute updates in a loop or as a batch.

A concise server-side pattern to process arrays and do safe updates:

<%
if ("POST".equalsIgnoreCase(request.getMethod())) {
    String[] ids = request.getParameterValues("ids");
    String[] statuses = request.getParameterValues("statuses");
    if (ids != null && statuses != null && ids.length == statuses.length) {
        try (Connection con = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement ps = con.prepareStatement(
                 "UPDATE student_details SET pa = ? WHERE reg_no = ?")) {
            for (int i = 0; i < ids.length; i++) {
                ps.setString(1, statuses[i]);
                ps.setString(2, ids[i]);
                ps.addBatch();
            }
            ps.executeBatch();
        }
    }
}
%>

Extra notes: use parameterized queries to avoid injection, close resources (try-with-resources), validate submitted values (only allow expected strings like "present"/"absent"), and perform a redirect after successful POST to avoid duplicate submissions. Also confirm the correct table name in the UPDATE and, if running a modern JVM, prefer a maintained JDBC driver for Access rather than the old bridge.

alright well from what i am reading, you create a table with a row for each students , but put no value in the textbox for the "present/absent" column. Then, you immidiatly update the table with the query :

"update csea set pa='"+pa+"'"

where pa is the parameter from the request.

and finish by adding a button that submits to csea.jsp

Now the problem is that the update statement should be in csea.jsp, not in your current page. also is "csea" also a table in your database? because im just assuming that your update query should update the "student_details" table instead of csea.

also, i guess id give different names for the textboxes instead of "pa" for all of them. Like "pa<%=rs.getString("reg_no")%>" so then you could loop through your students in csea.jsp and fetch "pa"+studentnumber from your request, and if its not null, update that single student with its value.

What is in csea.jsp?

EDIT : i just understood that this page IS csea.jsp , correct? and here is what is happening, the first time the page loads, it prints the form with nothing in textboxes, then updates all students with no info (i still dont understand how your query string can update the table "csea" and not "student_details" but whatever), THEN you fill the form and click the submit button, which calls back the page, prints out the form (with empty textboxes again) and then calls the update query with the request.parameter "pa" , which actually should return an array of parameters since there would be a total of 3 textboxes with the name "pa", (from my experiences with vb and classic asp anyways, I have never tried to retrieve such an array of parameters with jsp but i am assuming it would act somewhat like that).... but the query only updates the table with the first value of the 3 values in the array...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.