World's most popular travel blog for travel bloggers.

Write and run Servlet code to fetch and display all the fields of a student table stored in an Oracle database with attributes: (students id, student-name, student - address, program, semester )

, , No Comments

Steps to Fetch Student Data from Database

Create Html Page


index.html

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content="width=device-width">
    </head>
    <body>
       <form action="Search">
Enter your Name: <input type="text" name="uname"/><br/>
<input type="submit" value="search"/>
</form>
    </body>
</html>





Create Java Servlet

Search.java


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 name=request.getParameter("uname");                          
              try{
                     Class.forName("oracle.jdbc.driver.OracleDriver");
                     Connection con=DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe","sandeep","welcome");               
                     PreparedStatement ps=con.prepareStatement("select * from userlogin where name=?");
                     ps.setString(1,name);                   
                     out.print("<table width=25% border=1>");
                     out.print("<center><h1>Result:</h1></center>");
                     ResultSet rs=ps.executeQuery();                
                     /* Printing column names */
                     ResultSetMetaData rsmd=rs.getMetaData();
                     while(rs.next())
                        {
                     out.print("<tr>");
                     out.print("<td>"+rsmd.getColumnName(1)+"</td>");
                        out.print("<td>"+rs.getString(1)+"</td></tr>");
                        out.print("<tr><td>"+rsmd.getColumnName(2)+"</td>");
                        out.print("<td>"+rs.getString(2)+"</td></tr>");
                        out.print("<tr><td>"+rsmd.getColumnName(3)+"</td>");
                        out.print("<td>"+rs.getString(3)+"</td></tr>");
                        out.print("<tr><td>"+rsmd.getColumnName(4)+"</td>");
                        out.print("<td>"+rs.getString(4)+"</td></tr>");                  
                     }
                     out.print("</table>");

              }catch (Exception e2)
                {
                    e2.printStackTrace();
                }

              finally{out.close();
                }
       }

} 

Compile your servlet code and add to Apache Directory > web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>Search</servlet-name>
        <servlet-class>Search</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Search</servlet-name>
        <url-pattern>/Search</url-pattern>
    </servlet-mapping>
</web-app> 



0 comments:

Post a Comment

Let us know your responses and feedback