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

public class DBConnect extends HttpServlet
{
        public void doGet(HttpServletRequest req, HttpServletResponse res)
                          throws ServletException, IOException
        {
                Connection con = null;
                Statement stmt = null;
                ResultSet rs = null;

                res.setContentType("text/html");
                PrintWriter out = res.getWriter();

                try
                {
                        //Load and therefore register the driver
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                        //Get connection to the database
                        con = DriverManager.getConnection("jdbc:odbc:norma", "", "");

                        //Create a statement object
                        stmt = con.createStatement();

                        //Execute an SQL query, get a ResultSet
                        rs = stmt.executeQuery("SELECT NAME,PHONENUM FROM SUBSCRIBERS");

                        //Display the result set as a list
                        out.println("<HTML><HEAD><TITLE>Norma's subscriber list...</TITLE></HEAD>");
                        out.println("<BODY background=\"http://dl1487-6c6/norma/images/bground.gif\"><BR><BR><BR><BR><BR><BR><UL>");
                        while(rs.next())
                        {
                                out.println("<LI>" + rs.getString("userid") + " " + rs.getString("password"));
                        }
                        out.println("</UL>");
                        out.println("</BODY></HTML>");
                }
                catch (ClassNotFoundException e)
                {
                        out.println("Could not load the database driver: " + e.getMessage());
                }
                catch (SQLException e)
                {
                        out.println("SQLException caught: " + e.getMessage());
                }
                finally
                {
                        //Close all database connection.
                        try
                        {
                                if (con != null)
                                        con.close();
                        }
                        catch (SQLException e)
                        {
                                out.println("SQLException caught: " + e.getMessage());
                        }
                }
        }
}