Marios Koni
Marios Koni

Reputation: 143

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. I was using servlet scriptlet but found out JSTL is a better way so I went for that. 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.

I tried to fetch data from the same database with standard java and it worked... But here it does not. 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!

P.S: This is just a small project not something professional.

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...

Upvotes: 0

Views: 1002

Answers (1)

Deadron
Deadron

Reputation: 5289

You have not imported the taglibs in your file

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.

You can remove the <%@page import tags as well

Upvotes: 1

Related Questions