jstlsql:JSTL—SQL 语句小结来源: 发布时间:星期日, 2008年10月19日 浏览:300次 评论:0
JSTL—SQL 语句小结
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %> <%@ page c language="java" %> <html><head><title>JSTL的使用</title></head> <body> 创建普通的数据源 <sql:setDataSource var="example1" driver="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev" user="bn" password="bn" /> 创建普通的数据源,把用户名和密码写在url中 <sql:setDataSource var="example2" driver="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev;user=bn;password=bn" /> 从jndi名称空间中获得一个数据源 <sql:setDataSource var="example3" dataSource="jdbc/bn" /> 使用第一个数据源 <sql:query var="query1" dataSource="${example1}"> SELECT * FROM contact </sql:query> <table border="1"> <c:forEach var="row" items="${query1.rows}"> <tr> <td>Name: <c:out value="${row.userName}"/></td> <td>Value: <c:out value="${row.mobile}"/></td> </tr> </c:forEach> </table> 使用第二个数据源 <sql:query var="query2" dataSource="${example2}"> SELECT * FROM contact </sql:query> <table border="1"> <c:forEach var="row" items="${query2.rows}"> <tr> <td>Name: <c:out value="${row.userName}"/></td> <td>Value: <c:out value="${row.mobile}"/></td> </tr> </c:forEach> </table> 使用第三个数据源 <sql:query var="query3" dataSource="${example3}"> SELECT * FROM contact </sql:query> <table border="1"> <c:forEach var="row" items="${query3.rows}"> <tr> <td>Name: <c:out value="${row.userName}"/></td> <td>Value: <c:out value="${row.mobile}"/></td> </tr> </c:forEach> </table> </body> </html> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %> <%@ page c language="java" %> <html><head><title>JSTL:的使用</title></head> 第1种更新:更新记录值1 <sql:update var="update1" dataSource="${example}"> update contact set mobile='13688888' where userName='asiapower' </sql:update> 第2种更新:更新记录值2 <sql:update var="update2" sql="update contact set mobile=? where userName=?" dataSource="${example}"> <sql:param value="13999999"/> <sql:param value="hellking"/> </sql:update> 第3种更新:更新记录值3 <sql:update var="update3" dataSource="${example}"> update contact set mobile=? where userName=? <sql:param value="1399888"/> <sql:param value="chenzhanjun"/> </sql:update> 第4种更新:创建表 <sql:update var="update4" sql="create table test_temp(test varchar(20))" dataSource="${example}"/> 第5种更新:增加记录 <sql:update var="update5" sql="insert into test_temp values('hellking')" dataSource="${example}"/> 第6种更新:删除记录 <sql:update var="update6" sql="delete from test_temp where test='hellking'" dataSource="${example}"/> 第7种更新:删除表 <sql:update var="update7" sql="drop table test_temp" dataSource="${example}"/> </body> </html> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %> <%@ page c language="java" %> <html><head><title>JSTL:的使用</title></head> <body> 创建普通的数据源 <sql:setDataSource var="example" driver="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev" user="bn" password="bn" scope="session" /> 第一种查询 <sql:query var="query" dataSource="${example}"> SELECT * FROM contact </sql:query> <table border="1"> <c:forEach var="row" items="${query.rows}"> <tr> <td>Name: <c:out value="${row.userName}"/></td> <td>mobile: <c:out value="${row.mobile}"/></td> </tr> </c:forEach> </table> 第2种查询 <sql:query var="query2" sql="SELECT * FROM contact where userName=?" dataSource="${example}"> <sql:param value="asiapower"/> </sql:query> <table border="1"> <c:forEach var="row" items="${query2.rows}"> <tr> <td>Name: <c:out value="${row.userName}"/></td> <td>mobile: <c:out value="${row.mobile}"/></td> </tr> </c:forEach> </table> 第3种查询 <sql:query var="query3" dataSource="${example}"> SELECT * FROM contact where userName=? <sql:param value="hellking"/> </sql:query> <table border="1"> <c:forEach var="row" items="${query3.rows}"> <tr> <td>Name: <c:out value="${row.userName}"/></td> <td>mobile: <c:out value="${row.mobile}"/></td> </tr> </c:forEach> </table> </body> </html> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %> <%@ page c language="java" %> <html><head><title>JSTL:sql:param的使用</title></head> <sql:setDataSource var="example" dataSource="jdbc/bn" /> 执行数据添加操作 <sql:update var="update" dataSource="${example}"> insert into contact (userName,mobile,phone,mail)values(?,?,?,?) <sql:param>wyy</sql:param> <sql:param>13634234</sql:param> <sql:param>010213423434</sql:param> <sql:param>[email protected]</sql:param> </sql:update> 执行更新操作 <sql:update var="update2" sql="update contact set mobile=? where userName=?" dataSource="${example}"> <sql:param value="13999999"/> <sql:param value="hellking"/> </sql:update> </body> </html> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %> <%@ page c language="java" %> <html><head><title>JSTL:sql:transaction的使用</title></head> <sql:setDataSource var="example" dataSource="jdbc/bn" /> 使用事务处理方式创建一个表 <sql:transaction dataSource="${example}"> <sql:update var="newTable"> create table usertable ( nameid int primary key, name varchar(80) ) </sql:update> </sql:transaction> <p>DONE: 创建表完成 <hr> <h2>使用事务处理往表里插入数据:</h2> <sql:transaction dataSource="${example}"> <sql:update var="updateCount"> INSERT INTO usertable VALUES (1,'hellking') </sql:update> </sql:transaction> <p>DONE: 插入数据完成</p> <sql:transaction dataSource="${example}"> <sql:query var="query"> SELECT * FROM contact </sql:query> </sql:transaction> 查询数据记录:<hr> <table border="1"> <c:forEach var="row" items="${query.rows}"> <tr> <td>nameid: <c:out value="${row.nameid}"/></td> <td>name: <c:out value="${row.name}"/></td> </tr> </c:forEach> </table> <sql:update var="newTable" dataSource="${example}"> drop table usertable </sql:update> </body> </html> 0
相关文章
读者评论
发表评论 |