国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Jakarta Project: DBTags標簽庫(Pre Beta)

2019-11-18 14:17:38
字體:
來源:轉載
供稿:網友

  概述
DBTags 自定義標簽庫用于讀寫SQL數據庫

需要的條件
jsp 需求

這個需要一個支持JSP規范1.2版本以上的servlet引擎。

雖然它也可以工作于某些如tomcat這樣的JSP 1.1 版引擎,但是不能工作在如Weblogic 這樣的引擎上。它是根據JSP 1.2 規范設計的,需要<jsp:getPRoperty ... /> 標簽:

jsp:setProperty 和 jsp:getProperty 中的name屬性的值是通過pageContext 對象的findAttribute()方法得到的對象.
JSP 1.1 規范不要求這個行為而tomcat碰巧支持而Weblogic 不支持。也有相當直接的方法為Weblogic 的用戶寫一個自定義標簽效仿這個行為。已經有現成的范例可以從 這里得到。

DBTags 需求

DBTags 庫支持數據源,而這不是java 2 標準版的一部分。為了能使用數據庫,要么使用J2EE,或者下載JDBC 2.0 Optional API 。

配置
使用下面步驟使你的web應用可以使用這個標簽庫:

拷貝標簽庫的描述文件dbtags.tld 到你的web應用的 /WEB-INF 子目錄下
拷貝標簽庫的 JAR 文件到應用的 /WEB-INF/lib 子目錄下。
在/WEB-INF/web.xml 下增加如下內容::
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/dbtags</taglib-uri>
<taglib-location>/WEB-INF/dbtags.tld</taglib-location>
</taglib>

在你的JSP 頁面中使用這個標簽庫,在每頁的頂部直接加上如下內容:

<%@標簽lib uri="http://jakarta.apache.org/taglibs/dbtags" prefix="sql" %>

"sql" 是你希望使用的標簽前綴,你可以將它改為你喜歡使用的值。

文檔
簡單使用范例
下面是一個打印表中的書名的JSP頁面源文件:

<%@標簽lib uri="http://jakarta.apache.org/taglibs/dbtags" prefix="sql" %>
<%-- open a database connection --%>
<sql:connection id="conn1">
<sql:url>jdbc:MySQL://localhost/test</sql:url>
<sql:driver>org.gjt.mm.mysql.Driver</sql:driver>
</sql:connection>
<%-- open a database query --%>
<table>
<sql:statement id="stmt1" conn="conn1">
<sql:query>
select id, name, description from test_books
order by 1
</sql:query>
<%-- loop through the rows of your query --%>
<sql:resultSet id="rset2">
<tr>
<td><sql:getColumn position="1"/></td>
<td><sql:getColumn position="2"/></td>
<td><sql:getColumn position="3"/>
<sql:wasNull>[no description]</sql:wasNull></td>
</tr>
</sql:resultSet>
</sql:statement>
</table>
<%-- close a database connection --%>
<sql:closeConnection conn="conn1"/>
標簽具體介紹
下面是DBTags標簽庫的總體描述,標簽的某些細節,例如connection, statement, resultSet, 和 preparedStatement 標簽的所有可能的屬性,在這里沒有討論。 Tag Reference 列出了所有的細節。

Connection標簽
打開連接

有三種方式打開一個數據庫連接:

1. 使用數據庫 URL

connection標簽可以接受一個數據庫URL通過Driver Manager獲得一個Connection:

<%-- open a database connection --%>
<sql:connection id="conn1">
<%-- required --%>
<sql:url>jdbc:mysql://localhost/test</sql:url>
<%-- optional --%>
<sql:driver>org.gjt.mm.mysql.Driver</sql:driver>
<%-- optional --%>
<sql:userId>root</sql:userId>
<%-- optional --%>
<sql:passWord>notVerySecure</sql:password>
</sql:connection>
"id"屬性是必須的。在結束標簽后,一個java.sql.Connection 對象被加為一個pageContext屬性,可以被包括statement, preparedStatement, 和 closeConnection的其它的標簽使用。

不在標簽體內包含數據庫URL, 驅動器名,用戶id,或者口令,你可以使用可選屬性"initParameter":

<%-- store your connection info in the web.xml file --%>
<sql:connection id="conn1">
<sql:url initParameter="dbURL"/>
<sql:driver initParameter="mysqlDriver"/>
<sql:userId initParameter="dbUserId"/>
<sql:password initParameter="dbPassword"/>
</sql:connection>
2. 使用數據源

connection也可以接受一個指向Servlet屬性的javax.sql.DataSource對象的引用。(這個屬性是通過PageContext的findAttribute()方法得到的。):

<%-- open a database connection --%>
<sql:connection id="conn1" dataSource="ds1">
<%-- optional --%>
<sql:userId>root</sql:userId>
<%-- optional --%>
<sql:password>notVerySecure</sql:password>
</sql:connection>
3. 使用JNDI命名的JDBC數據源

Connection也可以接受一個使用JNDI命名的JDBC數據源。

