JSTL tag Library: SQL Tag Library

Source: Internet
Author: User
Tags connect range valid

SQL Tag Library
JSTL provides a database-related operation of the label, you can directly from the page to implement the functions of database operations, in the development of small Web sites can be very convenient to achieve data reading and operation. This chapter describes in detail the features and usage of these tags.
The SQL Tag library can be functionally divided into two categories: setting the data source label, and the SQL instruction tag.
The directive code that introduces the SQL tag library is:
<%@ taglib prefix= "sql" uri= "Http://java.sun.com/jsp/jstl/sql"%>
1 Setting up the data source
The use of <sql:setDataSource> tags enables you to configure your data source.
"Syntax 1": Use a data source that already exists directly.
<sql:setdatasource datasource= "DataSource" [var= "name"] [scope= "Page|request|session|application"]/>

Syntax 2: Establishing a database connection using JDBC.
<sql:setdatasource driver= "Driverclass" url= "Jdbcurl" user= "username" password= "pwd" [var= "name"] [scope=] page| Request|session|application "]/>

<sql:DataSource> Label Attribute Description
Parameter name: Description: EL: Type: must: Default value
DataSource: Data Source: Yes: String, Javax.sql.DataSource: No: None
Driver: JDBC driver used: Yes: String: No: None
URL: Connection to database path: Yes: No: No
User: The username to connect to the database: Yes, String: No: None
Password: the password to connect to the database: is: String: No: None
var: Specify variable name to store data source: No: String: No: None
Scope: Specify the range of JSP that the data source stores: No: No: page
Hint: Whether it must be relative, for example, if you use a data source, driver, URLs, and so on are no longer used. If you use JDBC, you use the driver, URL, user, password properties.

2 SQL Operation label
Jstl offers <sql:query>, <sql:update>, <sql:param>, <sql:dateParam> and <sql:transaction> These 5 tags, through the use of SQL language to operate the database, to achieve additions, deletions, modifications and other operations. The features and usage of these 5 tabs are described below.

1. <sql:query> Label
<sql:query> tags are used to query data.
"Syntax 1":
<sql:query sql= "SQLQuery" var= "name" [scope= "Page|request|session|application"] [datasource= "DateSource"] [ Maxrow= "MaxRow"] [startrow= "Starrow"]/>

"Syntax 2":
<sql:query var= "Name" [scope= "Page|request|session|application"] [datasource= "Datesource"] [maxRow= "MaxRow"] [ startrow= "Starrow"]>
SQLQuery
</sql:query>

<sql:query> Label Attribute Description
Parameter name: Description: EL: Type: must: Default value
SQL: SQL statement that queries the data: Yes: String: Yes: none
DataSource: Data source object: Yes: String, Javax.sql.DataSoutce: No: None
MaxRow: Sets the maximum number of rows that can be staged:: No: No
StartRow: Set starting from that line of data:: String: No: None
var: Specify variable name to store query results: No: String: Yes: none
Scope: Specifies the scope of the result: No: String: No: Page
Use <sql:query> You must specify a data source, DataSource is optional, if you do not give the property label to find out if the data source is set in the page range, an exception will be thrown if it is not found.
In general, use the <sql:setDateSource> tab to set up a data source to store in the session scope, use the DataSource property when you need a database connection, and implement the database operation.
The <sql:query> var attribute is required to hold the result set, and the default is page if no scope is specified, that is, we can output the query results at any time on the current page.
The MaxRows and StartRow properties are used to manipulate the result set, using the SQL statement first the data is placed in memory, check whether the StartRow property is set, and if it is set, the MaxRows value is started from the line specified by Starrow, and if not set, start at the first line.

Result set parameter description
Attribute name: Type: Description
Rowcount:int: The total number of records in the result set, which is the amount of valid records in the result set, that can be used for paging display of large amounts of data.
ROWS:JAVA.UTIL.MAP: The result of a field-indexed query gets the corresponding value of each field. The result returned is: Field name ={field value ...}
Rowsbyindex:object[]: A number of indexed query results, commonly used to get the data in the database, from the first element of a valid row to start the traversal, to the last valid row of the last element.
Columnnames:string[]: An array of field names used to get the field names in the database.
Limitedbymaxrows:boolean: Whether the MaxRows attribute is set to limit the number of query records to determine whether the maxrows is restricted.
Tip: Limitedbymaxrows is used to determine whether a program has received a MaxRows property restriction. It's not that the MaxRows property is set, the Limitedbymaxrows property of the result set is true, and when the result set is removed less than maxrows, MaxRows does not play a role in the result set and false at this time. For example, you can use the StartRow property to limit the amount of data in a result set.
The purpose of the result set is to define how the data is displayed on the page.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/JSP/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.