Jdbctm guide: Getting started 5-resultset

Source: Internet
Author: User

5-resultset
This overview is taken from jdbctm database access from javatm: a tutorial and annotated reference. Javasoft is currently preparing this book. This is a tutorial and an important reference manual for JDBC, which will be published by Addison-Wesley in the spring of 1997 as part of the Java series.

5.1 Overview
The resultset contains all rows that meet the conditions in the SQL statement, and provides access to the data in these rows through a set of get methods (these get methods can access different columns in the current row. The resultset. Next method is used to move to the next row in the resultset to make the next row the current row.

A result set is generally a table with the title and value of the column returned by the query. For example, if the query is select a, B, c from Table1, The result set will take the following form:

A B C
-------------------------
12345 Cupertino ca
83472 Redmond wa
83492 Boston MA

The followingCodeSegment is an example of executing an SQL statement. This SQL statement returns a set of rows, where column 1 is int, column 2 is string, and column 3 is a byte array:

Java. SQL. Statement stmt = conn. createstatement ();
Resultset r = stmt.exe cutequery ("select a, B, c from Table1 ");
While (R. Next ())
{
// Print the value of the current row.
Int I = R. getint ("");
String S = R. getstring ("B ");
Float F = R. getfloat ("C ");
System. Out. println ("ROW =" + I + "" + S + "" + F );
}

5.1.1 rows and cursors
Resultset maintains the cursor pointing to its current data row. Each time the next method is called, The cursor moves down a row. Initially it is located before the first line, so the first call next will place the cursor on the first line to make it the current line. With each call to next, the cursor moves down a row and obtains the resultset row in the order from top to bottom.

The cursor remains valid until the resultset object or its parent statement object is closed.

In SQL, the cursor of the result table has a name. If the database allows location update or location deletion, you need to provide the cursor name as a parameter to the update or deletion command. You can call the getcursorname method to obtain the cursor name.

Note: not all DBMS support location update and deletion. You can use the databasemetadata. supportspositioneddelete and supportspositionedupdate methods to check whether these operations are supported by a specific connection. When these operations are supported, DBMS/driverProgramMake sure that the selected row is properly locked so that location updates do not cause update exceptions or other concurrent issues.

5.1.2 Column
The getxxx method provides a way to obtain the value of a column in the current row. You can obtain column values in any order within each row. However, to ensure portability, you should obtain the column value from left to right and read the column value at a time.

The column name or column number can be used to identify the column from which data is to be obtained. For example, if the second column name of the resultset object RS is "title" and the value is stored as a string, any of the following code obtains the value stored in the column:

String S = Rs. getstring ("title ");
String S = Rs. getstring (2 );

Note that columns are numbered from left to right and start from column 1. The names of input columns used as the getxxx method are case insensitive.

The purpose of using the column name option is to allow users who specify the column name in the query to use the same name as the parameter of the getxxx method. On the other hand, if the SELECT statement does not specify a column name (for example, in "select * From Table1" or when the column is exported), the column number should be used. In these cases, you cannot know the column name exactly.

In some cases, multiple columns in the result set returned by an SQL query may have the same name. If the column name is used as a parameter of the getxxx method, getxxx returns the value of the first matching column name. Therefore, if multiple columns have the same name, you must use the column index to ensure that the correct column value is retrieved. In this case, the efficiency of using the column number is slightly higher.

You can call resultset. getmetadata to obtain the column information in resultset. The returned resultsetmetadata object provides the numbers, types, and attributes of each column of its resultset object.

If the column name is known but does not know its index, use findcolumn to obtain its column number.

5.1.3 data type and conversion
For the getxxx method, the JDBC driver attempts to convert the basic data to a specified Java type and then returns the appropriate Java value. For example, if the getxxx method is getstring and the data type in the basic database is varchar, The JDBC driver converts varchar to Java string. The return value of getstring is a Java String object.

The following table shows the JDBC Type that can be obtained using getxxx and the JDBC Type (Common SQL type) that is recommended to be obtained using getxxx ). The lower-case X indicates that the getxxx method is allowed to obtain the data type. The upper-case X indicates that the getxxx method is recommended for this data type. For example, any getxxx method except getbytes and getbinarystream can be used to obtain the longvarchar value. However, the getasciistream or getunicodestream method is recommended based on the returned data type. The GetObject method returns any data type as a Java object. It is useful when the basic data type is a database-specific abstract type or when a common application needs to accept any data type.

