JDBC foundation (four)

xiaoxiao2021-03-06  18

As the final part of the basic knowledge, let's take the results of the results set, of course, the processing of general result sets. As for the multi-result set returned by the stored procedure, we are still introduced in advanced applications.

How does the SQL statement execute a query action, then return a resultSet object, if you want to display the query results to the user, you must process the resultSet. Resultset returned to a table in a table, for ResultSet Processing progressive progressive, and for the processing of each row, you can press any order (note that this is just the requirements of the JDBC specification, and some JDBC implementations are still required to handle the user in sequence, but this is a very small number Since in fact, although you can press any order when you process the column, you can get high performance if you press from left to right.

From the bottom floor, you will not get such knowledge from the bottom, in any introduction to the JDBC, because it is a database manufacturer's thing. Resultset object is actually maintained a two-dimensional pointer, the first dimension is pointing When the current line, the initial point to the first line of the result set, so if you want to access the first line, you must first first (), you must first first () to access, then the second dimension pointer points Columns, as long as you go to rs.getxxx (column), remove the real data from the database, otherwise no machine can really put the data you want to take in memory.

So, I have to remember that if the Connection has been closed, it is impossible to get data from the resultset. There are a lot of people asking me, I can't write it to the session and turn off the connection, so Don't join each time. I can only tell you, your thoughts are very good, but it is wrong! Of course, Cacherow and WebCacherow can cook down the result set in Javax.sql package, but that And we open a data structure to put all the values ​​in the row of ResultSets, and there is no different.

Access the column in the row, you can access by field name or index. Below is a simple search result:

ResultSet RS = Stmt.executeQuery (/ "SELECT A1, A2, A3 from Table /");

While (rs.next ()) {

INT i = rs.getinT (1);

String a = rs.getstring (/ "a2 /");

............

}

For the result set used to display, use when using the while () is the most common, if next () returns false, the description does not have the available line. But sometimes we may not even have a line, and if there is a record I don't know how much, then if you have to do different processing with records and no records, you should use the following process to determine:

IF (rs.next ()) {

// Because it has first () first (), the record should be handled by do {} while ();

Do {

INT i = rs.getinT (1);

String a = rs.getstring (/ "a2 /");

} while (rs.next ());

}

ESLE {

System.out.println (/ "does not have a record! /");

}

Type conversion:

The resultSet's getXXX method will strive to convert the SQL data type in the result set to Java data type. The fact that most types can be converted, but there are still many confused can't be converted. If you can't convert a SQL float to Java's Date, you can't vARCHAR / "we /" to convert into Java int.

Larger value:

For the value of GetMaxFieldSize returned in Statement, ordinary getBytes () or getString () is not read, so that Java provides a method of reading the input and waves, for large objects, we can pass rs.getxxxStream () To get an InputStream, XXX type includes ASCII, BINAY, Unicode. Based on the type of stream type, in general, binary files with getBinayStream (), text file with getasciistryReam (), for Unicode characters Text files use getUnicodestream (), corresponding database field types: blob, clob, and nlob. Get information collection information:

In most cases, programmers understand the database structure, you can know the situation of each column in the result, but sometimes don't know what columns in the result set, what type is. At this time, you can get the result set. :

ResulsetMetadata RSMD = r.getMetadata ();

RSMD.GetColumnCount () Returns the number of columns.

GetColumnLabel (int) Returns the display title of the column corresponding to the INT

getColumnName (int) Returns the name of the column corresponding to the INT in the database.

GetColumnType (int) returns the data type in the database corresponding to the INT.

getColumnTypename (int) Returns the name of the data type of the column corresponding to the INT in the data source.

IsReadOnly (int) returns to whether the column corresponding to the INT is read only.

Isnullable (int) Returns whether the column corresponding to this int is empty

转载请注明原文地址:https://www.9cbs.com/read-56392.html

New Post(0)