import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class syncro extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GB2312";
  Connection con=null;//共享连接
  private String NUMBER; //初始化全局变量
  private String USERNAME;
  private String PASSWORD;
  private String TOTAL;
  private String REALNAME;
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    //加载JDBC－ODBC桥接器
    try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }catch(ClassNotFoundException e){}
  }
  //执行HTTP Post请求
  public void doPost(HttpServletRequest request,HttpServletResponse response) throws
      ServletException,IOException
  {
    PrintWriter out=response.getWriter();
    response.setContentType(CONTENT_TYPE);//设置响应的MIME类型。
    out.print("<HTML>");
    out.print("<BODY>");
    Statement stmt=null;
    ResultSet rs=null;
    if(con==null)
    {
      try{
          con=DriverManager.getConnection("jdbc:odbc:fafa");
          stmt=con.createStatement();
          String sql="Select * from GUEST";
          rs=stmt.executeQuery(sql);
          out.print("<Table Border>");
          out.print("<TR>");
          out.print("<TH width=100>"+"NUMBER");
          out.print("<TH width=100>"+"USERNAME");
          out.print("<TH width=50>"+"PASSWORD");
          out.print("<TH width=50>"+"TOTAL");
          out.print("<TH width=50>"+"REALNAME");
          out.print("</TR>");
          //循环将结果集显示
while(rs.next())
          {
           out.print("<TR>");
           NUMBER=rs.getString("NUM");
           out.print("<TD>"+NUMBER+"</TD>");
           USERNAME=rs.getString("USERNAME");
           out.print("<TD>"+USERNAME+"</TD>");
           PASSWORD=rs.getString("PASSWORD");
           out.print("<TD>"+PASSWORD+"</TD>");
           TOTAL=rs.getString("TOTAL");
           out.print("<TD>"+TOTAL+"</TD>");
           REALNAME=rs.getString("REALNAME");
           out.print("<TD>"+REALNAME+"</TD>");
           out.print("</TR>");
          }
      out.print("</Table>");
         }catch(SQLException e){}
  }
  else
  {  //其他用户通过同步块使用这个连接
     synchronized(con)
     {
      try{
        stmt=con.createStatement();
        String sql="Select * from GUEST";
        rs=stmt.executeQuery(sql);
        out.print("<Table Border>");
        out.print("<TR>");
        out.print("<TH width=100>"+"NUMBER");
        out.print("<TH width=100>"+"USERNAME");
        out.print("<TH width=50>"+"PASSWORD");
        out.print("<TH width=50>"+"TOTAL");
        out.print("<TH width=50>"+"REALNAME");
        out.print("</TR>");
        while(rs.next())
           {
            out.print("<TR>");
            NUMBER=rs.getString("NUM");
            out.print("<TD>"+NUMBER+"</TD>");
            USERNAME=rs.getString("USERNAME");
            out.print("<TD>"+USERNAME+"</TD>");
            PASSWORD=rs.getString("PASSWORD");
            out.print("<TD>"+PASSWORD+"</TD>");
            TOTAL=rs.getString("TOTAL");
            out.print("<TD>"+TOTAL+"</TD>");
            REALNAME=rs.getString("REALNAME");
            out.print("<TD>"+REALNAME+"</TD>");
            out.print("</TR>");
           }
           out.print("</Table>");
         }catch(SQLException e){}
      }
  }
  out.println("</BODY>");
  out.println("</HTML>");
}
  //执行 HTTP Get 请求
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
      doPost(request,response);
  }
  //Clean up resources
  public void destroy() {
  }
}