You can use the resultset. getxxx method to obtain common JDBC data types.

"X" indicates that the getxxx method can be used legally to obtain the specified JDBC Type.

"X" indicates that the getxxx method is recommended to obtain the specified JDBC Type.

T
I

Y
I

T S
M
A
L
L
I

T I

T
E
G
E
R B
I
G

N
T r
E
A
L f
L
O
A
T d
O
U

L
E d
E
C
I
M
A
L n
U
M
E
R
I
C B
I
T c
H
A
R v
A
R
C
H
A
R
L
O

G
V
A
R
C
H
A
R B
I

A
R
Y V
A
R

I

A
R
Y l
O

G
V
A
R

I

A
R
Y d
A
T
E t
I
M
E t
I
M
E

T
A
M

getbyte x
getshort x
getint x x
getlong x
getfloat x x
getdouble x
getbigdecimal x
getboolean x
getstring x
getbytes x
getdate x
gettime x
gettimestamp x
getasciistream x x
getunicodestream x
getbinarystream x
GetObject x x

5.1.4 use stream for very large row values
Resultset can obtain any large longvarbinary or longvarchar data. The getbytes and getstring Methods return the data to a large block (the maximum value is the return value of statement. getmaxfieldsize ). However, it may be easier to obtain very large data with a small fixed block, but this can be done by returning the java. Io. input stream to the resultset class. Data can be read in blocks from the stream. Note: You must access these streams immediately, because they will be automatically disabled the next time you call getxxx on the resultset (this is due to restrictions on access to large data blocks ).

The jdbc api has three methods for getting streams, with different return values:

Getbinarystream returns a stream that only provides the original database bytes without any conversion.

Getasciistream returns a stream that provides single-byte ASCII characters.

Getunicodestream returns a stream that provides double-byte Unicode characters.

Note: Unlike Java streams, the latter returns non-type bytes and can (for example) be used for ASCII and Unicode characters.

The following code demonstrates the usage of getasciistream:

Java. SQL. Statement stmt = con. createstatement ();
Resultset r = stmt.exe cutequery ("select X from Table2 ");
// Obtain the result of column 1 in 4 K blocks:
Byte buff = new byte [4096];
While (R. Next ()){
Java. Io. inputstream fin = R. getasciistream (1 );
For (;;){
Int size = fin. Read (buff );
If (size =-1) {// reach the end of the stream
Break;
}
// Send the newly filled buffer to the ASCII output stream:
Output. Write (buff, 0, size );
}
}

5.1.5 null result value
To determine whether the given result value is JDBC null, you must first read the column and then use resultset. wasnull to check whether JDBC null is returned for this read.

When the resultset. getxxx method is used to read JDBC null, The wasnull method returns one of the following values:

Java null value: For getxxx methods that return Java objects (such as getstring, getbigdecimal, getbytes, getdate, gettime, gettimestamp, getasciistream, getunicodestream, getbinarystream, and GetObject ).

Zero value: For getbyte, getshort, getint, getlong, getfloat, and getdouble.

False: For getboolean.

5.1.6 optional result set or multi-result set
Executequery (which returns a single resultset) or executeupdate (which can be used for any database modification statement and returns the number of updated rows) can be used to execute SQL statements. However, in some cases, the application does not know whether to return the result set before executing the statement. In addition, some stored procedures may return several different result sets and/or update counts.

To adapt to these situations, JDBC provides a mechanism that allows applications to execute statements and then process any set of result sets and update counts. The principle of this mechanism is to call a fully common execute method, and then call the other three methods, getresultset, getupdatecount, and getmoreresults. These methods allow the application to study statement results one by one and determine whether the given results are resultset or update count.

You do not have to disable the resultset. When the statement generated is closed, re-executed, or used to obtain the next result from the Multi-result sequence, the resultset is automatically disabled by the statement.

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.