Forums Register Login

Servlet Program trying to search and then updating the records

+Pie Number of slices to send: Send
But i'm facing problem during updating the records..Here is the code

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;

public class HttpSearch extends HttpServlet
{
Connection con;
PreparedStatement pst,pst_updt;
ResultSet rs;

public void init(ServletConfig sc) throws ServletException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://locajana");
pst=con.prepareStatement("select * from insert_table where Roll_No=? ",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);//No Problem with searching
pst_updt=con.prepareStatement("update insert_table set Name=?, Address=? where Roll_No=? ",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
System.out.println("HTTP SEARCH IS INITIALIZED");
}
catch(ClassNotFoundException e)
{
System.out.println("Class Not Found"+e.getMessage());
e.printStackTrace();
}
catch(SQLException e)
{
System.out.println("Sql Exception"+e.getMessage());
e.printStackTrace();
}
}

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
try
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
int rollno=Integer.parseInt(req.getParameter("roll"));// Search code
pst.setInt(1,rollno);
rs=pst.executeQuery();
if(rs.next())
{
String name=rs.getString("Name");
String address=rs.getString("Address");
pw.println("<html>");
pw.println("<body bgcolor='black' align='center'>");
pw.println("<form action='http://localhost:9090/Test/HttpSearch'>");
pw.println("<h1><font face='Andalus' color='red'>Search Result</font></h1>");
pw.println("<table align='center' border='1' bordercolor='gray'>");
pw.println("<tr>");
pw.println("<td> <font face='Andalus' color='red'>RollNo </font></td>");
pw.println("<td colspan='2'> <input type='text' value="+rollno+" name='updt_roll'> </td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td> <font face='Andalus' color='red'>Name </font></td>");
pw.println("<td colspan='2'> <input type='text' value="+name+" name='updt_name'> </td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td> <font face='Andalus' color='red'>Address </font></td>");
pw.println("<td> <input type='text' value="+address+" name='updt_address'> </td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td colspan='2' align='center'> <input type='submit' value='Update' style='height: 40px; width: 80px'> </td>");
pw.println("</tr>");
pw.println("</table>");
pw.println("<br><br>");
pw.println("<font color='red' face='Andalus' size='12px'><a href='http://localhost:9090/Test/HttpSearch.html'>Search More Record</a></font>");
pw.println("</form>");
pw.println("</body>");
pw.println("</html>");
}
else
{
pw.println("<html>");
pw.println("<body bgcolor='black' align='center'>");
pw.println("<h1><font face='Andalus' color='red'>Error</font></h1><br><br>");
pw.println("<font face='Andalus' color='red' size='5Search Record Found</font><br>");
pw.println("<p align='left'><font color='red' face='Andalus' size='7px'><a href='http://localhost:9090/Test/HttpSearch.html'>Search Again</a></font></p>");
}

// Update code
String updt_name=req.getParameter("updt_name");
String updt_address=req.getParameter("updt_address");
pst_updt.setInt(3,rollno);
pst_updt.setString(1,updt_name);
pst_updt.setString(2,updt_address);
int r_updt=pst.executeUpdate();
if(r_updt>=1)
{
pw.println("<html>");
pw.println("<body bgcolor='black' align='center'>");
pw.println("<h1><font face='Andalus' color='red'>Record Is Updated</font></h1><br><br>");
}
else
{
pw.println("<html>");
pw.println("<body bgcolor='black' align='center'>");
pw.println("<h1><font face='Andalus' color='red'>Record Not Updated</font></h1><br><br>");
}

System.out.println("HTTP SEARCH IS CALLED");
}
catch(Exception e)
{
System.out.println("Exception"+e.getMessage());
e.printStackTrace();
}
}
}
+Pie Number of slices to send: Send
Your comment near the beginning says "No problem with searching" but it's next to the Update SQL, not the Select SQL. I'm guessing (since you didn't provide any information about your problem except the code) that you think you're using the same value of the Roll_No field in both SQL statements. But you aren't. You use different values. Perhaps that explains why no records are updated -- but again, I'm just guessing that is your problem.

If your problem is something else then please feel free to let us know what it is.
+Pie Number of slices to send: Send
When i run this code i get error saying Cannot issue executeUpdate() for SELECTs..But I used executeQuery() for select sql and executeUpdate() for update sql.
+Pie Number of slices to send: Send
i think you should write
pst_updt.executeUpdate();
+Pie Number of slices to send: Send
Buddy see the 6th line from update code..I have written that only..
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 925 times.
Similar Threads
How To Execute Servlet From JSP
update of records
wrong jar reference
JDBC code with type 1 driver not working
JSP two pages
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:22:48.