使用共享连接的servlet源文件
ShareInquire.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class ShareInquire extends HttpServlet
{  Connection con=null; //共享连接。
  public void init(ServletConfig config) throws ServletException
    {super.init(config);
      //加载JDBC-ODBC桥接器：
       try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          }
       catch(ClassNotFoundException e){}
    }
   //通过Post方法按名字查询记录：
  public  void doPost(HttpServletRequest request,HttpServletResponse response) 
                        throws ServletException,IOException
  { PrintWriter out=response.getWriter();
    response.setContentType("text/html;charset=GB2312");//设置响应的MIME类型。
    out.println("<HTML>"); 
    out.println("<BODY>");
    Statement sql=null; 
    ResultSet rs=null; 
    if(con==null) 
    { try
       {   //第一个用户负责建立连接con。
           con=DriverManager.getConnection("jdbc:odbc:sun","sa","");
           sql=con.createStatement();
           String condition="SELECT * FROM students";
           rs=sql.executeQuery(condition);
        out.print("<Table Border>");
            out.print("<TR>");
            out.print("<TH width=100>"+"Number");
            out.print("<TH width=100>"+"Name");
            out.print("<TH width=50>"+"Math");
            out.print("<TH width=50>"+"English");
            out.print("<TH width=50>"+"Phsics");
            out.print("</TR>");
       while(rs.next())
           { out.print("<TR>");
             out.print("<TD >"+rs.getString(1)+"</TD>"); 
             out.print("<TD >"+rs.getString(2)+"</TD>");
             out.print("<TD >"+rs.getInt("数学成绩")+"</TD>"); 
             out.print("<TD >"+rs.getInt("英语成绩")+"</TD>");
             out.print("<TD >"+rs.getInt("物理成绩")+"</TD>");   
             out.print("</TR>") ;          
            }
         out.print("</Table>");
       }
     catch(SQLException e)
          {
          }
    }
   //其它客户通过同步块使用这个连接：
   else
    { synchronized(con)
       {try{ sql=con.createStatement();
             String condition="SELECT * FROM students";
             rs=sql.executeQuery(condition);
             out.print("<Table Border>");
             out.print("<TR>");
             out.print("<TH width=100>"+"Number");
             out.print("<TH width=100>"+"Name");
             out.print("<TH width=50>"+"Math");
             out.print("<TH width=50>"+"English");
             out.print("<TH width=50>"+"Phsics");
             out.print("</TR>");
             while(rs.next())
              { out.print("<TR>");
                out.print("<TD >"+rs.getString(1)+"</TD>"); 
                out.print("<TD >"+rs.getString(2)+"</TD>");
                out.print("<TD >"+rs.getInt("数学成绩")+"</TD>"); 
                out.print("<TD >"+rs.getInt("英语成绩")+"</TD>");
                out.print("<TD >"+rs.getInt("物理成绩")+"</TD>");   
                out.print("</TR>") ;          
              }
            out.print("</Table>");
          }
        catch(SQLException e)
          {
          }
       }
    }
   out.println("</BODY>");
   out.println("</HTML>");
 }
  public void doGet(HttpServletRequest request,HttpServletResponse response) 
                        throws ServletException,IOException
 {  doPost(request,response);
 }
}