<%-- open a database connection --%>
<sql:connection id="conn1" jndiName="java:/comp/jdbc/test"/>
關閉連接

將一個connection的引用傳遞到"closeConnection" 標簽關閉一個連接:

<%-- 除非你使用自己的連接池,否則總應該關閉連接 --%>
<sql:closeConnection conn="conn1"/>
Statement標簽
"Statements"是向數據庫提交查詢的一種方法。(另一個是使用 "preparedStatement"。) 基于statement查詢的語法對任何知道SQL的人都是不生疏的。為了查詢數據庫,打開一個"statement"標簽,傳遞給它一個sql "query", 然后要么對inserts, updates, 和 deletes "execute"申明,或者調用resultSet 標簽在一個select申明的結果上循環執行。下面是一個簡單的insert:

<%-- 向數據庫插入一行 --%>
<sql:statement id="stmt1" conn="conn1">
<%-- 設置SQL查詢 --%>
<sql:query>
insert into test_books (id, name)
values (3, ´<sql:escapeSql><%=request.getParameter("book_title")%></sql:escapeSql>´)
</sql:query>
<%-- 執行查詢 --%>
<sql:execute/>
</sql:statement>
轉義SQL

"escapeSql"標簽用在一個SQL查詢里面轉義輸入的值里面可能的單引號。

錯誤處理

缺省情況下,SQL查詢的執行導致的錯誤(例如主鍵violations(違例),殘缺的SQL申明)將導致JSP頁面的失敗,你可以選擇性的設置"execute"標簽的"ignoreErrors"屬性為"true",這將使SQL錯誤打印到標準輸出而不會終止頁面:

<sql:statement id="stmt1" conn="conn1">
<%-- 這個SQL查詢是殘缺的 --%>
<sql:query>delete * from test_books</sql:query>
<%-- 查詢將失敗,但是頁面會繼續 --%>
<sql:execute ignoreErrors="true"/>
</sql:statement>
空白處理

所有的statement和preparedStatement自動的去除空白。

PreparedStatement標簽
"Prepared statements"是產生SQL查詢的比較高級的形式。它不是直接將值插入SQL申明中,而是在需要設置值得地方放入一個´?´符號,然后使用一組獨立的標簽實際設置那些值。下面是statement中使用的范例的preparedstatement版本:

<%-- 向數據庫插入一行 --%>
<sql:preparedStatement id="stmt1" conn="conn1">
<%-- 設置SQL查詢。注重"name"值上缺少引號 --%>
<sql:query>
insert into test_books (id, name)
values (?, ?)
</sql:query>
<sql:execute>
<sql:setColumn position="1">3</sql:setColumn>
<sql:setColumn position="2"><%=request.getParameter("book_title")%></sql:setColumn>
</sql:execute>
</sql:preparedStatement>
prepared statements的一個優點就是你不需要在文本上執行sql轉義。然而,記住標準的statements對于那些沒有連接池和prepared statements的數據庫和驅動器在性能上更好。

setColumn標簽

你可以將prepared statements的setColumn標簽放置在execute或者 resultset標簽的前面, 或者是execute標簽的里面。execute標簽永遠不會輸出它的內容(body),因此將setColumn標簽放置在里面可以防止不必要的空白。

ResultSet標簽
Resultset是一個select申明的結果。resultSet標簽自動循環,每次一行。使用 "getColumn"標簽從每行中提取值然后要么顯示他們,要么將他們存為字符串:

<%--在一個Html表格里面打印行 --%>
<table>
<sql:statement id="stmt1" conn="conn1">
<sql:query>
select id, name, description from test_books
order by 1
</sql:query>
<%-- 循環提取查詢結果中的行 --%>
<sql:resultSet id="rset2">
<tr>
<td><sql:getColumn position="1"/></td>
<td><sql:getColumn position="2"/></td>
<td><sql:getColumn position="3"/>
<%-- 假如書沒有說明則打印一個注釋 --%>
<sql:wasNull>[no description]</sql:wasNull></td>
</tr>
</sql:resultSet>
</sql:statement>
</table>
"wasNull"和"wasNotNull"標簽

"wasNull"標簽只有在前面的"getColumn"標簽碰到一個數據庫中的空值(null)時執行它的體中的內容。你只能在一個resultset內并且"getColumn"標簽已經被執行時使用"wasNull"標簽。"wasNotNull"標簽在它前面的getColumn標簽沒有產生一個空值(null)使執行它的體中的內容。參看Tag參考獲得范例。

"getColumn"標簽

getColumn標簽執行兩個中的一個功能。你可以:

直接向JSP輸出列值(缺省行為)

<%-- 向JSP輸出值 --%>
<sql:getColumn position="1"/>
, 或者

將值作為一個String對象寫為頁面的一個屬性,通過"to"屬性。假如你愿意,你也可以為"scope"屬性分配一個不同于"page"的值。假如數據庫中該列的值為null, getColumn標簽 將不會創建屬性。下面是一個使用getColumn標簽產生一個整型的請求屬性:

<%-- 注重請求的屬性將是一個String --%>
<sql:getColumn position="1" to="someId" scope="request"/>
"getNumber"標簽

