【问题标题】:Printing SQL select result set directly onto HTML webpage?将 SQL 选择结果集直接打印到 HTML 网页上?
【发布时间】:2018-04-04 18:36:09
【问题描述】:

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

 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());
        }
    }
}

【问题讨论】:

  • 那么您使用的是哪些网络技术?如果您使用的是 JSP,您可以在页面上打印一个表格。
  • 没有 JSP,只有普通的 servlet。
  • 如果我只是用 Print Writer 打印结果集,结果只是一个带有一堆随机字母和数字的对象名称。

标签: java html mysql database resultset


【解决方案1】:

下面是参考您的servlet响应代码的示例,用于使用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();

} 

【讨论】:

    【解决方案2】:

    一种方法是从 servlet 响应中获取编写器,然后编写所需的 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());
        }
    }
    

    【讨论】:

      【解决方案3】:

      你需要返回结果集。

      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>");
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-26
        • 1970-01-01
        • 2010-12-28
        • 2012-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多