Java--JDBC learning--performing query operations with ResultSet

Source: Internet
Author: User

ResultSet: Result set. Encapsulates the results of querying using JDBC.
1. Call the Statement object's executeQuery (SQL) to get the result set.
2. The ResultSet returned is actually a data table. There is a pointer to the front of the first sample of the data table. You can call the next () method to detect if the next row is valid. If valid, the method returns true and the pointer moves down. Equivalent to the combination of the Hasnext () and Next () methods of the Iterator object.
3. When the pointer is bit-to-row, you can get the value of each column by calling GETXXX (index) or getXxx (ColumnName). For example: GetInt (1), getString ("name").
4. ResultSet, of course, also needs to be closed.

Example:

 Public voidTestresultset () {//gets the records of the id=4 customers data sheet and printsConnection Conn=NULL; Statement Statement=NULL; ResultSet RS=NULL; Try {            //1. Get Connectionconn =jdbctools.getconnection ();                        SYSTEM.OUT.PRINTLN (conn); //2. Get Statementstatement =conn.createstatement ();                        SYSTEM.OUT.PRINTLN (statement); //3. Prepare SQLString sql = "SELECT ID, name, email, birth" + "from Customers"; //4. Execute the query and get ResultSetrs =statement.executequery (SQL);                        System.out.println (RS); //5. Handling ResultSet             while(Rs.next ()) {intid = rs.getint (1); String name= rs.getstring ("name"); String Email= Rs.getstring (3); Date Birth= Rs.getdate (4);                SYSTEM.OUT.PRINTLN (ID);                SYSTEM.OUT.PRINTLN (name);                SYSTEM.OUT.PRINTLN (email);            System.out.println (birth); }                    } Catch(Exception e) {e.printstacktrace (); } finally{            //6. Close the database resource.Jdbctools.release (RS, statement, Conn); }            }

Jdbctools.java See http://www.cnblogs.com/androidsuperman/p/6938569.html

Java--JDBC learning--performing query operations with ResultSet

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.