【发布时间】:2018-01-15 13:15:11
【问题描述】:
如何使用 Servlet 在 HTML 页面中显示来自数据库的数据?
【问题讨论】:
-
只要谷歌一次,你就会得到大量的例子来使用和修改。
如何使用 Servlet 在 HTML 页面中显示来自数据库的数据?
【问题讨论】:
嗯,这是一个非常愚蠢的问题,但既然你问了,所以我在这里写答案。
您可以使用 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,结果将显示在浏览器上。
【讨论】: