【问题标题】:How to display data in the HTML page from the Database using Servlet? [duplicate]如何使用 Servlet 在数据库中的 HTML 页面中显示数据? [复制]
【发布时间】:2018-01-15 13:15:11
【问题描述】:

如何使用 Servlet 在 HTML 页面中显示来自数据库的数据?

【问题讨论】:

  • 只要谷歌一次,你就会得到大量的例子来使用和修改。

标签: html servlets


【解决方案1】:

嗯,这是一个非常愚蠢的问题,但既然你问了,所以我在这里写答案。

您可以使用 JDBC 从数据库中获取数据,然后使用响应打印器显示它。

示例程序

Connection con;
PrintWriter out = response.getWriter();
response.setContentType("text/html");
con = DriverManager.getConnection("jdbc:mysql://localhost:" + "3306" + "/" + "pinnnacledb1", "root", "");
String query = "Select * from table where something = ?";
PreparedStatement pst = currentCon.prepareStatement(query);
pst.setString(1, username);
ResultSet rs = pst.executeQuery();
while (rs.next())
{
    //Your logic for display
    out.println(<html>Whole html logic can be written here</html>);

}

现在运行 servlet,结果将显示在浏览器上。

【讨论】:

    猜你喜欢
    • 2016-08-06
    • 2017-07-29
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2020-12-18
    相关资源
    最近更新 更多