1。查询数据库
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head></head>
<body>
<sql:setDataSource var="orads" driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:oracle" user="neo" password="123"/>
<sql:query var="query" dataSource="${orads}" sql="select * from student where age<? and CLASS_ID=?">
<sql:param value="23"/>
<sql:param value="402881eb213ce81701213ce81a220001"/>
</sql:query>
<table>
<c:forEach var="row" items="${query.rows}">
<tr>
<td>Name:</td><td><c:out value="${row.sname}"/></td>
<td>Age:</td><td><c:out value="${row.age}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
2.更新数据库
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head></head>
<body>
<sql:setDataSource var="orads" driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:oracle" user="neo" password="123"/>
<sql:update var="update1" sql="update student set sname=? where sid=?" dataSource="${orads}">
<sql:param value="aaa"/>
<sql:param value="402881eb213cf22701213cf229c20001"/>
</sql:update>
<sql:update var="update2" sql="insert into student values" dataSource="${orads}">
<sql:param value="aaa"/>
<sql:param value="402881eb213cf22701213cf229c20001"/>
</sql:update>
更新成功
</body>
</html>
3.事物
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head></head>
<body>
<sql:setDataSource var="orads" driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:oracle" user="neo" password="123"/>
<sql:transaction dataSource="${orads}">
<sql:update var="createtest" >
create table test(
tid number(4) primary key,
tname varchar2(20))
</sql:update>
<sql:update var="update2">
insert into test values(1,'张三')
</sql:update>
<sql:update var="update3">
insert into test values(2,'李四')
</sql:update>
<sql:update var="update4">
insert into test values(3,'王五')
</sql:update>
<sql:update var="update5">
update test set tname='AAA' where tid=1
</sql:update>
</sql:transaction>
事务完成
<sql:query var="query" dataSource="${orads}" sql="select * from test"/>
<table>
<c:forEach var="row" items="${query.rows}">
<tr>
<td>tid:</td><td><c:out value="${row.tid}"/></td>
<td>tname:</td><td><c:out value="${row.tname}"/></td>
</tr>
</c:forEach>
</body>
</html>
以上文件后缀都为.jsp 用tomcat运行
以上数据库都为oracle
216

被折叠的 条评论
为什么被折叠?



