In this you will learn how to fetch results for the given roll no. We are assuming that there is a table given below:
CREATE TABLE "RESULT" ( "ROLLNO" NUMBER, "NAME" VARCHAR2(40), "RESULT" VARCHAR2(40), "GRADE" VARCHAR2(40), CONSTRAINT "RESULT_PK" PRIMARY KEY ("ROLLNO") ENABLE ) /
We are presuming that there are many records(data) in this table. We are going to get the data from the database in the servlet and print it in this given example. We are doing all the database login in the servlet for clarity of the program.If you separate it from the servlet file then it will be better.
We have created 3 files in this example
Index.html
Getting data from the user and transferring this data to the Servlet is done by this page which is responsible to view the records based on the given roll no.
<html> <body> <form action="servlet/Search"> Enter your Rollno:<input type="text" name="roll"/><br/> <input type="submit" value="search"/> </form> </body> </html>
Search.java
This is the servlet file which obtains the input from the user and maps this data with the database and prints the record from the matched data. In this page, we are showing the column name of the database through data. So, we are making use of the ResultSetMetaData interface.
import java.io.*; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.http.*; public class Search extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String rollno=request.getParameter("roll"); int roll=Integer.valueOf(rollno); try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); PreparedStatement ps=con.prepareStatement("select * from result where rollno=?"); ps.setInt(1,roll); out.print("<table width=50% border=1>"); out.print("<caption>Result:</caption>"); ResultSet rs=ps.executeQuery(); /* Printing column names */ ResultSetMetaData rsmd=rs.getMetaData(); int total=rsmd.getColumnCount(); out.print("<tr>"); for(int i=1;i<=total;i++) { out.print("<th>"+rsmd.getColumnName(i)+"</th>"); } out.print("</tr>"); /* Printing result */ while(rs.next()) { out.print("<tr><td>"+rs.getInt(1)+"</td><td>"+rs.getString(2)+" </td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td></tr>"); } out.print("</table>"); }catch (Exception e2) {e2.printStackTrace();} finally{out.close();} } }
Web.xml file
Information of the Servlet to the Container is provided by this configuration file.
<web-app> <servlet> <servlet-name>Search</servlet-name> <servlet-class>Search</servlet-class> </servlet> <servlet-mapping> <servlet-name>Search</servlet-name> <url-pattern>/servlet/Search</url-pattern> </servlet-mapping> </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 a java application with the oracle database, an ojdbc14.jar file is needed to be loaded. Put this jar file in WEB-INF/lib folder.