Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with the servlet and jdbc

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow 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
    <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


     
    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>&nbsp;</P></BODY></HTML>");
     
     
    				    }
     
     
    			}   
    				  }
     
    		rs.close();
    		con.close();
    		}catch(Exception e){
     
    				      e.printStackTrace();
     
    				  }	 
     
    				}
    	}
    Last edited by copeg; September 7th, 2010 at 08:45 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,336
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Problem with the servlet and jdbc

    1) Your servlet calls next without retrieving the results of this call.
    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.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,336
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default 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)

Similar Threads

  1. What are the requirements to develop Servlet Application?
    By yousef atya in forum Java Servlet
    Replies: 2
    Last Post: July 28th, 2011, 06:20 PM
  2. jdbc connection problem
    By sny in forum JDBC & Databases
    Replies: 11
    Last Post: January 6th, 2011, 03:39 AM
  3. Problem With JDBC
    By il912 in forum JDBC & Databases
    Replies: 7
    Last Post: October 17th, 2009, 03:59 AM
  4. JNDI - JDBC Integration problem
    By rangarajank in forum JDBC & Databases
    Replies: 0
    Last Post: September 29th, 2009, 06:07 AM
  5. JDBC Problem
    By seejay in forum JDBC & Databases
    Replies: 1
    Last Post: September 16th, 2009, 07:55 AM

Tags for this Thread