JSTL_sql

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值