JSTL SQL

JSTL全稱爲JSP Standard Tag Library,即jsp標準標籤庫。標籤是jsp作爲java EE 分層程序中顯示的一種解決方法。jsp標籤與HTML代碼相似,使用標籤要顯得整潔,可讀性好,可以重複使用。所以推薦使用標籤。
JSTL可以直接操作數據庫,sql標籤庫操作數據庫要比jsp使用Scriptlet操作數據庫要簡單。

JSTL標籤庫的使用
1. 引入相關的jar、tld文件
下載路徑:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
然後將相應的jar包(jstl.jar,standard.jar)添加到/WEB-INF/lib下面和將tld文件添加到/WEB-INF下即可,如圖:
這裏寫圖片描述
2. 添加sql的驅動包,根據你所使用的數據庫,添加相應的驅動包,我使用的是sql server,如圖
這裏寫圖片描述
3. 引入相應的標籤庫:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
  1. 設置數據源(根據實際情況填寫相關的driver、user、password、url)
<sql:setDataSource driver="net.sourceforge.jtds.jdbc.Driver" user="sa"
                    password="123456" url="jdbc:jtds:sqlserver://localhost:1433/test" var="dataSource" scope="page"/>

附件:一個操作數據庫的例子

<%--
  Created by IntelliJ IDEA.
  User: AA
  Date: 2017/4/14
  Time: 14:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<html>
<head>
    <title>Title</title>
    <style type="text/css">
        table {
            border-collapse: collapse;
            border: 1px solid #000000;
        }

        td {
            border: 1px solid #000000;
            padding: 2px;
        }

        .title td {
            text-align: center;
            background: #DDDDDD;
        }
    </style>
</head>
<body>
<sql:setDataSource driver="net.sourceforge.jtds.jdbc.Driver" user="sa"
                    password="123456" url="jdbc:jtds:sqlserver://localhost:1433/test" var="dataSource" scope="page"/>

<%--<sql:setDataSource dataSource="jdbc/test" var="serverDataSource" />--%>
<%--${dataSource}--%>

<sql:update var="result" dataSource="${ dataSource }" sql="update person set age=12 ">
</sql:update>
update, 影響到的數據條數:${ result } <br/>

<%--<sql:update var="result" dataSource="${ dataSource }">--%>
    <%--create table tb_corporation (--%>
    <%--id int not null unique check(id>0),--%>
    <%--name varchar(255),--%>
    <%--description text,--%>
    <%--primary key(id)--%>
    <%--)--%>
<%--</sql:update>--%>

<%--CREATE TABLE, 影響到的數據條數:${ result } <br/>--%>

<%--<sql:update var="result" dataSource="${ dataSource }">--%>
    <%--insert into tb_corporation (id,name, description ) values (2,'MicroSoft', '微軟')--%>
<%--</sql:update>--%>

<%--INSERT, 影響到的數據條數:${ result } <br/>--%>

<%--<sql:update var="result" dataSource="${ dataSource }">--%>
    <%--insert into tb_corporation ( name, description ) values ('IBM', '國際商用機器')--%>
<%--</sql:update>--%>

<%--INSERT, 影響到的數據條數:${ result } <br/>--%>
<jsp:useBean id="date" class="java.util.Date"></jsp:useBean>
<sql:query var="rs" dataSource="${dataSource}" scope="page" sql="select * from person where name = ? ">
    <sql:param value="lee"/>
</sql:query>

<table>
    <tr class="title">
        <%--<td>ID</td>--%>
        <td>Name</td>
        <td>Age</td>
    </tr>

    <c:forEach var="row" items="${ rs.rows }">
        <tr>
            <%--<td>${ row['id'] }</td>--%>
            <td>${ row['name'] }</td>
            <td>${ row['age'] }</td>
        </tr>
    </c:forEach>

</table>
</body>
</html>
發佈了94 篇原創文章 · 獲贊 18 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章