Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Fetching records

Here, you will learn that how to fetch result for the given rollno. I am assuming that there is a table as given below:

  2.    (    “ROLLNO” NUMBER,
  3.     “NAME” VARCHAR2(40),
  4.     “RESULT” VARCHAR2(40),
  5.     “GRADE” VARCHAR2(40),
  7.    )
  8. /

We are assuming there are many records in this table. In this example, we are getting the data from the database in servlet and printing it. We are doing all the database logic in servlet for simplicity of the program. But it will be better to separate it from the servlet file.

Example of Fetching Result for the given rollno

In this example, we have create three files

  • index.html
  • web.xml


This page gets rollno from the user and forwards this data to servlet which is responsible to show the records based on the given rollno.

  1. <html>
  2. <body>
  3. <form action=“servlet/Search”>
  4. Enter your Rollno:<input type=“text” name=“roll”/><br/>
  5. <input type=“submit” value=“search”/>
  6. </form>
  7. </body>
  8. </html>

This is the servlet file which gets the input from the user and maps this data with the database and prints the record for the matched data. In this page, we are displaying the column name of the database along with data, so we are using ResultSetMetaData interface.

  1. import*;
  2. import java.sql.*;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.*;
  5. public class Search extends HttpServlet {
  6. public void doGet(HttpServletRequest request, HttpServletResponse response)
  7.             throws ServletException, IOException {
  8. response.setContentType(“text/html”);
  9. PrintWriter out = response.getWriter();
  10. String rollno=request.getParameter(“roll”);
  11. int roll=Integer.valueOf(rollno);
  12. try{
  13. Class.forName(“oracle.jdbc.driver.OracleDriver”);
  14. Connection con=DriverManager.getConnection(
  15. “jdbc:oracle:thin:@localhost:1521:xe”,“system”,“oracle”);
  16. PreparedStatement ps=con.prepareStatement(“select * from result where rollno=?”);
  17. ps.setInt(1,roll);
  18. out.print(“<table width=50% border=1>”);
  19. out.print(“<caption>Result:</caption>”);
  20. ResultSet rs=ps.executeQuery();
  21. /* Printing column names */
  22. ResultSetMetaData rsmd=rs.getMetaData();
  23. int total=rsmd.getColumnCount();
  24. out.print(“<tr>”);
  25. for(int i=1;i<=total;i++)
  26. {
  27. out.print(“<th>”+rsmd.getColumnName(i)+“</th>”);
  28. }
  29. out.print(“</tr>”);
  30. /* Printing result */
  31. while(
  32. {
  33. out.print(“<tr><td>”+rs.getInt(1)+“</td><td>”+rs.getString(2)+”
  34. </td><td>“+rs.getString(3)+”</td><td>“+rs.getString(4)+”</td></tr>”);
  35. }
  36. out.print(“</table>”);
  37. }catch (Exception e2) {e2.printStackTrace();}
  38. finally{out.close();}
  39. }
  40. }

web.xml file

This is the configuration file which provides information of the servlet to the container.

  1. <web-app>
  2. <servlet>
  3. <servlet-name>Search</servlet-name>
  4. <servlet-class>Search</servlet-class>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>Search</servlet-name>
  8. <url-pattern>/servlet/Search</url-pattern>
  9. </servlet-mapping>
  10. </web-app>

download this example (developed without IDE)
download this example (developed using Myeclipse IDE)
download this example (developed using Eclipse IDE)
download this example (developed using Netbeans IDE)

To connect java application with the Oracle database ojdbc14.jar file is required to be loaded. Put this jar file in WEB-INF/lib folder.