来源(null)

// servlet Query0 as first 3tier example

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

public class Query0 extends httpServlet{
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
    res.setContentType("text/html");
    PrintWriter out=res.getWriter();
    String theName=req.getParameter("theName");
    // theName is the NAME attribute of the input field in submitting from
    String queryResult=lookup(theName); //does all database work
    String title="Query result: Phone for "+ theName;
    wrapInHTMLPage(out,queryResult,title);      // does all output HTML work
  }
  public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException{
    doGet(req,res);
  }
  public void wrapInHTMLPage(PrintWriter out,String queryResult,String title) throws IOException{
    out.println("<html><head><title>" + title + "</title></head>");
    out.println("<body><h1>" + title + "</h1>");
    out.println(queryResult);
    out.println("</body></html>");
  }
  public String lookup(String key){
    // variables for database work
    String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
    String dbURL="jdbc:odbc:PhoneBook";
    String queryString="SELECT THENUMBER FROM PHONEBOOK WHERE THENAME='" + key + "';";
    // if key is "Jane Doe " then queryString is
    // "SELECT THENUMBER FROM PHONEBOOK WHERE THENAME='Jane Doe'"
    try{
      Class.forName(driverName);        // load the driver
      Connection con=DriverManager.getConnection(dbURL,"usr","pwd");
      Statement stmt=con.createStatement();
      ResultSet rs=stmt.executeQuery(queryString);
      if(null==rs){
        return "db failure on "+ key;
      }
      if(!rs.next()){
        return "No such name as " + key;        // empty result set
      }
      return rs.getString(1);
    }catch(java.long.Exception ex){
      ex.printStackTrace();
      return null;
    }
  }
}


Link: http://www.asm32.net/article_details.aspx?id=97