将 SQL select 结果集直接打印到 HTML 网页上?

Printing SQL select result set directly onto HTML webpage?

我如何重写下面的代码以直接在我的网页上而不是在控制台上打印结果?

 public static void doSQL() {
        try {
            String url = "jdbc:msql://...";
            Connection conn = DriverManager.getConnection(url,"user","password");
            Statement stmt = conn.createStatement();
            ResultSet rs;

            rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");
            while ( rs.next() ) {

                // I want to print the ResultSet directly on my HTML page, how may I go about doing that?
                String lastName = rs.getString("Lname");
                System.out.println(lastName);
            }
            conn.close();
        } catch (Exception e) {

            System.err.println("Got an exception! ");
            System.err.println(e.getMessage());
        }
    }
}

您需要 return 结果集。

public static ResultSet doSQL() {
        try {
            String url = "jdbc:msql://...";
            Connection conn = DriverManager.getConnection(url,"user","password");
            Statement stmt = conn.createStatement();
            ResultSet rs;

            rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");

            conn.close();
            return rs;
        } catch (Exception e) {

            System.err.println("Got an exception! ");
            System.err.println(e.getMessage());
        }

        return null
    }

在您的 servlet 中,获取 doSQL();

ResultSet rs = MyStaticFile.doSQL();
int rowCount = 0;

 out.println("<P ALIGN='center'><TABLE BORDER=1>");
 ResultSetMetaData rsmd = rs.getMetaData();
 int columnCount = rsmd.getColumnCount();
 // table header
 out.println("<TR>");
 for (int i = 0; i < columnCount; i++) {
   out.println("<TH>" + rsmd.getColumnLabel(i + 1) + "</TH>");
   }
 out.println("</TR>");
 // the data
 while (rs.next()) {
  rowCount++;
  out.println("<TR>");
  for (int i = 0; i < columnCount; i++) {
    out.println("<TD>" + rs.getString(i + 1) + "</TD>");
    }
  out.println("</TR>");
  }
 out.println("</TABLE></P>");

这不是一个完整的例子,还没有完全测试。此参考将使您了解如何操作。顺便说一句,不要对数据库操作使用静态。您可以用于学习目的。

一种方法是从servlet响应中获取一个writer,然后写入HTML你想要的内容。我对您的 doSQL() 方法进行了轻微重构,以接受 PrintWriter 作为参数。

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
    PrintWriter pw = response.getWriter();
    doSQL(pw);
}

public static void doSQL(PrintWriter pw) {
    try {
        String url = "jdbc:msql://...";
        Connection conn = DriverManager.getConnection(url,"user","password");
        Statement stmt = conn.createStatement();
        ResultSet rs;

        rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");
        pw.println("<html><table>");
        while (rs.next()) {
            // you only select one field, but you can easily adapt
            // this code to have more fields (i.e. table columns)
            String lastName = rs.getString("Lname");
            pw.println("<tr><td>" + lastname + "</td></tr>");
        }
        pw.println("</table></html>");
        conn.close();
    } catch (Exception e) {

        System.err.println("Got an exception! ");
        System.err.println(e.getMessage());
    }
}

下面是参考您的 servlet 响应代码的示例,如 html 用于使用 GET 方法的 html 请求,类似地,您可以为 POST 方法编写。在 servlet 中添加以下代码。

  public void doGet(HttpServletRequest request, HttpServletResponse 
  response) throws IOException { 
PrintWriter out = response.getWriter(  ); 
response.setContentType("text/html"); 
    out.println("<html>");
    out.println("<head>");

out.println("<title> Any Title </title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<H1>Last Name from a Servlet</H1></br>"); 
try {
        String url = "jdbc:msql://...";
        Connection conn = DriverManager.getConnection(url,"user","password");
        Statement stmt = conn.createStatement();
        ResultSet rs;

        rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");
        while ( rs.next() ) {

            // I want to print the ResultSet directly on my HTML page, how may I go about doing that?
            String lastName = rs.getString("Lname");
            out.println("<H1>"+ lastName +"</H1></br>");
            System.out.println(lastName);
        }
        conn.close();
    } catch (Exception e) {

        System.err.println("Got an exception! ");
        System.err.println(e.getMessage());
    }

      out.println("</body>");
      out.println("</html>");

      out.close();

}