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>
//结果输出为:
SQL标签的更多相关文章
- java web(一) 使用sql标签库+tomcat+mysql手动创建一个jsp练习总结
2016-09-0111:06:53 使用sql标签库+tomcat+mysql手动创建一个jsp 1. 1.1安装tomcat ...
- 转: JSTL SQL标签库 使用
SQL标签库 JSTL提供了与数据库相关操作的标签,可以直接从页面上实现数据库操作的功能,在开发小型网站是可以很方便的实现数据的读取和操作.本章将详细介绍这些标签的功能和使用方法. SQL标签库从功能 ...
- 夺命雷公狗---DEDECMS----18dedecms之无可奈何标签-sql标签取出今天更新
我们在一些开发时候遇到普通标签都解决不了的问题的时候可以尝试下我们dedecms自带的sql标签,几乎可以完成任何的查询需求 语法如下所示: 我们在这里将刚才首页今天更新那块给改写下,原先的是: {d ...
- 9.mybatis动态SQL标签的用法
mybatis动态SQL标签的用法 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...
- JSP标准标签库(JSTL)--SQL标签库 sql
了解即可.SQL标签库 No. 功能分类 标签名称 描述 1 数据源标签 <sql:setDataSource> 设置要使用的数据源名称 2 数据库操作标签 <sql:query&g ...
- JSTL SQL标签库 使用
推荐博客:http://blog.sina.com.cn/s/blog_4f925fc30101820u.html 怕博主把原文删了,所以在这里先保存一下. SQL标签库 JSTL提供了与数据库相关操 ...
- 织梦SQL标签的使用
(>=DedeCMS 3,DedeCMS 4,DedeCMS 5) 名称:sql 功能:用于获取MySQL数据库内容的标签 语法: 1 2 3 {dede:sql sql='' appname= ...
- 小峰servlet/jsp(7)jstl国际化标签库、sql标签库等
一.jstl国际化标签库: fmt:setLocale 设定用户所在的区域: fmt:formatDate 对日期进行格式化 fmt:requestEncoding 设置所有的请求编码; fmt: ...
- mybatis动态sql中的sql标签——抽取可重用的sql片段
1.用<sql>标签抽取可重用的sql片段 <!-- 抽取可重用的SQL片段,方便后面引用 1.sql抽取,经常将要查询的列名,或者插入用的列名,之后方便引用 ...
随机推荐
- Java Web include指令和动作的区别
- Odoo 采购单 加盖 电子公章
简单说一下思路,第一步呢是要有一个电子公章的图片,第二步就是把这张图片添加到生成的PDF文件中. 最后的效果图如下:
- Js练笔——用循环和递归实现追踪对象深度(循环引用关系不考虑)
function reobs(obj){ //返回对象中对象属性组成的数组 var a=[]; var b=[]; for(it in obj){ a.push(it); } for(var i=0; ...
- "我爱记单词"测试报告兼功能展示
"我爱记单词"测试报告兼功能展示 前言: 我们大部分的测试都是一边开发一边完成的,这里给出软件开发基本完成后在使用时的一些测试例子. 一.背景介绍 我们的数据库中一共有10个表: ...
- wampserver的php.ini文件
在修改php.ini文件时,找到了php文件夹下的php.ini文件,但是重启所有服务后就是不起作用.查看前辈的博客后,明白了是在apache目录下的php.ini才是起作用的. .
- 测试常用SQL注入语句大全
转载自Cracer,标题:<渗透常用SQL注入语句大全>,链接http://www.xxxx.com/?p=2226 1.判断有无注入点 整形参数判断 1.直接加' 2.and 1=1 3 ...
- linux卸载mysql,apache,php
卸载Mysql 1.查找以前是否装有mysql 命令:rpm -qa|grep -i mysql 可以看到mysql的包: mysql-3.23.58-9php-mysql-4.3.4-11mod_a ...
- php课程---练习(发布新闻)
做一个发布新闻的页面,实现发布新闻,查看新闻,修改新闻与删除等功能 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...
- nginx服务器状态监控
Nginx开启监控需在编译时加入with-http_stub_status_module,查看当前Nginx编译参数:/usr/local/nginx/sbin/nginx -V 1.以二级目录方式开 ...
- AspNet Mvc 路由解析中添加.html 等后缀 出现404错误的解决办法
使用Mvc 有时候我们希望,浏览地址以.html .htm 等后缀名进行结尾. 于是我们就在RouteConfig 中修改路由配置信息,修改后的代码如下 routes.IgnoreRoute(&quo ...