假如你想對數字格式有更多的控制,使用getNumber標簽。

"format"屬性可以是DecimalFormat構造方法可以接受的模式或者是下面的類型: "CURRENCY", "PERCENT" 或者 "NUMBER"。

"locale"屬性可以有一到三個部分,也就是Locale構造方法可以接受的形式: 語言, 國家 和 變量。它們使用"_"分割。例如:


<%-- 格式化數據庫值為英國貨幣形式 --%>
<sql:getNumber colName="id" format="CURRENCY" locale="en_GB"/>

假如format和locale屬性都沒有設置,輸出將和getColumn一樣。
time標簽

有幾個標簽是設計用來顯示時間相關的數據的: getTime, getTimestamp 和 getDate。

"format"屬性可以是被SimpleDateFormat接受的形式或者是一個類型: "FULL", "LONG", "MEDIUM" 或 "SHORT"。這個屬性是可選的。

"locale"屬性可以有一到三個部分,也就是Locale構造方法可以接受的形式: 語言, 國家 和 變量。它們使用"_"分割。

禁止循環

缺省情況下resultset標簽對ResultSet中的每行循環執行。通過設置可選屬性"loop"為"false"就可以禁止這個特性然后手工操作ResultSet對象或者將它傳遞給另外的自定義標簽。

<sql:statement id="stmt1" conn="conn1">
<sql:query>
select id, name, description from test_books
order by 1
</sql:query>
<%-- 禁止resultset標簽的循環 --%>
<sql:resultSet id="rset2" loop="false">
<%
ResultSet rset = (ResultSet) pageContext.getAttribute("rset2");
// 手工操作
%>
</sql:resultSet>
</sql:statement>
使用RowSets

你也可以用一個RowSet對象使用resultSet標簽。通過設置選項"name",resultSet標簽將查找一個ResultSet對象(包括RowSets)并將它以該名字存儲在page, request, 或者session上下文上。通過設置可選屬性"scope",你可以指定上下文來包含你的ResultSet/RowSet。注重當你從一個屬性中讀取一個ResultSet/RowSet,resultSet標簽可以不在statement標簽內。

<%-- 循環執行ResultSet/RowSet的每行,無論它來自何處 --%>
<sql:resultSet id="rset1" name="rsetAtt">
<tr>
<td><sql:getColumn position="1"/></td>
<td><sql:getColumn position="2"/></td>
<td><sql:getColumn position="3"/>
<%-- 假如書沒有說明則打印一個注釋 --%>
<sql:wasNull>[no description]</sql:wasNull></td>
</tr>
</sql:resultSet>
"wasEmpty"和"wasNotEmpty"標簽

"wasEmpty"標簽只有在上一個ResultSet標簽從數據庫中得到0行時執行它的體內的內容。它必須放在一個resultSet標簽后否則將出錯。"wasNotEmpty"標簽在上一個ResultSet從數據庫中得到了多于 0 行時執行體內的內容。參看Tag參考得到使用范例。

"rowCount"標簽

"rowCount"標簽打印數據庫返回的行數。可以在ResultSet標簽內使用它提供一個運行計數,或者在ResultSet標簽后面使用打印總數。參看Tag 參考得到使用范例。在ResultSet前使用該標簽將產生一個錯誤。

Tag概要
Connection標簽 connection 從DriverManager或者DataSource得到一個java.sql.Connection對象。
url 在封閉的connection標簽中設置數據庫URL。
jndiName 在封閉的connection標簽中設置JNDI命名的JDBC數據源。
driver 在封閉的connection標簽中設置驅動器類名。
userId 設置connection標簽的用戶名。
password 設置connection標簽的用戶名口令。
closeConnection 關閉指定的連接。"conn"屬性是該頁上下文上的一個connection對象。

單一的Statement標簽 statement 創建并執行一個數據庫查詢。
escapeSql 將標簽體中的每個單引號替換成一對單引號。

Statement/PreparedStatement標簽 query 為一個statement或者preparedStatement標簽設置一個查詢。
execute 為statement或者preparedStatement標簽執行一個insert, update 或者 delete。

單一的PreparedStatement標簽 preparedStatement 創建并執行一個記號化的數據庫查詢。
setColumn 設置preparedStatement中的一個字段。將值設置為標簽體內的一個字符串。

