Reputation: 25
${employeeId} is showing properly if I put it in html tags (paragraph, anchor etc.) but is not working when I'm passing it through a sql prepared statement to select employee from database like: prepStatement.setInt(1, ${employeeId}). if I pass 1, 2, 3 etc. instead of ${employeeId} it works and show the related results this means my code is almost correct but it's not working with ${employeeId}, I tried adding single and double quotes around ${employeeId} but nothing works.
EDITED:
JSP file
<%
Connection connection = DriverManager.getConnection("jdbc:h2:file:./MANAGER", "test", "password");
Statement statement = connection.createStatement();
String queryString = "SELECT * FROM OWNER WHERE EMPLOYEEID = ? ORDER BY ID DESC LIMIT 1";
PreparedStatement pstatement = connection.prepareStatement(queryString);
pstatement.setInt(1, ${employeeId});
ResultSet resultset = pstatement.executeQuery();
if(!resultset.next()) {
out.println("Sorry, could not find that publisher. ");
} else {
%>
<%= resultset.getString("employeeId") %>
<%
}
%>
Upvotes: 0
Views: 158
Reputation: 842
use <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> and try
Upvotes: 0
Reputation: 79550
The parameter ${employeeId}
is syntactically wrong because there is no operator such as $
in Java. Moreover, PreparedStatement
does not know how to treat a JSTL tag.
If employeeId
is an attribute present in the request object (e.g. set into the controller as request.setAttribute("employeeId", 123)
), replace
pstatement.setInt(1, ${employeeId});
with
pstatement.setInt(1, Integer.parseInt((String)request.getAttribute("employeeId")));
If employeeId
is a variable declared and populated in the page itself (e.g. <c:set var="employeeId" value="123"/>
), replace
pstatement.setInt(1, ${employeeId});
with
pstatement.setInt(1, Integer.parseInt((String)pageContext.getAttribute("employeeId")));
Upvotes: 1