Do the query in a servlet not a JSP. For example, one way would be to put the results of the query in a request attribute and forward from the servlet to a JSP.
In servlet:
request.setAttribute("queryresults", queryresults);
request.getRequestDispatcher("somejsp.jsp").forward(request, response);
JSP:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
<c:forEach items="${queryresults}" var="row">
<tr>
<td>${row.field1}</td>
<td>${row.field2}</td>
<td>${row.field3}</td>
</tr>
</c:forEach>
</table>
See also How to avoid Java code in JSP files?