Problem with the servlet and jdbc
I want to search the database from servlet depend upon the column name ,if the result found then it has to display the result in a new jsp page . im not getting the output. im attaching code please give me the solution for this
--
Search.jsp
Code :
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Search organization</title>
</head>
<body>
<form action ="SearchOrg" method=post>
<br><br>
<br><br>
<h3>Search</h3> <input type ="text" name="search" size="50" >
<br><br>
<input type="submit" value="search" >
</form>
</body>
</html>
SearchOrg.java --//servlet class
Code java:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class SearchOrg extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config)
throws ServletException{
this.setConfig(config);
}
public void setConfig(ServletConfig config) {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out =response.getWriter();
response.setContentType("text/html");
String search_org = request.getParameter("search");
// out.println("<TD>Website</TD></TR>");
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/org_info","root","dhyuti");
Statement stmt ;
ResultSet rs;
stmt=(Statement) con.createStatement();
rs=stmt.executeQuery("select * from org_details");
boolean Records=rs.next();
//String org_name =rs.getString(1);
//String s=rs.getString(8);
if(!Records ){
out.println("<p> No Data found </p>");
}
else {
while(rs.next()){
if((rs.getString(1)).equalsIgnoreCase(search_org)){
out.println();
out.println("<HTML><HEAD><TITLE>Organization List</TITLE>");
out.println("</HEAD>");
out.println("<BODY bgColor=blanchedalmond text=#008000 topMargin=0>");
out.println("<P align=center><FONT face=Helvetica><FONT color=fuchsia style=\"BACKGROUND-COLOR: white\"><BIG><BIG>List of Organization.</BIG></BIG></FONT></P>");
out.println("<P align=center>");
out.println("<TABLE align=center border=1 cellPadding=1 cellSpacing=1 width=\"75%\">");
out.println("<TR>");
out.println("<TD>Organization Name</TD>");
out.println("<TR>");
out.println("<TD>" + rs.getString(1) + "</TD>");
//out.println("<TD>" + s + ">" + s + "</TD>");
out.println("</TR>");
out.println("</TABLE></P>");
out.println("<P> </P></BODY></HTML>");
}
}
}
rs.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
Re: Problem with the servlet and jdbc
1) Your servlet calls next without retrieving the results of this call.
Code java:
boolean Records=rs.next();
....
while(rs.next()){
2) You should be sure the first column returned is the one you want to search. I'd recommend explicitly stating this in your sql call ('select columnName1, columnName2 from tablename"). It would also be a better technique to let the database do the search for you ("SELECT columnName1, columnName2 FROM tablename WHERE columnName1='search'")
3) If neither of these work, I suggest setting up some println statements or step through the code in a debugger to illustrate the behavior a bit more.
Re: Problem with the servlet and jdbc
Thank you for your answer. now i want to search the name of the company from the table , but when i use query = select * from table_name ; it provides result only for exact matches , what to do if i want to search the name of the company by giving first letter of the company itself? i want to specify the name of the company from dynamically.
Re: Problem with the servlet and jdbc
This is more of an SQL question rather than a java question...look into using the operator 'LIKE' (SQL LIKE Operator)