ResultSet標簽 resultSet 標簽resulset執行查詢并循環執行封閉的statement或者preparedstatement標簽中的結果。這個標簽體的內容在resultset的每行上執行。可選屬性"loop"(缺省為true)指定是否在每行上執行標簽體,或者只是簡單的將ResultSet分配給頁面的一個用"id"指定的屬性。
wasNull 假如前面的getColumn標簽從數據庫得到一個null值時執行標簽體。必須在一個resultset標簽內而且前面有一個getColumn標簽,否則將產生一個錯誤。
wasNotNull 假如上一個getColumn標簽從數據庫得到的不是一個null值執行標簽體。
getColumn 在封閉的resultset內得到字段值,作為一個字符串。字段索引通過"position"屬性設置,使用"to"屬性將該值設置為一個serlvet屬性而不是一個標簽體。servlet屬性的范圍使用"scope"屬性指定(缺省為page)。
getNumber 和getColumn相似,但是對數字格式提供更精確的控制。 "format"屬性可以是DecimalFormat構造函數可以接受的形式或者是一個類型: "CURRENCY","PERCENT" 或 "NUMBER"。 "locale"屬性可以有一到三個部分,也就是Locale構造方法可以接受的形式: 語言, 國家 和 變量。它們使用"_"分割。 假如format和locale屬性都沒有設置,輸出將和getColumn一樣。
getTime 和getColumn類似,但是對java.sql.Time格式提供更精確的控制。 "format"屬性可以是被SimpleDateFormat接受的形式或者是一個類型: "FULL", "LONG", "MEDIUM" 或 "SHORT"。這個屬性是可選的。 "locale"屬性可以有一到三個部分,也就是Locale構造方法可以接受的形式: 語言, 國家 和 變量。它們使用"_"分割。 假如format和locale屬性都沒有設置,輸出將和getColumn一樣。
getTimestamp 和getColumn類似,但是對java.sql.Timestamp格式提供更精確的控制。 "format"屬性可以是被SimpleDateFormat接受的形式或者是一個類型: "FULL", "LONG", "MEDIUM" 或 "SHORT"。這個屬性是可選的。 "locale"屬性可以有一到三個部分,也就是Locale構造方法可以接受的形式: 語言, 國家 和 變量。它們使用"_"分割。 假如format和locale屬性都沒有設置,輸出將和getColumn一樣。
getDate 和getColumn類似,但是對java.sql.Date格式提供更精確的控制。 "format"屬性可以是被SimpleDateFormat接受的形式或者是一個類型: "FULL", "LONG", "MEDIUM" 或 "SHORT"。這個屬性是可選的。 "locale"屬性可以有一到三個部分,也就是Locale構造方法可以接受的形式: 語言, 國家 和 變量。它們使用"_"分割。
wasEmpty 上一個ResultSet標簽從數據庫得到0行執行標簽體。必須在ResultSet標簽后使用否則將產生一個錯誤。
wasNotEmpty 上一個ResultSet標簽從數據庫得到多于0行執行標簽體。必須在ResultSet標簽后使用否則將產生一個錯誤。
rowCount "rowCount"標簽打印數據庫返回的行數。可以在ResultSet標簽內使用它提供一個運行計數,或者在ResultSet標簽后面使用打印總數。在ResultSet前使用該標簽將產生一個錯誤。


Tag 參考
connection Availability: 1.0
Get a java.sql.Connection object from the DriverManager or a DataSource.

Tag Body JSP
Restrictions None
Attributes Name Required Runtime EXPression Evaluation Availability
id yes no 1.0
Name of the resulting Connection attribute.

dataSource no no 1.0
Name of an existing page attribute that contains a DataSource object.

jndiName no no 1.0
Name used to find a datasource via jndi.

Variables Name Scope Availability
id attribute value End of tag to end of page 1.0

Properties Name Get Set Availability
catalog yes yes 1.0
Set the catalog for this connection.

closed yes no 1.0
False if the connection is open, true if it is not.

readOnly yes yes 1.0
True if the connection has read-only permission.

Examples Method 1: using the DriverManager


<%-- open a database connection --%>
<sql:connection id="conn1">
<%-- required --%>
<sql:url>jdbc:mysql://localhost/test</sql:url>
<%-- optional --%>
<sql:driver>org.gjt.mm.mysql.Driver</sql:driver>
<%-- optional --%>
<sql:userId>root</sql:userId>
<%-- optional --%>
<sql:password>notVerySecure</sql:password>
</sql:connection>




Method 2: using a DataSource


<%-- open a database connection --%>
<sql:connection id="conn1" dataSource="ds1">
<%-- optional --%>
<sql:userId>root</sql:userId>
<%-- optional --%>
<sql:password>notVerySecure</sql:password>
</sql:connection>




Method 3: using a jndi named DataSource


<%-- open a database connection --%>
<sql:connection id="conn1" jndiName="java:/comp/jdbc/test"/>




url Availability: 1.0
Sets the database URL of the enclosing connection tag.

Tag Body JSP
Restrictions Use inside a connection tag.
Attributes Name Required Runtime Expression Evaluation Availability
initParameter no no 1.0
Optional attribute, indicating the name of an init parameter

Variables None
Examples


<%-- example 1: using the tag body --%>
<sql:connection id="conn1">
<sql:url>jdbc:mysql://localhost/test</sql:url>
</sql:connection>
<%-- example 2: using an init parameter --%>
<sql:connection id="conn1">
<sql:url initParameter="dbURL"/>
</sql:connection>




jndiName Availability: 1.0
Sets the JNDI named JDBC DataSource of the enclosing connection tag.

Tag Body JSP
Restrictions Use inside a connection tag.
Attributes Name Required Runtime Expression Evaluation Availability
initParameter no no 1.0
Optional attribute, indicating the name of an init parameter

