就是说,我在FillGrid.java中定义了FillGrid类;
在SearchBook.java中的SearchBook类中使用了FillGrid类
用的编译器是:JDK自己的javac

解决方案 »

  1.   

    //======================= FillGrid.java =====================
    package library;import java.sql.*;
    public class FillGrid
    {
       private Connection con;
       private String sql;
       public FillGrid(Connection con, String sql)
       {
       this.con = con;
       this.sql = sql;
       }
       public String toString()
       {
       StringBuffer sb = new StringBuffer();
       try
       {
       Statement statement = con.createStatement();
       if (statement.execute(sql))
       {
       ResultSet rs = statement.getResultSet();
       ResultSetMetaData md = rs.getMetaData();
       int colCount = md.getColumnCount();
       sb.append("<TABLE><TR>");
       for (int i = 1; i <= colCount; i++)
            sb.append("<TH>" + md.getColumnLabel(i));
       while (rs.next())
       {
       sb.append("<TR>");
       for (int i = 1; i <= colCount; i++)
       {
       sb.append("<TD>");
       Object obj = rs.getObject(i);
       if (obj != null)
       sb.append(obj.toString());
       else
       sb.append("&nbsp;");
       }
       }
       sb.append("</TABLE>\n");
             }
           else
             sb.append("<B>Update Count:</B> " + statement.getUpdateCount());
           }
         catch (SQLException e)
           {
           sb.append("</TABLE><H1>SQL异常错误:</H1> " + e.getMessage() + sql);
           }
           return sb.toString();
           }
    }//======================= SearchBook.java ========================
    package library;import java.io.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class SearchBook extends HttpServlet
    {
       private Connection con = null;
    //
       public void init(ServletConfig config)  throws ServletException
         {
         super.init(config);
         try
         {
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           con = DriverManager.getConnection("jdbc:odbc:LibraryBook");
         }
         catch (ClassNotFoundException e)
         {
         throw new UnavailableException(this,"无法加载数据库驱动程序");
         }
         catch (SQLException e)
         {
           throw new UnavailableException(this,"无法与制定数据库联机");
         }
        }
    //
       public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
         {
         String Operator = req.getParameter("Condition");
         String Condition = "";
         //书号
         String Value = req.getParameter("BookNO");
         if (Value.compareTo(new String(""))!=0)
         {
         if (Condition.compareTo(new String(""))==0)
         {
         Condition = "书号 like '" + Value + "%'";
         }
         else
         {
         Condition = Condition + "书号 like '" + Value + "%'";
         }
         }
         //图书分类号
         Value = req.getParameter("BookTypeNO");
         if (Value.compareTo(new String(""))!=0)
         {
         if (Condition.compareTo(new String(""))==0)
         {
         Condition = "图书分类号 like '" + Value + "%'";
         }
         else
         {
         Condition = Condition + "图书分类号 like '" + Value + "%'";
         }
         }
         //书名
         Value = req.getParameter("BookName");
         if (Value.compareTo(new String(""))!=0)
         {
         if (Condition.compareTo(new String(""))==0)
         {
         Condition = "书名 like '" + Value + "%'";
         }
         else
         {
         Condition = Condition + "书名 like '" + Value + "%'";
         }
         }
         //作者
         Value = req.getParameter("BookWriter");
         if (Value.compareTo(new String(""))!=0)
         {
         if (Condition.compareTo(new String(""))==0)
         {
         Condition = "作者 like '" + Value + "%'";
         }
         else
         {
         Condition = Condition + "作者 like '" + Value + "%'";
         }
         }
         //出版社
         Value = req.getParameter("BookPressName");
         if (Value.compareTo(new String(""))!=0)
         {
         if (Condition.compareTo(new String(""))==0)
         {
         Condition = "出版单位 like '" + Value + "%'";
         }
         else
         {
         Condition = Condition + "出版单位 like '" + Value + "%'";
         }
         }
        
    String sql;
         if (Condition.compareTo(new String(""))==0)
         {
         String CommandTitle = "Select * from Book Where  ";
         sql = CommandTitle + "(" + Condition + ")";
         }
         else
         {
         sql = "Select * from Book ";
         }
        
         res.setContentType("text/html");
         ServletOutputStream sos=res.getOutputStream();
         PrintWriter out=new PrintWriter(sos,true);
         out.println("<HTML><HEAD><TITLE>书籍查询结果</TITLE></HEAD>");
         out.println("<BODY>");
         FillGrid result = new FillGrid(con, sql);//生成表格
         out.println(result);
         out.println("</BODY></HTML>");
         }
    //
       public void destroy()
         {
         try 
         { 
         if (con != null) 
         con.close(); 
         }
         catch (SQLException ignored)
         {
         // 
         }
         }
    }