简体   繁体   English

无法使用JSP从MySQL数据库获取数据

[英]Cannot fetch data from mySQL database with JSP

I'm trying to retrieve data from my database with JSP and project them to an html page. 我正在尝试使用JSP从我的数据库检索数据并将它们投影到html页面。 I was using servlet scriptlet but found out JSTL is a better way so I went for that. 我使用的是servlet scriptlet,但发现JSTL是更好的方法,所以我去了。 The problem is that when compiled the page does return an error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. 问题在于,在编译时页面确实返回了错误: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

I tried to fetch data from the same database with standard java and it worked... But here it does not. 我试图从使用标准Java的同一数据库中获取数据,但它确实起作用了……但是在这里却没有。 My source code for the specific page is: 我针对特定页面的源代码是:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.sql.*" %>
<%@page import="java.lang.String" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Main Menu</title>
</head>

<style>
    body {
        background-color: lightblue;
    }
</style>

<sql:setDataSource var = "snapshot" driver = "com.mysql.jdbc.Driver"
                   url = "jdbc:mysql://127.0.0.1:3306/CARDATA"
                   user = "toomlg4u"/>

<sql:query dataSource = "${snapshot}">USE CARDATA;</sql:query>

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

<table border = "1" width = "100%">
    <tr>
        <th>ID</th>
        <th>Full Name</th>
        <th>Password</th>
        <th>Email</th>
    </tr>

<c:forEach var = "row" items = "${result.rows}">
    <tr>
        <td> <c:out value = "${row.userId}"/></td>
        <td> <c:out value = "${row.realName}"/></td>
        <td> <c:out value = "${row.userName}"/></td>
        <td> <c:out value = "${row.passWord}"/></td>
        <td> <c:out value = "${row.email}"/></td>
    </tr>
</c:forEach>
</table>

</body>
</html>

Every bit of help is appreciated! 感谢您的帮助!

PS: This is just a small project not something professional. PS:这只是一个小项目,并不专业。

EDIT 编辑

I copied the jconnector over to the lib folder of $CATALINA_HOME, so now it shows the table but without values... and strangely my two queries above... 我将jconnector复制到$ CATALINA_HOME的lib文件夹中,因此现在它显示表但没有值...奇怪的是,上面的两个查询...

You have not imported the taglibs in your file 您尚未将taglibs导入文件中

Add

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> 

to the top of your page. 到页面顶部。

Also your table has 5 columns but only 4 header columns. 您的表也有5列,但只有4个标题列。

You can remove the <%@page import tags as well 您也可以删除<%@ page导入标签

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

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