Variables None
Examples


<%-- example 1: using the tag body --%>
<sql:connection id="conn1">
<sql:jndiName>java:/comp/jdbc/test</sql:jndiName>
</sql:connection>
<%-- example 2: using an init parameter --%>
<sql:connection id="conn1">
<sql:jndiName initParameter="jndiDataSource"/>
</sql:connection>




driver Availability: 1.0
Sets the driver class name for the connection tag.

Tag Body JSP
Restrictions Use inside a connection tag.
Attributes Name Required Runtime Expression Evaluation Availability
initParameter no no 1.0
Optional attribute, indicating the name of an init parameter.

Variables None
Examples


<%-- example 1: using the tag body --%>
<sql:connection id="conn1">
<sql:url>jdbc:mysql://localhost/test</sql:url>
<sql:driver>org.gjt.mm.mysql.Driver</sql:driver>
</sql:connection>
<%-- example 2: using an init parameter --%>
<sql:connection id="conn1">
<sql:url initParameter="dbURL"/>
<sql:driver initParameter="dbDriver"/>
</sql:connection>




userId Availability: 1.0
Sets the user id for the connection tag.

Tag Body JSP
Restrictions Use inside a connection tag.
Attributes Name Required Runtime Expression Evaluation Availability
initParameter no no 1.0
Optional attribute, indicating the name of an init parameter.

Variables None
Examples


<%-- example 1: using the tag body --%>
<sql:connection id="conn1">
<sql:url>jdbc:mysql://localhost/test</sql:url>
<sql:userId>root</sql:userId>
</sql:connection>
<%-- example 2: using an init parameter --%>
<sql:connection id="conn1">
<sql:url initParameter="dbURL"/>
<sql:userId initParameter="dbUserId"/>
</sql:connection>




password Availability: 1.0
Sets the password for the connection tag.

Tag Body JSP
Restrictions Use inside a connection tag.
Attributes Name Required Runtime Expression Evaluation Availability
initParameter no no 1.0
Optional attribute, indicating the name of an init parameter.

Variables None
Examples


<%-- example 1: using the tag body --%>
<sql:connection id="conn1">
<sql:url>jdbc:mysql://localhost/test</sql:url>
<sql:userId>root</sql:userId>
<sql:password>notVerySecure</sql:password>
</sql:connection>
<%-- example 2: using an init parameter --%>
<sql:connection id="conn1">
<sql:url initParameter="dbURL"/>
<sql:userId initParameter="dbUserId"/>
<sql:password initParameter="dbPassword"/>
</sql:connection>




closeConnection Availability: 1.0
Close the specified connection. The "conn" attribute is the name of a connection object in the page context.

Tag Body empty
Restrictions None
Attributes Name Required Runtime Expression Evaluation Availability
conn yes no 1.0
Id of the connection you want to close.

Variables None
Examples


<%-- open a database connection --%>
<sql:connection id="conn1">
<sql:url>jdbc:mysql://localhost/test</sql:url>
<sql:userId>root</sql:userId>
<sql:password>notVerySecure</sql:password>
</sql:connection>
<%-- statement tags go here --%>
<sql:closeConnection conn="conn1"/>




statement Availability: 1.0
Create and execute a database query.

Tag Body JSP
Restrictions None
Attributes Name Required Runtime Expression Evaluation Availability
id yes no 1.0
Script variable id for use with standard jsp:getProperty tag.

conn yes no 1.0
id of the connection to use

Variables Name Scope Availability
id attribute value Nested within tag 1.0

Properties Name Get Set Availability
fetchSize yes yes 1.0
the number of rows that should be fetched from the database when more rows are needed

maxRows yes yes 1.0
the maximum number of rows that a ResultSet object can contain (handy!)

queryTimeout yes yes 1.0
the number of seconds the driver will wait for a Statement object to execute

Examples


<%-- insert a row into the database --%>
<sql:statement id="stmt1" conn="conn1">
<%-- set the SQL query --%>
<sql:query>
insert into test_books (id, name)
values (3,
´<sql:escapeSql><%= request.getParameter("book_title") %></sql:escapeSql>´)
</sql:query>
<%-- execute the query --%>
<sql:execute/>
</sql:statement>




escapeSql Availability: 1.0
Replaces each single quote in the tag body with a pair of single quotes.

Tag Body JSP
Restrictions Use inside a query tag.
Attributes None
Variables None
Examples


<%-- insert a row into the database --%>
<sql:statement id="stmt1" conn="conn1">
<%-- set the SQL query --%>
<sql:query>
insert into test_books (id, name)
values (3,
´<sql:escapeSql><%=request.getParameter("book_title")%></sql:escapeSql>´)
</sql:query>
<%-- execute the query --%>
<sql:execute/>
</sql:statement>




query Availability: 1.0
Set a query for a statement or preparedStatement tag

Tag Body JSP
Restrictions Use inside a statement or preparedStatement tag.
Attributes None
Variables None
Examples


