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