【问题标题】:how to show contents of a MySQL table in HTML using jsp如何使用jsp在HTML中显示MySQL表的内容
【发布时间】:2015-06-18 08:54:42
【问题描述】:

我是一个绝对的编程初学者,我有这个任务:在网页中显示来自 MySql 数据库的表的内容。我正在尝试使用从教程中获取的代码:

html代码:

<html>
    <head>
        <title>Spettacoli disponibili</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
    </head>
    <body>
        <h2>Lista dei teatri</h2>
        <form method="post" action="Teatri.jsp"></form>
    </body>
</html>

teatri.jsp

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<html>
<head>
<title>Lista dei teatri</title>
</head>
<body>

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/teatro"
     user="root"/>

<sql:query dataSource="${snapshot}" var="result">
SELECT * from teatri;
</sql:query>

<table border="1" width="100%">
<tr>
   <th>Teatro</th>
   <th>Indirizzo</th>
   <th>Citta</th>
   <th>Provincia</th>
   <th>Telefono</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
   <td><c:out value="${row.nome}"/></td>
   <td><c:out value="${row.indirizzo}"/></td>
   <td><c:out value="${row.citta}"/></td>
   <td><c:out value="${row.provincia}"/></td>
   <td><c:out value="${row.telefono}"/></td>
</tr>
</c:forEach>
</table>

</body>
</html>

但这对我不起作用,页面打开但表格未显示。 我错了吗?

【问题讨论】:

  • 日志有错误吗?

标签: html mysql jsp


【解决方案1】:

sql:setDataSource标签中缺少“password”属性,需要添加。

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/teatro"
     user="root"
     password="Your DB Password"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-01
    • 2023-03-11
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多