<%-- insert a row into the database --%>
<sql:statement id="stmt1" conn="conn1">
<%-- set the SQL query --%>
<sql:query>
insert into test_books (id, name)
values (3, ´<sql:escapeSql><%=request.getParameter("book_title")%></sql:escapeSql>´)
</sql:query>
<%-- execute the query --%>
<sql:execute/>
</sql:statement>




execute Availability: 1.0
Executes an insert, update or delete for a statement or preparedStatement tag

Tag Body JSP
Restrictions Use inside a statement or preparedStatement tag.
Attributes None
Variables None
Examples


<%-- insert a row into the database --%>
<sql:statement id="stmt1" conn="conn1">
<%-- set the SQL query --%>
<sql:query>
insert into test_books (id, name)
values (3, ´<sql:escapeSql><%=request.getParameter("book_title")%></sql:escapeSql>´)
</sql:query>
<%-- execute the query --%>
<sql:execute/>
</sql:statement>




preparedStatement Availability: 1.0
Create and execute a tokenized database query

Tag Body JSP
Restrictions The scipt variable is not available until after the query tag is called.
Attributes Name Required Runtime Expression Evaluation Availability
id yes no 1.0
Script variable id

conn yes no 1.0
id of the connection to use

Variables Name Scope Availability
id attribute value Nested within tag 1.0

Properties Name Get Set Availability
fetchSize yes yes 1.0
the number of rows that should be fetched from the database when more rows are needed

maxRows yes yes 1.0
the maximum number of rows that a ResultSet object can contain (handy!)

queryTimeout yes yes 1.0
the number of seconds the driver will wait for a Statement object to execute

Examples


<%-- insert a row into the database --%>
<sql:preparedStatement id="stmt1" conn="conn1">
<sql:query>
insert into test_books (id, name)
values (?, ?)
</sql:query>
<sql:execute>
<sql:setColumn position="1">3</sql:setColumn>
<sql:setColumn position="2"><%=request.getParameter("book_title")%></sql:setColumn>
</sql:execute>
</sql:preparedStatement>




setColumn Availability: 1.0
Set a field in a preparedStatement. Set the value as a String inside the tag body.

Tag Body JSP
Restrictions Use within the preparedStatement tag
Attributes Name Required Runtime Expression Evaluation Availability
position yes no 1.0
Column position

Variables None
Examples


<%-- use the tag body --%>
<sql:setColumn position="1"><%= someValue %></sql:setColumn>




resultSet Availability: 1.0
JSP tag resulset, executes the query and loops through the results for the enclosing statement or preparedstatement tag. The body of this tag is executed once per row in the resultset. The optional "loop" attribute, which default to true, specifies whether to execute the tag body once per row "true", or to simply assign the ResultSet to the page attribute specified by "id".

Tag Body JSP
Restrictions If a name attribute is not supplied, use within a statement or preparedStatement and after a query. If a name attribute is supplied, there are no restrictions.
Attributes Name Required Runtime Expression Evaluation Availability
id yes no 1.0
Script variable id

loop no no 1.0
True: execute the tag body once per row in the result set, automatically advancing the rows. False: execute the tag body once.

name no no 1.0
Name of an attribute containing a ResultSet object. If you pull a ResultSet object from an attribute, it is not necessary to place this tag inside of a statement.

scope no no 1.0
Scope (page, request, session, or application) to search for the ResultSet attribute indicated in the "name" attribute. If this is not supplied, we use the default findAttribute() behaviour.

Variables Name Scope Availability
id attribute value Nested within tag 1.0

Properties Name Get Set Availability
fetchSize yes yes 1.0
the number of rows that should be fetched from the database when more rows are needed

Examples


<%-- open a database query --%>
<table>
<sql:statement id="stmt1" conn="conn1">
<sql:query>
select id, name, description from test_books
order by 1
</sql:query>
<%-- loop through the rows of your query --%>
<sql:resultSet id="rset2">
<tr>
<td><sql:getColumn position="1"/></td>
<td><sql:getColumn position="2"/></td>
<td><sql:getColumn position="3"/>
<sql:wasNull>[no description]</sql:wasNull></td>
</tr>
</sql:resultSet>
</sql:statement>
</table>




wasNull Availability: 1.0
Executes its body if the last getColumn tag received a null value from the database. You must be inside a resultset tag and there must be a previous getColumn tag, or an error will be generated.

Tag Body JSP
Restrictions Must be used following a getColumn tag.
Attributes None
Variables None
Examples


<%-- open a database query --%>
<table>
<sql:statement id="stmt1" conn="conn1">
<sql:query>
select id, name, description from test_books
order by 1
</sql:query>
<%-- loop through the rows of your query --%>
<sql:resultSet id="rset2">
<tr>
<td><sql:getColumn position="1"/></td>
<td><sql:getColumn position="2"/></td>
<td><sql:getColumn position="3"/>
<sql:wasNull>[no description]</sql:wasNull></td>
</tr>
</sql:resultSet>
</sql:statement>




wasNotNull Availability: 1.0
Executes its body if the last getColumn tag did not encounter a null value from the database.

