Hi friends,

I create the data source Books.mdb as a System DSN on my machine,and I
try to use JdbcServlet to retrieve the data from the Arthurs table.

when I invoke it using http://localhost:8080/servlet/JdbcServlet,it gave
me blank page.I use java web server2.0.

Can someone give me a idea?

thanks.

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

/**
 * JdbcServlet reads customer information from an ODBC data
 * source and presents it to the client in an HTML table.
 *
 */
public class JdbcServlet extends HttpServlet
{
  //database connection is shared by all requests
  static Connection dbConn;

  /**
   * init method is called when servlet is initialized.
   * Establishes a database connection when servlet is initially
   * loaded that can be shared across all requests.
   */
  public void init(ServletConfig config) throws
    ServletException
  {
    super.init(config); //pass ServletConfig to parent

    try
    {
      //load JDBC-ODBC Bridge driver
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      //establish database connection to Books using ODBC
      dbConn = DriverManager.getConnection(
        "jdbc:odbc:Books");
    }
    catch (ClassNotFoundException e) //Class.forName throws
    {
      System.out.println("JDBC-ODBC bridge not found!");
      return;
    }
    catch (SQLException e) //DriverManager.getConnection throws
    {
      System.out.println("SQL exception thrown in init!");
      return;
    }
  }


  /**
   * doGet method is called in response to a GET request
   */
  public void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException,
    IOException
  {
    try
    {
      response.setContentType("text/html"); //returns HTML

      //get handle to output stream
      PrintWriter out = response.getWriter();

      //create statement
      Statement stat = dbConn.createStatement();
      //query database for result set
      ResultSet customers = stat.executeQuery(
        "SELECT AuthorID, FirstName, LastName, YearBorn FROM " +
        "Authors");

      //generate HTML document to return to client
      out.println("<HTML>");
      out.println("<HEAD><TITLE>author list</TITLE></HEAD>");
      out.println("<BODY>");
      out.println("<H2>author List</H2>");
      out.println("<TABLE BORDER=1>"); //create an HTML table
      out.println("<TR><TH>Author ID</TH>");
      out.println("<TH>first Name</TH>");
      out.println("<TH>last name</TH>");
      out.println("<TH>year born</TH></TR>");

      while (customers.next()) //iterate through all records
      {
        //add a table row for each record
        out.println("<TR><TD>" +
          customers.getInt("AuthorID")+ "</TD><TD>" +
          customers.getString("FirstName") + "</TD><TD>" +
          customers.getString("LastName") + "</TD><TD>" +
          customers.getString("YearBorn") + "</TD></TR>");
      }

      out.println("</TABLE>");
      out.println("</BODY></HTML>");
      out.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  /**
   * Tells the server about this servlet
   */
  public String getServletInfo()
  {
    return "Sample JDBC servlet";
  }
}

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to