import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;


public class Selection extends HttpServlet
{
   public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
   {

      response.setContentType("Text/html");
      PrintWriter out = response.getWriter();

       //creates the html page with Simple Oracle Example title and Student heading	
      out.println("<HTML>");
      out.println("<HEAD><TITLE>Simple Oracle Example</TITLE></HEAD>");
      out.println("<BODY BGCOLOR=\"#FFFFFF\">");
      out.println("<CENTER>");
      out.println("<B>Student</B>");
      out.println("<BR><BR>");

      Connection conn = null;
      try
      {
	 //load the oracle driver
         Class.forName("oracle.jdbc.driver.OracleDriver");//Oracle driver
         
	//connect to the database
	 conn = DriverManager.getConnection(
                   "jdbc:oracle:thin:@localhost:1521:databasename","sqlpluslogin","sqlpluspassword");
	
	//create a statement object
         Statement stmt = conn.createStatement();

	//execute select statement(to get records from the table student)	
	 ResultSet rs = stmt.executeQuery("SELECT * FROM Student");

         //Print start of table and column headers
         out.println("<TABLE CELLSPACING=\"0\" CELLPADDING=\"3\" BORDER=\"1\">");
         out.println("<TR><TH>ID</TH><TH>NAME</TH></TR>");

         //Loop through results of query.
         while(rs.next())
         {
            out.println("<TR>");
            out.println("<TD>" + rs.getString(1) + "</TD>");
            out.println("<TD>" + rs.getString(2) + "</TD>");
            out.println("</TR>");
         }

         out.println("</TABLE>");
      }
      catch(SQLException e)
      {
         out.println("SQLException: " + e.getMessage() + "<BR>");
         while((e = e.getNextException()) != null)
            out.println(e.getMessage() + "<BR>");
      }
      catch(ClassNotFoundException e)
      {
         out.println("ClassNotFoundException: " + e.getMessage() + "<BR>");
      }
      finally
      {
         //Clean up resources, close the connection.
         if(conn != null)
         {
            try
            {
               conn.close();
            }
            catch (Exception ignored) {}
         }
      }

      out.println("</CENTER>");
      out.println("</BODY>");
      out.println("</HTML>");

   }
}