Tag Body JSP
Restrictions Must be used following a getColumn tag.
Attributes None
Variables None
Examples


<%-- open a database query --%>
<table>
<sql:statement id="stmt1" conn="conn1">
<sql:query>
select id, name, description from test_books
order by 1
</sql:query>
<%-- loop through the rows of your query --%>
<sql:resultSet id="rset2">
<tr>
<td><sql:getColumn position="1"/></td>
<td><sql:getColumn position="2"/></td>
<td><sql:getColumn position="3" to="description"/>
<sql:wasNotNull>Description: <%= pageContext.getAttribute("description") %></sql:wasNotNull></td>
</tr>
</sql:resultSet>
</sql:statement>




getColumn Availability: 1.0
Gets the value, as a String, of a coulmn in the enclosing resultset. The column number is set via the "position" attribute. You can optionally set the value, as a String, to a serlvet attribute instead of the tag body with the "to" attribute. The scope of the servlet attribute is specified by the "scope" XML attribute (default = page).

Tag Body empty
Restrictions Use within the resultSet tag
Attributes Name Required Runtime Expression Evaluation Availability
position no no 1.0
Column position

colName no no 1.0
Column name

to no no 1.0
Optionally assign the String to an attribute rather than the JSP output.

scope no no 1.0
Optionally change the scope of the attribute designated in "to" (default = page).

Variables None
Examples


<%-- output to the JSP directly --%>
<sql:getColumn position="1"/>




getNumber Availability: 1.0
Similar to getColumn, but provides more precise control over number formatting. The "format" attribute can be either a pattern as accepted by the DecimalFormat constrUCtor or a style: "CURRENCY", "PERCENT" or "NUMBER". The "locale" attribute can have one to three components as accepted by the Locale constructor: language, country and variant. They are separated by "_". If neither the format nor locale attribute is set, output should be identical to getColumn.

Tag Body empty
Restrictions Use within the resultSet tag
Attributes Name Required Runtime Expression Evaluation Availability
position no no 1.0
Column position

colName no no 1.0
Column name

to no no 1.0
Optionally assign the String to an attribute rather than the JSP output.

scope no no 1.0
Optionally change the scope of the attribute designated in "to" (default = page).

locale no yes 1.0
Format according to a particular locale.

format no yes 1.0
Specify a format for the number.

Variables None
Examples


<%-- format a database value as English currency --%>
<sql:getNumber colName="id" format="CURRENCY" locale="en_GB"/>




getTime Availability: 1.0
Similar to getColumn, but provides more precise control over java.sql.Time formatting. The "format" attribute can be either a pattern as accepted by SimpleDateFormat or a style: "FULL", "LONG", "MEDIUM" or "SHORT". The "locale" attribute can have one to three components as accepted by the Locale constructor: language, country and variant. They are separated by "_". If neither the format nor locale attribute is set, output should be identical to getColumn.

Tag Body empty
Restrictions Use within the resultSet tag
Attributes Name Required Runtime Expression Evaluation Availability
position no no 1.0
Column position

colName no no 1.0
Column name

to no no 1.0
Optionally assign the String to an attribute rather than the JSP output.

scope no no 1.0
Optionally change the scope of the attribute designated in "to" (default = page).

locale no yes 1.0
Format according to a particular locale.

format no yes 1.0
Specify a format for the time.

Variables None
Examples


<sql:getTime colName="time"/>




getTimestamp Availability: 1.0
Similar to getColumn, but provides more precise control over java.sql.Timestamp formatting. The "format" attribute can be either a pattern as accepted by SimpleDateFormat or a style: "FULL", "LONG", "MEDIUM" or "SHORT". The "locale" attribute can have one to three components as accepted by the Locale constructor: language, country and variant. They are separated by "_". If neither the format nor locale attribute is set, output should be identical to getColumn.

Tag Body empty
Restrictions Use within the resultSet tag
Attributes Name Required Runtime Expression Evaluation Availability
position no no 1.0
Column position

colName no no 1.0
Column name

to no no 1.0
Optionally assign the String to an attribute rather than the JSP output.

scope no no 1.0
Optionally change the scope of the attribute designated in "to" (default = page).

locale no yes 1.0
Format according to a particular locale.

format no yes 1.0
Specify a format for the timestamp.

Variables None
Examples


<sql:getTimestamp colName="time"/>




getDate Availability: 1.0
Similar to getColumn, but provides more precise control over java.sql.Date formatting. The "format" attribute can be either a pattern as accepted by SimpleDateFormat or a style: "FULL", "LONG", "MEDIUM" or "SHORT". It is required. The "locale" attribute can have one to three components as accepted by the Locale constructor: language, country and variant. They are separated by "_".

Tag Body empty
Restrictions Use within the resultSet tag
Attributes Name Required Runtime Expression Evaluation Availability
position no no 1.0
Column position

colName no no 1.0
Column name

to no no 1.0
Optionally assign the String to an attribute rather than the JSP output.

scope no no 1.0
Optionally change the scope of the attribute designated in "to" (default = page).

locale no yes 1.0
Format according to a particular locale.

