如何使用JSTL sql标签

时间:2011-10-08 13:13:27

标签: java mysql jstl

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

<sql:setDataSource var="dataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/cloud" user="root"  password="root"
scope="session" /> 

<sql:query var="qryProvider" >
    SELECT * FROM `provider`;
</sql:query>

<table>
    <c:forEach var="row" items="${qryProvider.rows}">
        <tr>
            <td>${row.display_name}</td>

        </tr>
    </c:forEach>
</table>

我收到以下错误:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /testJSTL.jsp at line 8

5: url="jdbc:mysql://localhost:3306/cloud" user="root"  password="root"
6: scope="session" /> 
7:  
8: <sql:query var="qryProvider" >
9:     SELECT * FROM `provider`;
10: </sql:query>
11:  


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:567)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.lang.NullPointerException"
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
    org.apache.jsp.testJSTL_jsp._jspService(testJSTL_jsp.java:94)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

我试图关注this tut,但如果我写

,则会给我一个错误
<sql:query var="qryProvider" dataSource="${dataSource}" >

我是新手,有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:10)

我的建议是完全忘记<sql>标签,并使用普通Java(在首选MVC框架的servlet或操作中)进行所有数据库操作。这个servlet将构建一个bean实例列表,准备好由JSP显示。使用RequestDispatcher从servlet将请求分派给JSP。

即使official Java EE tutorial说:

  

用于访问表7-7中列出的数据库的JSTL SQL标记是   专为快速原型设计和简单应用而设计。用于生产   应用程序,数据库操作通常封装在   JavaBeans组件。

答案 1 :(得分:-2)

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


<sql:setDataSource var="dataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/cloud" user="root"  password="root"
scope="session" /> 


<sql:query var="qryProvider" dataSource="${dataSource}">

    SELECT * FROM provider;
</sql:query>

<table>
    <c:forEach var="row" items="${qryProvider.rows}">
        <tr>
            <td>${row.display_name}</td>

        </tr>
    </c:forEach>
</table>

答案 2 :(得分:-2)

SELECT * FROM provider;

替换您的代码并导入sql包

或 导入sql包和 去掉 ;来自您的选择陈述