SQL標簽


SQL標簽庫提供了與關系型數據庫進行交互的標簽。

引入語法:<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

數據庫:test

用戶名:root

密碼:123

項目中加入驅動jar包:mysql-connector-java-5.1.26-bin.jar

表:create table Employees

 ( id int not null, age int not null, first varchar (255), last varchar (255),
   birth date//dateParam標簽后增加的一列
);

數據:insert into Employees values(100, 18, 'Zara', 'Ali');

    insert into Employees values(101, 25, 'Mahnaz', 'Fatma');

    insert into Employees values(102, 30, 'Zaid', 'Khan');

    insert into Employees values(103, 28, 'Sumit', 'Mittal');

標簽包括有:

標簽 描述
<sql:setDataSource> 指定數據源
<sql:query> 運行SQL查詢語句
<sql:update> 運行SQL更新語句
<sql:param> 將SQL語句中的參數設為指定值
<sql:dateParam> 將SQL語句中的日期參數設為指定的java.util.Date 對象值
<sql:transaction> 在共享數據庫連接中提供嵌套的數據庫行為元素,將所有語句以一個事務的形式來運行

<sql:setDataSource> 指定數據源,用來配置數據源或者將數據源信息存儲在某作用域的變量中,用來作為其他Jstl數據庫操作的數據源。

屬性:

屬性 描述 是否必要 默認值
driver 要注冊的JDBC驅動
url 數據庫連接的JDBC URL
user 數據庫用戶名
password 數據庫密碼
dataSource 事先准備好的數據庫
var 代表數據庫的變量 默認設置
scope var屬性的作用域 Page

 

eg:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <sql:setDataSource var="snapshot" 
     driver
="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/test" user="root" password="123" /> </body> </html>

 

 

 

<sql:query>  用來運行select語言,並將結果存儲在作用域變量中。

屬性:

屬性 描述 是否必要 默認值
sql 需要執行的SQL命令(返回一個ResultSet對象) Body
dataSource 所使用的數據庫連接(覆蓋默認值) 默認數據庫
maxRows 存儲在變量中的最大結果數 無窮大
startRow 開始記錄的結果的行數 0
var 代表數據庫的變量 默認設置
scope var屬性的作用域 Page

 

eg:

 

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" 
        url="jdbc:mysql://localhost/test"
        user="root"
        password="123" />
    <sql:query dataSource="${snapshot}" var="result">
        select * from Employees;
    </sql:query>    
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
        <tr>
            <td><c:out value="${row.id}" /></td>
            <td><c:out value="${row.first}" /></td>
            <td><c:out value="${row.last}" /></td>
            <td><c:out value="${row.age}" /></td>
        </tr>
        </c:forEach>
    </table>
</body>
</html>

//結果輸出為:

 

 

 

<sql:update> 用來執行一個沒有返回值的SQL語句,如insert,update,delete。

語法:

屬性 描述 是否必要 默認值
sql 需要執行的SQL命令(不返回ResultSet對象) Body
dataSource 所使用的數據庫連接(覆蓋默認值) 默認數據庫
var 用來存儲所影響行數的變量
scope var屬性的作用域 Page

 eg:

<%@ page language="java" contentType="text/html; charset=utf-8"   pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" 
        url="jdbc:mysql://localhost/test"
        user="root"
        password="123" />
    <sql:query dataSource="${snapshot}" var="result">
        select * from Employees;
    </sql:query>    
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
        <tr>
            <td><c:out value="${row.id}" /></td>
            <td><c:out value="${row.first}" /></td>
            <td><c:out value="${row.last}" /></td>
            <td><c:out value="${row.age}" /></td>
        </tr>
        </c:forEach>
    </table>
    <sql:update dataSource="${snapshot}" var="count">
        insert into Employees values(104,2,'Nuha','Ali');    
    </sql:update>
    <sql:query dataSource="${snapshot}" var="result">
        select * from Employees;
    </sql:query>
    <p>增加一條信息后:</p>
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
            <tr>
                <td><c:out value="${row.id}" /></td>
                <td><c:out value="${row.first}" /></td>
                <td><c:out value="${row.last}" /></td>
                <td><c:out value="${row.age}" /></td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

 

//結果輸出為:

 

<sql:param> 提供值占位符,與上面兩個標簽簽到使用。

語法:

屬性 描述 是否必要 默認值
value 需要設置的參數值 Body

eg:

