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


public class dbtestserv extends HttpServlet 
{

    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
                // components out of which to build the connect string
                String driver   = "jdbc:oracle:thin";
                String user     = "sa";
                String password = "pass";
                String host     = "myhost";
                String port     = "1521";
                String sid      = "oTest";

                // build the connectstring out of the listed components
                String connectstring = driver + ":" + user + "/" + password + "@" + host + ":" + port + ":" + sid;
        	
                try 
                {
                        // load the JDBC driver
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        } catch (ClassNotFoundException e) {
                        System.err.println("Unable to load JDBC driver");
                        System.err.println("Exception: " + e.getMessage());
                }

                try 
                {
                        // connect to the database
                        Connection con = DriverManager.getConnection(connectstring);

                        // set the "content type" header of the response
                        res.setContentType("text/html");

                        // get the response's PrintWriter to return text to the client.
                        PrintWriter toClient = res.getWriter();
                        toClient.println("<HTML><HEAD></HEAD><BODY><H1>");

                        String myQuery = "select derivation " +
                                                         "from   mytable " +
                                                         "where  id = 118368 ";

                        // build the statement
                        Statement getDerivation = con.createStatement();

                        // run the query
                        ResultSet rs = getDerivation.executeQuery(myQuery);

                        // read the results & print them out
                        while (rs.next()) {
                                String derivation = rs.getString("DERIVATION");
                                toClient.println("DERIVATION is " + derivation);
                        }

                        // print the final line of HTML
                        toClient.println("</H1></BODY></HTML>");
                        toClient.close();
                        } catch (SQLException e) {
                        e.printStackTrace();
                        System.err.println("Unable to read derivation: " + e.getMessage());
                }
        }

}
