• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Program trying to search and then updating the records  RSS feed

 
Tushar Gosalia
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
}
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tushar Gosalia
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
suki sahu
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you should write
pst_updt.executeUpdate();
 
Tushar Gosalia
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Buddy see the 6th line from update code..I have written that only..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!