<%@ page language="java" contentType="text/html; charset=utf-8"   pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" 
        url="jdbc:mysql://localhost/test"
        user="root"
        password="123" />
    <c:set var="id" value="104" />    
    <p>查詢id=104的人員信息:</p>
    <sql:query dataSource="${snapshot}" var="result">
        select * from Employees where id=?;
        <sql:param value="${id}" />
    </sql:query>    
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
        <tr>
            <td><c:out value="${row.id}" /></td>
            <td><c:out value="${row.first}" /></td>
            <td><c:out value="${row.last}" /></td>
            <td><c:out value="${row.age}" /></td>
        </tr>
        </c:forEach>
    </table>
    
</body>
</html>

 

//結果輸出為:

 

 

<sql:dateParam> 與<sql:param>用法一直只是提供的是日期和時間的占位符。

語法:

屬性 描述 是否必要 默認值
value 需要設置的日期參數(java.util.Date) Body
type DATE (只有日期),TIME(只有時間), TIMESTAMP (日期和時間) TIMESTAMP

eg:

<%@ page language="java" contentType="text/html; charset=utf-8"   pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" 
        url="jdbc:mysql://localhost/test"
        user="root"
        password="123" />
    
    <sql:query dataSource="${snapshot}" var="result">
        select * from Employees ;
    </sql:query>    
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
            <th>Birth</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
        <tr>
            <td><c:out value="${row.id}" /></td>
            <td><c:out value="${row.first}" /></td>
            <td><c:out value="${row.last}" /></td>
            <td><c:out value="${row.age}" /></td>
            <td><c:out value="${row.birth}" /></td>
        </tr>
        </c:forEach>
    </table>
    <c:set var="id_new" value="102" />
    <c:set var="birth_new" value="<%=new java.util.Date() %>" />
    <sql:update dataSource="${snapshot}" var="count">
        update Employees set birth=? where id=?;
        <sql:dateParam type="DATE" value="${birth_new}" />
        <sql:param value="${id_new}" />    
    </sql:update>
    <sql:query dataSource="${snapshot}" var="result">
        select * from Employees;
    </sql:query>
    <p>更改  id=102 的 birth:</p>
     <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
            <th>Birth</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
        <tr>
            <td><c:out value="${row.id}" /></td>
            <td><c:out value="${row.first}" /></td>
            <td><c:out value="${row.last}" /></td>
            <td><c:out value="${row.age}" /></td>
            <td><c:out value="${row.birth}" /></td>
        </tr>
        </c:forEach>
    </table>
</body>
</html>

 

//結果輸出為:

 

<sql:transaction> 事務處理,用來將<sql:query>和<sql:update>標簽封裝在事務中,使之成為單一事務,同時提交或回滾。

語法:

描述 是否必要 默認值
dataSource 所使用的數據庫(覆蓋默認值) 默認數據庫
isolation 事務隔離等級 (READ_COMMITTED,,READ_UNCOMMITTED, REPEATABLE_READ或 SERIALIZABLE) 數據庫默認

eg:

<%@ page language="java" contentType="text/html; charset=utf-8"   pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" 
        url="jdbc:mysql://localhost/test"
        user="root"
        password="123" />
    
    <sql:query dataSource="${snapshot}" var="result">
        select * from Employees ;
    </sql:query>    
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
            <th>Birth</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
        <tr>
            <td><c:out value="${row.id}" /></td>
            <td><c:out value="${row.first}" /></td>
            <td><c:out value="${row.last}" /></td>
            <td><c:out value="${row.age}" /></td>
            <td><c:out value="${row.birth}" /></td>
        </tr>
        </c:forEach>
    </table>
    <c:set var="id_new" value="102" />
    <c:set var="birth_new" value="<%=new java.util.Date() %>" />
    
    <sql:transaction dataSource="${snapshot}">
        <sql:update var="count">
            update Employees set birth=? where id=?;
            <sql:dateParam type="DATE" value="${birth_new}" />
            <sql:param value="${id_new}" />    
        </sql:update>
        <sql:update var="count">
             update Employees set last='Ali' where id=102;
        </sql:update>
        
        <sql:update var="count">
             insert into Employees values(104,2,'Nuha','Ali','2014/2/3'); 
        </sql:update>
    </sql:transaction>
    
    
    <sql:query dataSource="${snapshot}" var="result">
        select * from Employees;
    </sql:query>
    <p>一次性事務執行后:</p>
     <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
            <th>Birth</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
        <tr>
            <td><c:out value="${row.id}" /></td>
            <td><c:out value="${row.first}" /></td>
            <td><c:out value="${row.last}" /></td>
            <td><c:out value="${row.age}" /></td>
            <td><c:out value="${row.birth}" /></td>
        </tr>
        </c:forEach>
    </table>
</body>
</html>

 

//結果輸出為:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM