jstl--sql 語句小結

<%@ 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>
  <title>JSTL:的使用</title>
</head>
<body bgcolor="#FFFFFF">
創建普通的數據源:<br>
<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中:<br>
<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名稱空間中獲得一個數據源。<br>
<sql:setDataSource
  var="example3"
  dataSource="jdbc/bn"
/>
<hr>
使用第一個數據源:<hr>
<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>
使用第二個數據源:<hr>
<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>

使用第三個數據源:<hr>
<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 contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL:的使用</title>
</head>

第1種更新:更新記錄值1<hr>
<sql:update var="update1" dataSource="${example}">
    update contact set mobile='13688888' where userName='asiapower'
</sql:update>

<hr>
第2種更新:更新記錄值2<hr>
<sql:update var="update2" sql="update contact set mobile=? where userName=?" dataSource="${example}">
   <sql:param value="13999999"/>
   <sql:param value="hellking"/>
</sql:update>

<hr>
第3種更新:更新記錄值3<hr>
<sql:update var="update3" dataSource="${example}">
    update contact set mobile=? where userName=?
     <sql:param value="1399888"/>
     <sql:param value="chenzhanjun"/>
</sql:update>

第4種更新:創建表<hr>
<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種更新:刪除記錄<hr>
<sql:update var="update6" sql="delete from test_temp where test='hellking'" dataSource="${example}"/>  
第7種更新:刪除表<hr>
<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 contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL:的使用</title>
</head>
<body bgcolor="#FFFFFF">
創建普通的數據源:<br>
<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"
/>
第一種查詢:<hr>
<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>
<hr>
第2種查詢:<hr>
<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>
<hr>
第3種查詢:<hr>
<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 contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL:sql:param的使用</title>
</head>
<sql:setDataSource
  var="example"
  dataSource="jdbc/bn"
/>
執行數據添加操作:<hr>
<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>
執行更新操作:<hr>
<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 contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL:sql:transaction的使用</title>
</head>
<sql:setDataSource
  var="example"
  dataSource="jdbc/bn"
/>
<h2>使用事務處理方式創建一個表:</h2>

<sql:transaction dataSource="${example}">
  <sql:update var="newTable">
    create table usertable (
      nameid int primary key,
      name varchar(80)
    )
  </sql:update>
</sql:transaction>

<p>DONE: 創建表完成</p>

<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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章