SQL標籤

SQL標籤庫提供了與關係型數據庫進行交互的標籤。html

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

數據庫:testmysql

用戶名:rootsql

密碼:123數據庫

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

表:create table Employeesui

 ( 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');url

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

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

    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>

 

//結果輸出爲:

相關文章
相關標籤/搜索