user1162494
user1162494

Reputation: 43

calling information from a database

Can anyone help me, i am trying to bring up a list of users from an SQL table into a dropdown list on html. And when a name is selected from this list, i want it to bring up the details in a table called notices. I am unsure where the problem is with my code.

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

<%-- 
    Document   : history
    Created on : 12-Mar-2012, 03:49:35
    Author     : 
--%>

<sql:query var="user" dataSource="jdbc/noticeboard">
    SELECT username FROM registeredusers
</sql:query>



          <sql:query var="noticesQuery" dataSource="jdbc/noticeboard">
           SELECT N_id,postedon,messages,ack FROM notices order by N_id desc limit 1; 
        </sql:query>
       <c:set var="noticesDetails" value="${noticesQuery.rows[0]}"/>

   <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title> Notice Board</title>
    </head>
    <body>


        <form action="one.jsp">
    <strong>Select a username:</strong>
    <select name="username">
        <c:forEach var="row" items="${user.rowsByIndex}">
            <c:forEach var="column" items="${row}">
                <option value="<c:out value="${column}"/>"><c:out value="${column}"/></option>
            </c:forEach>
        </c:forEach>
    </select>
    <input type="submit" value="submit" name="submit" />
</form>







    </body>
</html>





    <%-- 
    Document   : one
    Created on : 26-Mar-2012, 07:35:21
    Author     : 
--%>
<sql:query var="userQuery" dataSource="jdbc/noticeboard">
    SELECT messages FROM notices WHERE user = ? <sql:param value="${param.user}"/>
</sql:query>
   <c:set var="messageDetails" value="${userQuery.rows[0]}"/>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>


        <table border="1">

                <tr>
                    <td>ID</td>
                    <td>USERNAME</td>
                    <td>MESSAGE</td>
                    <td>DATE</td>
                    <td>ACK</td>
                </tr>
                <tr>
                    <td>${messageDetails.N_id}</td>
                    <td>${messageDetails.user}</td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>

        </table>

    </body>
</html>

Upvotes: 1

Views: 1844

Answers (1)

tusar
tusar

Reputation: 3424

Your parameter name is not correct

You are passing the username parameter to one.jsp. to use it, use ${param.username} or ${param[username] or <%=request.getParameter("username")%>. So your query will look like:

<sql:query var="userQuery" dataSource="jdbc/noticeboard">
    SELECT messages FROM notices WHERE user = ? 
    <sql:param value="<%=request.getParameter("username")%>"/>
</sql:query>

Also, I dont know whether it is a copy-paste mistake, you didnot add the taglib URIs in one.jsp

Upvotes: 1

Related Questions