format no yes 1.0
Specify a format for the date.

Variables None
Examples


<sql:getDate colName="time" format="FULL"/>




wasEmpty Availability: 1.0
Executes its body if the last ResultSet tag received 0 rows from the database. You must be after a ResultSet tag, or an error will be generated.

Tag Body JSP
Restrictions Use after a ResultSet tag.
Attributes None
Variables None
Examples


<%-- showing the contents of the table --%>
<table>
<tr><th>id</th><th>name</th><th>description</th></tr>
<sql:preparedStatement id="stmt6" conn="conn1">
<sql:query>
select id, name, description from test_books
</sql:query>
<sql:resultSet id="rset4">
<tr>
<td><sql:getColumn position="1"/></td>
<td><sql:getColumn position="2"/></td>
<td><sql:getColumn position="3" to="description"/></td>
</tr>
</sql:resultSet>
<tr>
<td colspan="3">
<%-- show different text, depending on whether or not
any rows were retrieved --%>
<sql:wasEmpty>No rows retrieved.</sql:wasEmpty>
<sql:wasNotEmpty><sql:rowCount/> rows retrieved.</sql:wasNotEmpty>
</td>
</tr>
</sql:preparedStatement>




wasNotEmpty Availability: 1.0
Executes its body if the last ResultSet tag received more than 0 rows from the database. You must be after a ResultSet tag, or an error will be generated.

Tag Body JSP
Restrictions Use after a ResultSet tag.
Attributes None
Variables None
Examples


<%-- showing the contents of the table --%>
<table>
<tr><th>id</th><th>name</th><th>description</th></tr>
<sql:preparedStatement id="stmt6" conn="conn1">
<sql:query>
select id, name, description from test_books
</sql:query>
<sql:resultSet id="rset4">
<tr>
<td><sql:getColumn position="1"/></td>
<td><sql:getColumn position="2"/></td>
<td><sql:getColumn position="3" to="description"/></td>
</tr>
</sql:resultSet>
<tr>
<td colspan="3">
<%-- show different text, depending on whether or not
any rows were retrieved --%>
<sql:wasEmpty>No rows retrieved.</sql:wasEmpty>
<sql:wasNotEmpty><sql:rowCount/> rows retrieved.</sql:wasNotEmpty>
</td>
</tr>
</sql:preparedStatement>




rowCount Availability: 1.0
Prints out the number of rows retrieved from the database. It can be used inside a ResultSet tag to provide a running count of rows retreived, or after the ResultSet tag to display the total number. Using the tag before the ResultSet will produce an error.

Tag Body empty
Restrictions Use inside or after a ResultSet tag (not before).
Attributes None
Variables None
Examples


<%-- showing the contents of the table --%>
<table>
<tr><th>id</th><th>name</th><th>description</th></tr>
<sql:preparedStatement id="stmt6" conn="conn1">
<sql:query>
select id, name, description from test_books
</sql:query>
<sql:resultSet id="rset4">
<tr>
<td><sql:getColumn position="1"/></td>
<td><sql:getColumn position="2"/></td>
<td><sql:getColumn position="3" to="description"/></td>
</tr>
</sql:resultSet>
<tr>
<td colspan="3">
<%-- show different text, depending on whether or not
any rows were retrieved --%>
<sql:wasEmpty>No rows retrieved.</sql:wasEmpty>
<sql:wasNotEmpty><sql:rowCount/> rows retrieved.</sql:wasNotEmpty>
</td>
</tr>
</sql:preparedStatement>





Examples
See the example application DBTags-examples.war for examples of the usage of the tags from this custom tag library.

Java Docs
Java programmers can view the java class documentation for this tag library as javadocs.

Revision History
Review the complete revision history of this tag library.

Developers´ Notes
Last updated: 08/14/2001 <div id="developers-notes">
On the radar screen
Here´s a list of what´s cooking with DBTags. If you´ve made a suggestion or contributed a patch that you think we´ve missed, send a note to taglibs-user@jakarta.apache.org.

To-do:

Add support for RowSets. (considering several contributions) [Update: Preliminary support has been added, see the history for details]

Under consideration:

ResultSet/RowSet "paging". (Ciot submitted some code to do this, which we plan to review and see if it´s a sufficiently general solution.)

On the back burner:

Connection management. There has been some discussion lately on if/how to terminate connections upon a JSP page error without making the usage too clunky. I think we´re still waiting for that spark of inspiration.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台南县| 托里县| 定州市| 江孜县| 宜州市| 石台县| 云龙县| 苏尼特左旗| 崇义县| 孙吴县| 肥西县| 郧西县| 英吉沙县| 郯城县| 南宫市| 上蔡县| 禹城市| 滦平县| 博客| 梁河县| 师宗县| 乳山市| 乌拉特后旗| 东明县| 始兴县| 隆林| 都安| 天全县| 广东省| 永清县| 浏阳市| 呼玛县| 怀仁县| 阿勒泰市| 杨浦区| 梅州市| 垣曲县| 吐鲁番市| 寿宁县| 兴宁市| 剑河县|