欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSP中操作數(shù)據(jù)庫的常用SQL標(biāo)簽用法總結(jié)

 更新時間:2016年04月14日 15:29:40   作者:張大鵬  
這篇文章主要介紹了JSP中操作數(shù)據(jù)庫的常用SQL標(biāo)簽用法總結(jié),SQL標(biāo)簽封裝了數(shù)據(jù)庫訪問的通用邏輯,可以簡化數(shù)據(jù)庫操作,需要的朋友可以參考下

<sql:setDataSource>
標(biāo)簽設(shè)定數(shù)據(jù)源

語法結(jié)構(gòu):

復(fù)制代碼 代碼如下:

    <sql:setDataSource url="jdbcUrl" driver="driverClassName" user="userName" password="password" [var = "varName"][scope="{page | request | session | application}"] />


<sql:update>
標(biāo)簽進(jìn)行增刪改的操作

第一種格式:

復(fù)制代碼 代碼如下:

<sql:update sql="sqlUpdate" [var="varName"] [scope="{page|request|session|application}"][dataSource="dataSource"] />

eg:

<%@ page contentType="text/html;charset=GBK"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>測試標(biāo)簽</title>
</head>
<sql:setDataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"
 user="liky" password="redhat"
 url="jdbc:sqlserver://localhost:1433;DatabaseName=csu" var="db" />
<body>
<!-- 這里使用update標(biāo)簽創(chuàng)建一個表 -->
<sql:transaction dataSource="${db}">
<sql:update var="update" scope="page">
 if exists(select 1 from sysobjects where name='tmp')
 drop table tmp
 
 create table tmp
 (
 id int identity(1,1) primary key,
 name varchar(20),
 pass varchar(20)
 )
</sql:update>
</sql:transaction>
<!-- 這里使用update標(biāo)簽修改表的結(jié)構(gòu),添加一個列 -->
<sql:transaction dataSource="${db}">
<sql:update var="update" scope="page">
 alter table tmp add age tinyint
</sql:update>
</sql:transaction>
<br>
</body>
</html>

第二種格式是將SQL語句作為本體內(nèi)容

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<!DOCTYPE html>
<html>
  <head>
    <title>Update標(biāo)簽添加數(shù)據(jù)</title>
  </head>
  <body>
    <%-- 指定數(shù)據(jù)庫鏈接URL,JDBC驅(qū)動,用戶名和密碼 --%>
    <sql:setDataSource url="jdbc:mysql://localhost:3306/javaweb" driver="com.mysql.jdbc.Driver"
        user="root" password="zhangda890126;;"/>
    <%-- 通過update標(biāo)簽添加數(shù)據(jù) --%>
    <sql:update>
      INSERT INTO user(userid,username,password) VALUES(null,"admin1","root1");
    </sql:update>
  </body>
</html>



<sql:query>標(biāo)簽
用來查詢數(shù)據(jù)庫中的數(shù)據(jù)
第一種格式:

復(fù)制代碼 代碼如下:

<sql:query sql="sqlQuery" [var="varName"] [scope="{page|request|session|application}"][dataSource="dataSource"]maxRows="" startRow="startRow" />

第二種格式是將SQL語句作為本體內(nèi)容

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
  <head>
    <title>Query標(biāo)簽的使用</title>
  </head>
  <body>
    <%-- 指定數(shù)據(jù)庫鏈接URL,JDBC驅(qū)動,用戶名和密碼 --%>
    <sql:setDataSource url="jdbc:mysql://localhost:3306/javaweb" driver="com.mysql.jdbc.Driver"
        user="root" password="zhangda890126;;"/>
    <%-- 通過update標(biāo)簽添加數(shù)據(jù) --%>
    <sql:query var="result">
      SELECT * FROM user
    </sql:query>
    <%-- 顯示所有的數(shù)據(jù) --%>
    <center>
      <h3>查詢所有的數(shù)據(jù)</h3>
      <table border="1">
        <tr>
          <td>userID</td>
          <td>userName</td>
          <td>password</td>
        </tr>
        <%-- 使用foreach循環(huán)輸出所有的值 --%>
        <c:forEach items="${result.rows}" var = "row">
          <tr>
            <td>${row.userid}</td>
            <td>${row.username}</td>
            <td>${row.password}</td>
          </tr>
        </c:forEach>
      </table>
    </center>
  </body>
</html>


<sql:param>標(biāo)簽和<sql:dateParam>標(biāo)簽
<sql:param>有兩種格式即:帶有本體內(nèi)容和不帶本體內(nèi)容的

不帶本體內(nèi)容的格式為

<sql:param value="value" />

帶本體內(nèi)容的格式為

<sql:param>

本體內(nèi)容

</sql:param>

<sql:dateParam>標(biāo)簽的格式為:

<sql:dateParam value="value" [type="{timestamp|time|date}"] />

如果參數(shù)與時間和日期有關(guān)的話就使用<sql:dateParam>標(biāo)簽


<sql:transaction>標(biāo)簽
提供存取數(shù)據(jù)庫時的一種安全機制(事物處理安全機制)

格式為:

<sql:transaction [dataSource="dataSource"] [isolation="read_committed|read_uncomited|repeatabl_read|serializable"]>

<sql:update> or <sql:query>

</sql:transaction>

 

相關(guān)文章

最新評論