简体   繁体   English

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

[英]Printing SQL select result set directly onto HTML webpage?

How can I rewrite my code below to print the results directly on my webpage instead of on the console? 如何重写下面的代码以直接在网页上而不是在控制台上打印结果?

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

One method is to obtain a writer from the servlet response and then write the HTML content you want. 一种方法是从Servlet响应中获取编写器,然后编写所需的HTML内容。 I made a slight refactor of your doSQL() method to accept a PrintWriter as a parameter. 我对您的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());
    }
}

You need to return resultset. 您需要返回结果集。

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
    }

in your servlet, get the doSQL(); 在您的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>");

This is not a full example and not fully test yet. 这不是一个完整的示例,还没有完全测试。 This reference will get you the idea how to do it. 此参考将使您了解如何执行此操作。 Btw, dont use static for DB operation. 顺便说一句,不要对数据库操作使用静态。 You can use for learning purpose. 您可以用于学习目的。

Below is example with reference to your code of servlet response as html for html request with GET method, similarly you can write for POST method. 下面是示例,该示例将您的Servlet响应代码引用为html,用于使用GET方法的html请求,类似地,您可以编写POST方法。 Add below code in servlet. 在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();

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM