关于JSTL的标签信息可以查看《JSP JSTL标签》
直接上代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%
pageContext.setAttribute("testId", "123");
%>
<!-- 设置数据源 -->
<sql:setDataSource var="dataSource"
driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:test" user="test"
password="test" />
<html>
<head>
<title>index</title>
</head>
<body>
<p>查询数据</p>
<sql:query var="result" dataSource="${dataSource}">
select id,name from table1
</sql:query>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th width="200">ID</th>
<th width="200">Name</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.id}" /></td>
<td><c:out value="${row.name}" /></td>
</tr>
</c:forEach>
</table>
<hr />
<p>插入数据</p>
<sql:update var="result" dataSource="${dataSource}">
insert into table1(id,name) values(?, 'testName')
<sql:param value="${testId}" />
</sql:update>
<p>影像记录条数:${result}</p>
<sql:query var="result" dataSource="${dataSource}">
select id,name from table1
</sql:query>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th width="200">ID</th>
<th width="200">Name</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.id}" /></td>
<td><c:out value="${row.name}" /></td>
</tr>
</c:forEach>
</table>
<hr />
<p>修改数据</p>
<sql:update var="result" dataSource="${dataSource}">
update table1 set name = 'Name' where id = ?
<sql:param value="${testId}" />
</sql:update>
<p>影像记录条数:${result}</p>
<sql:query var="result" dataSource="${dataSource}">
select id,name from table1
</sql:query>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th width="200">ID</th>
<th width="200">Name</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.id}" /></td>
<td><c:out value="${row.name}" /></td>
</tr>
</c:forEach>
</table>
<hr />
<p>删除数据</p>
<sql:update var="result" dataSource="${dataSource}">
delete from table1 where id = ?
<sql:param value="${testId}" />
</sql:update>
<p>影像记录条数:${result}</p>
<sql:query var="result" dataSource="${dataSource}">
select id,name from table1
</sql:query>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th width="200">ID</th>
<th width="200">Name</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.id}" /></td>
<td><c:out value="${row.name}" /></td>
</tr>
</c:forEach>
</table>
</body>
</html>
运行结果