Jdbc-resultset Object Details

Source: Internet
Author: User

ResultSet Object
Function: When the executed statement is a query statement, the ResultSet object is used to encapsulate the query results.

Method:
Boolean next () This method lets the pointer (cursor) in the result set Move down one line. and determine if the row has data. There is a return of true, no return false
String getString (int cloumncount) obtains a string type of data from the currently pointed line. Based on the index where the column is located.
String getString (String columnName) obtains a string type of data from the currently pointed line. Based on the name of the column.
There are many kinds of methods for GetXXX series, and none of them are for different types in the database.
How does the type root GetXXX method in the database correspond?
The Get method corresponding to the database type
-------------------------------------------------
Char/varchar getString
int GetInt
bigint Getlong
Float/double getfloat/getdouble
Datetime/timestamp getDate

--------------------------------------------------------------------------------------------------------------- ---

 Packagecn.itcast.d_rs;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement;Importorg.junit.Test;//ResultSet Details//Features: Encapsulating result set data//action: How to get (take out) the result//Conclusion://1. Next method, move down and determine if there is any content//2. GetXXX method to get the contents of a column based on column index or column name Public classDemo {@Test Public voidFUN1 ()throwsexception{//1 Registration DriveClass.forName ("Com.mysql.jdbc.Driver"); //2 getting connectedConnection conn =Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/day05", "root", "1234"); //3 Creating statementStatement st =conn.createstatement (); //4 Writing SQLString sql = "SELECT * FROM T_user" ; //5 Execute SQLResultSet rs =st.executequery (SQL); //Move down one line, and Judge         while(Rs.next ()) {//have data//fetch data: GetXXX            intid = rs.getint (1);//get the value of the first column//int ID rs.getint ("id");//get the value of the ID columnString name = rs.getstring (2);//get the value of the second column            intAge = Rs.getint (3);//get the value of the third columnSystem.out.println (id+ "==>" +name+ "==>" +Age ); }                //6 Closing ResourcesSt.close ();    Conn.close (); }    /*database type Java type int int double double decimal do        uble char string varchar string datetime Date timestamp Timestamp/date*/}



8.ResultSet (Learn content)
Result set scrolling;
Scrolling refers to the position of the pointer can not only downward, but also arbitrary control.
The methods involved are as follows:
A Boolean absolute (int row) moves the pointer to the specified position. The parameter is the position. The position of the first row is 1. If a negative number is filled in, the last line, for example -1=>. If the move is out of range, it will return false.
void Afterlast () moves the cursor to the end of this ResultSet object, just after the last line. (The row has no data)
void Beforefirst () moves the cursor to the beginning of this ResultSet object, just before the first line. (initial position of result)
Boolean first () moves the cursor to the top row
Boolean last () moves the cursor to the final line
The Boolean next () cursor moves down one line.
Boolean previous () next moves in the opposite direction. Move up one line.
//--------------------------------------------------------
Use resultset to modify records.
By default, resultset cannot reverse-Modify records in the database. When you create a statement object, you create a statement that produces a ResultSet object that can modify the data by specifying parameters
Statement createstatement (int resultsettype, int resultsetconcurrency)
Parameter 1 ResultsetType-result set type
Resultset.type_forward_only, does not support the result set scrolling, can only forward.
Resultset.type_scroll_insensitive supports scrolling, insensitive, insensitive result sets.
Resultset.type_scroll_sensitive supports scrolling, sensitive result sets.
Parameter 2 resultSetConcurrency-whether the result supports modification type
Resultset.concur_read_only does not support modification
Resultset.concur_updatable Support Modification

You can reverse-modify the data in the database using the following code:
String sql = "SELECT * from EMP";
Statement state = Conn.createstatement (resultset.type_forward_only,resultset.concur_updatable);

ResultSet rs = state.executequery (SQL);

Rs.next ();

Rs.updatestring ("ename", "haha");

Rs.updaterow ();

Conclusion: Do not use ResultSet to do the modified operation. Really want to make changes we want to do with the handwritten UPDATE statement.

 Packagecn.itcast.d_rs;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement;Importorg.junit.Test;//ResultSet Details//1. The scroll of result set and the pointer to move the result set is the scroll//2. Result set reverse modifying database Public classDemo2 {@Test Public voidFUN1 ()throwsexception{//1 Registration DriveClass.forName ("Com.mysql.jdbc.Driver"); //2 getting connectedConnection conn = drivermanager.getconnection ("jdbc:mysql://localhost:3306/day05", "root", "1234"); //3 Creating statementStatement st =conn.createstatement (); //4 Writing SQLString sql = "SELECT * FROM T_user" ; //5 Execute SQLResultSet rs =st.executequery (SQL); //traverse backwards.//1> After the cursor moves to the last lineRs.afterlast (); //2> traverse = =             while(Rs.previous ()) {//move the cursor up and determine if there is data                intid = rs.getint ("id");//get the value of the ID columnString name = rs.getstring ("name");//get the value of the second column                intAge = Rs.getint ("Age");//get the value of the third columnSystem.out.println (id+ "==>" +name+ "==>" +Age ); }        //6 Closing ResourcesSt.close ();    Conn.close (); }    /*database type Java type int int double double decimal do        uble char string varchar string datetime Date timestamp Timestamp/date*/}

Jdbc-resultset Object Details

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.