Original link: http://dev.9cbs.net/Develop/Article/35/Article/34/Article/34/Article/34/Article/35/35924.shtm from the previous study In the middle, we have mastered the SQL statement through the Statement class and its subclasses to access the database management system. In general, most of the operation of the database is a query statement. The result of this statement is to return a RESULTSET class. To return the results of the query to the user, you must do the ResultSet object. Today, we will learn how to process the results set.
According to the practice, let's take a look at an example:
Package com.rongji.deemo;
Import java.sql. *;
PUBLIC CLASS DATADEMO {
Public DataDemo () {
}
Public static void main (String [] args) {
Try {
Class.Forname ("Oracle.jdbc.driver.OracleDriver");
//establish connection
// Second step is to connect to DBMS with an appropriate driver, look at the following code [You can modify the database related information you are connected]:
String Url = "JDBC: Oracle: Thin: @ 192.168.4.45: 1521: Oemrep";
String Username = "ums";
String password = "rongji";
// Create a connection with URL
Connection Con = DriverManager.getConnection (URL, UserName, Password);
Statement Sta = con.createstatement ();
String SQL = "SELECT * from RBAC_Application";
ResultSet ResultSet = Sta.executeQuery (SQL);
While (resultSet.next ()) {
INT INT_VALUE = ResultSet.Getint (1);
String string_value = resultSet.getstring (2);
String a = resultset.getstring (3);
String b = resultSet.getstring (4);
// Net data from the database in two different ways.
System.out.println (int_value "" string_value " a " " " "
b);
// The search result is output on the user browser.
}
// Get the result set information
ResultSetMetadata ResultSetMD = ResultSet.getMetAdata ();
System.out.println ("ColumnCount:" ResultSetMd.getColumnCount ());
For (int i = 1; i System.out.println ("ColumnName:" ResultSetMd.getColumnName (i) " "ColumnTypename:" ResultSetMd.getColumnTypename (i)); System.out.println ("IsreadOnly:" ResultSetMd.isreadOnly (i) "Iswriteable:" ResultSetMd.iswritable (i) "Isnullable:" ResultSetMd.isnullable (i)); System.out.println ("TableName:" ResultSetMd.gettablename (i)); } //shut down C. close (); } Catch (Exception EX) { EX.PrintStackTrace (); } } } 1, the basic processing method of the ResultSet class A RESULTSET object corresponds to a table returned by the query statement, contains all the query results, in fact, we can see a resultset object as a table. The processing of the ResultSet object must be performed progressively, and each column in each row can be processed in any order. The ResultSet object maintains a pointer to the current row. Initially, this pointer points to the first line. The NEXT () method of the Result class makes this pointer down. Therefore, the first time the next () method will guide the pointer to the first line of the result set, and the data of the first row can be processed. After processing, use the next () method to move the pointer down and continue to process the second line of data. The return value of the next () method is a Boolean type value, which is true, and the description has a next record, and the pointer has successfully pointed to the record, and it can be processed; if the return value is false, then Explain that there is no record, the result set has been processed. When processing each line, each column can be processed in any order. However, the processing can be processed on each column from left to right to obtain higher execution efficiency. The getxxx () method of the ResultSet class can get the search result from a column. Where XXX is a Java data type in JDBC, such as int, string, date, etc., which is similar to the PreparedStateMent class and the CallableStatement class setting SQL statement parameter value. The Getxxx () API provided by Sun provides two ways to specify the column name for retrieval: one is an index of a column with an int value, and the other is to index as a column name as a column name. You can refer to the above example. 2, get information about the result set Under the premise of the table structure of the database already understand, you can know some of the columns of the columns in the column of the columns, such as column name data type, and more. Sometimes don't know the results of the results concentrated, you can use the GetMetadata method of the ResultSet class to get the information set. As with the example above: ResultSetMetadata ResultSetMD = ResultSet.getMetAdata (); GetMetadata () method returns an object of a RESULTSETMETADATA class, using this class, get a lot of information about the result set, and give several common methods below: (1) getColumnCount () Returns an int value and points to the number of columns in the result set. (2) GetTableName (int column) returns a string that points out the name of the table represented in the parameter. (3) GetColumnLabel (int Column) returns a String object that is the display title of the column referred to in Column. (4) GetColumnName (int Column) returns the name of the column in the database. The String object returned by this method can be used as a parameter of the getxxx () method of the ResultSet class. However, there is no great practical meaning. (5) getColumnType (int coMLUMN) Returns the SQL data type of the specified column. His return value is an INT value. There is a definition of various SQL data types in the Java.sql.Types class. (6) getColumnTypename (int coMLUMN) Returns the name of the data type of the specified column in the data source. His return value is a String object. (7) IsReadonly (int column) Returns a boolean value indicating whether the column is read-only. (8) IsWriteable (int column) Returns a boolean value indicating whether the column can be written. (9) Isnullable (int column) Returns a Boolean value indicating whether the column allows you to deposit a NULL value. JDBC Overview (5) 5 - ResultSet 5.1 Overview ResultSet contains all rows that meet the conditions in the SQL statement, and it provides data from these rows through a GET method (these GET methods can access different columns in the current row) access. The ResultSet.next method is used to move to the next row in the ResultSet, making the next row as the current line. The result set is generally a table, which has the column header and the corresponding value returned. For example, if the query is SELECT A, B, C from table1, the result set will have the following form: AB C ------------------------ 12345 CUPERTINO CA83472 Redmond WA83492 Boston MA The following code segment is an example of executing a SQL statement. The SQL statement will return a row collection, where column 1 is int, column 2 is String, and column 3 is byte arrays: java.sql.Statement Stmt = conn.createstatement (); resultset r = stmt.executeQuery ("SELECT) a, b, c from table1 "); while (r.next ()) {// prints the value of the current row. INT i = r.Getint ("a"); string s = r.getstring ("b"); float f = r.Getfloat ("c"); system.out.println ("row =" i " " S " " f);} 5.1.1 Rows and cursor ResultSet Maintenance points to the cursor of its current data line. Each time the next method is called, the cursor moves down. Initially, it was located before the first line, so the first call next time, the cursor will be placed on the first line, making it a current line. As the NEXT that causes the NEXT to cause the cursor to move down, obtain the Resultset line according to the order from the top. The cursor remains valid until the ResultSet object or its parent Statement object is turned off. In SQL, the cursor of the result table is named. If the database allows positioning updates or locating deletion, you need to provide the name of the cursor as a parameter to the update or delete command. The light scalar can be obtained by calling method getcursorname. Note: Not all DBMS supports positioning updates and deletion. You can use the DatabaseMetadata.supportsPositionedDelete and SupportSpositionedUpDate methods to check if specific connections support these operations. When these operations are supported, the DBMS / driver must ensure that the selected line is properly locked so that the positioning update does not cause the update of exceptions or other concurrency issues. 5.1.2 Column Method Getxx provides a way to get a column value in the current row. In each line, you can get column values in any order. However, in order to ensure portability, the column value should be obtained from left to right and read the column value at once. Column names or column numbers can be used to identify columns to get data from it. For example, if the second column name of the ResultSet object RS is "Title", then stores the value as a string, then the following code will get the value stored in the column: String s = rs.getstring ("Title") String s = rs.getstring (2); Note The column is numbered from left to right and starts from column 1. At the same time, the column name used as the input of the getxxx method is not case sensitive. The purpose of providing the list name is to allow users who specify the column name in the query to use the same name as the parameters of the getxxx method. On the other hand, if the SELECT statement does not specify a column name (eg, when it is exported in "select * from table1"), the column number should be used. In these cases, the household will not know the column name. In some cases, there may be multiple columns with the same name in the result set of SQL queries. If the column name is used as a parameter of the getxxx method, Getxxx will return the value of the first match column name. Thus, if multiple columns have the same name, you need to use the column index to ensure that the correct column value is retrieved. At this time, the use of the rating efficiency is slightly higher. About the information in the resultSet can be obtained by calling the method ResultSet.getMetadata. The returned ResultSetMetadata object will give the number, type, and properties of each column of its ResultSet object. If the column name is known, but do not know the index, you can use the method FINDCOLUMN to get its column number. 5.1.3 Data Types and Conversion For the Getxxx method, the JDBC driver attempts to convert basic data to specify the Java type and then return 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 will convert VARCHAR to Java String. GetString's return value will be a Java String object. The following table shows the JDBC type that allows GetXXX and the JDBC type (universal SQL type) that is recommended for getting it. The lowercase x represents the Getxxx method to get the data type; the upper-written X indicates that the Getxxx method is recommended for this data type. For example, in addition to any getXXX method other than getBytes and getBinaryStream can be used to get the longvarChar value, but it is recommended to use the Getasciistream or the getUnicODestream method based on the returned data type. Method GetObject returns any data type to Java Object. It is very useful when the basic data type is an abstract type specific to a database or when a universal application needs to accept any data types. You can use the ResultSet.Getxxx method to get a common JDBC data type. "X" indicates that the getxxx method can be legally used to obtain a given JDBC type. "X" means that it is recommended to use the Getxxx method to get a given JDBC type. getByte X xxxxxxxxxxxx getShort x X xxxxxxxxxxx getInt xx X xxxxxxxxxx getLong xxx X xxxxxxxxx getFloat xxxx X xxxxxxxx getDouble xxxxx XX xxxxxx getBigDecimal xxxxxxx XX xxxx getBoolean xxxxxxxxx X xxx getString xxxxxxxxxx XX xxxxxx xgetBytes XX x getDate xxx X xgetTime xxx X xgetTimestamp xxx x XgetAsciiStream xx X xxx getUnicodeStream xx X xxx getBinaryStream xx X getObject xxxxxxxxxxxxxxxxxx x5.1.4 using very large flow ResultSet row values may be obtained in any large or LONGVARCHAR LONGVARBINARY data. Methods getBytes and getString return data to large blocks (maximum return value for Statement.getMaxFieldSize). However, it may be more convenient to get very large data with a smaller fixed block, and this can be done by returning the ResultSet class to java.Io.input. The data can be read from the stream. Note: These streams must be accessed immediately because they will automatically turn off when the result is called getxxx next time (this is due to the basic implementation of large block data access). The JDBC API has three ways to get flow, each with different return values: GetBINARYSTREAM returns only the stream that only provides database origin without any conversion. GetasciistReam returns a stream that provides a single-byte ASCII character. GetUnicODestream returns a stream that provides a double-byte Unicode character. Note: It is different from the Java stream, and the latter returns non-type bytes and can be (for example, universal in ASCII and Unicode characters.