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>

//结果输出为:

最新文章

  1. Windows转到linux中,文件乱码,文件编码转换 &amp; 解决sqlplus连接oracle乱码
  2. MicroERP主要业务流程示意图
  3. Asp.Net正在中止线程引发的问题
  4. APMServ 配置记录
  5. loj 1009(dfs)
  6. JAVA TCP/IP Socket通信机制以及应用
  7. PreTranslateMessage和TranslateMessage区别
  8. 实验数据结构——KMP算法Test.ming
  9. 需求收集实例三之 FM
  10. VUE请求本地数据的配置json-server
  11. @RequestBody和@RequestParam区别
  12. X级联动
  13. 用IntelliJ IDEA搭建第一个SpringBoot例子
  14. python之匿名函数lambda
  15. cad2020卸载/安装失败/如何彻底卸载清除干净cad2020注册表和文件的方法
  16. 【python】关于python中模块导入的总结
  17. 【图像处理】基于OpenCV底层实现的直方图匹配
  18. quartz---触发job时间和结束时间
  19. poj 3984 -- 迷宫问题 深搜
  20. Oracle备份提示,EXP-00091: 正在导出有问题的统计信息。

热门文章

  1. Maven构建简单的多模块项目
  2. R中创建not-yet-evaluated对象
  3. Eclipse上的项目分享到GitHub
  4. JAVA中JDBC连接数据库
  5. HDU3177 贪心
  6. 【HDU 4747 Mex】线段数
  7. Greenplum各种Tips(不定时更新)
  8. Android SDK 镜像站
  9. MySQL进阶
  10. A trip through the Graphics Pipeline 2011_11 Stream Out