Thursday, 1 October 2015

Example of Fetching Result for the given rollno

Example of Fetching Result for the given rollno

Here, you will learn that how to fetch result for the given rollno. I am assuming that there is a table as given below:
  1. CREATE TABLE  "RESULT"   
  2.    (    "ROLLNO" NUMBER,   
  3.     "NAME" VARCHAR2(40),   
  4.     "RESULT" VARCHAR2(40),   
  5.     "GRADE" VARCHAR2(40),   
  6.      CONSTRAINT "RESULT_PK" PRIMARY KEY ("ROLLNO") ENABLE  
  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
  • Search.java
  • web.xml

index.htmlThis 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.   
  6. <input type="submit" value="search"/>  
  7. </form>  
  8. </body>  
  9. </html>  

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

web.xml fileThis is the configuration file which provides information of the servlet to the container.
  1. <web-app>  
  2.   
  3. <servlet>  
  4. <servlet-name>Search</servlet-name>  
  5. <servlet-class>Search</servlet-class>  
  6. </servlet>  
  7.   
  8. <servlet-mapping>  
  9. <servlet-name>Search</servlet-name>  
  10. <url-pattern>/servlet/Search</url-pattern>  
  11. </servlet-mapping>  
  12.   
  13. </web-app>  



No comments:

Post a Comment