非常教程

JSP 教程教程

<sql:query> 标签

<sql:query> 标签

<sql:query> 标签

<sql:query> 标签

JSP 标准标签库

<sql:query>标签用来运行SQL SELECT语句,还有就是将结果存储在作用域变量中。

语法格式

<sql:query
  var="<string>"
  scope="<string>"
  sql="<string>"
  dataSource="<string>"
  startRow="<string>"
  maxRows="<string>"/>

属性

<sql:query>标签有如下属性:

属性 描述 是否必要 默认值
sql 需要执行的SQL命令(返回一个ResultSet对象) Body
dataSource 所使用的数据库连接(覆盖默认值) 默认数据库
maxRows 存储在变量中的最大结果数 无穷大
startRow 开始记录的结果的行数 0
var 代表数据库的变量 默认设置
scope var属性的作用域 Page


程序示例

首先,需要在TEST数据库中建一个Employees表,然后往表中添加几条记录,具体操作步骤如下:

步骤1:

打开CMD,将目录转至安装目录下:

C:\>
C:\>cd "Program Files\MySQL\bin"
C:\Program Files\MySQL\bin>

步骤2:

登陆数据库:

C:\Program Files\MySQL\bin>mysql -u root -p
Enter password: ********
mysql>

步骤3:

在TEST数据库中建立Employees表:

mysql> use TEST;
mysql> create table Employees
    (
     id int not null,
     age int not null,
     first varchar (255),
     last varchar (255)
    );
Query OK, 0 rows affected (0.08 sec)
mysql>

创建数据记录:

最后,在Employees表中创建几条记录:

mysql> INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali');
Query OK, 1 row affected (0.05 sec)
 
mysql> INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');
Query OK, 1 row affected (0.00 sec)
 
mysql>

现在,编写JSP文件,使用<sql:query>标签来执行SQL SELECT语句:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<html>
<head>
<title>JSTL sql:query Tag</title>
</head>
<body>
 
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/TEST"
     user="root"  password="pass123"/>

<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:query> 标签


<sql:query> 标签

JSP 标准标签库

<sql:query> 标签
JSP 教程

JSP,即 Java Server Pages,是由 Sun Microsystems 公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户端请求,而动态生成 HTML、XML 或其他格式文档的 Web 网页的技术标准。

JSP 教程目录

1.JSP 简介
2.JSP 教程
3.JSP 隐式对象
4.JSP 指令
5.JSP 发送邮件
6.JSP 自动刷新
7.JSP 页面重定向
8.JSP 日期处理
9.JSP 国际化
10.JSP 调试
11.JSP 自定义标签
12.<c:forEach>, <c:forTokens> 标签
13.<c:import> 标签
14.<c:choose>, <c:when>, <c:otherwise> 标签
15.<c:if> 标签
16.<c:catch> 标签
17.<c:remove> 标签
18.<c:set> 标签
19.<c:out> 标签
20.<fmt:setBundle> 标签
21.<fmt:setLocale> 标签
22.<fmt:bundle> 标签
23.<fmt:parseDate> 标签
24.<fmt:formatDate> 标签
25.<fmt:parseNumber> 标签
26.<fmt:formatNumber> 标签
27.<c:redirect> 标签
28.<c:url> 标签
29.<c:param> 标签
30.<sql:transaction> 标签
31.<sql:dateParam> 标签
32.<sql:param> 标签
33.<sql:update> 标签
34.<sql:query> 标签
35.<sql:setDataSource> 标签
36.<fmt:requestEncoding> 标签
37.<fmt:message> 标签
38.<fmt:setTimeZone> 标签
39.<fmt:timeZone> 标签
40.JSTL fn:containsIgnoreCase()函数
41.JSTL fn:contains()函数
42.<x:param> 标签
43.<x:transform> 标签
44.<x:choose>, <x:when>, <x:otherwise> 标签
45.<x:forEach> 标签
46.<x:if> 标签
47.<x:set> 标签
48.<x:parse> 标签
49.<x:out> 标签
50.JSTL fn:substringAfter()函数
51.JSTL fn:substring()函数
52.JSTL fn:startsWith()函数
53.JSTL fn:split()函数
54.JSTL fn:replace()函数
55.JSTL fn:length()函数
56.JSTL fn:join()函数
57.JSTL fn:indexOf()函数
58.JSTL fn:escapeXml()函数
59.JSTL fn:endsWith()函数
60.JSP 标准标签库(JSTL)
61.JSTL fn:trim()函数
62.JSTL fn:toUpperCase()函数
63.JSTL fn:toLowerCase()函数
64.JSTL fn:substringBefore()函数