How to use JSTL sql tag

后端 未结 3 547
逝去的感伤
逝去的感伤 2021-01-20 02:32
<%@taglib prefix=\"c\" uri=\"http://java.sun.com/jsp/jstl/core\" %>
<%@taglib prefix=\"sql\" uri=\"http://java.sun.com/jstl/sql\" %>



        
相关标签:
3条回答
  • 2021-01-20 02:40

    SELECT * FROM provider;
    

    Replace your code and import sql package

    or import sql package and Remove ; from your select statement

    0 讨论(0)
  • 2021-01-20 02:46

    My advice would be to forget about the <sql> tags completely, and to make all your database operations in plain Java (in a servlet or action of your preferred MVC framework). This servlet would build a list of bean instances, ready to be displayed by your JSP. Use RequestDispatcher to dispatch the request to the JSP from the servlet.

    Even the official Java EE tutorial says:

    The JSTL SQL tags for accessing databases listed in Table 7-7 are designed for quick prototyping and simple applications. For production applications, database operations are normally encapsulated in JavaBeans components.

    0 讨论(0)
  • 2021-01-20 02:46
     <%@ 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>
    
    0 讨论(0)
提交回复
热议问题