Java .

Java ResultSet

java.sql.ResultSet

a ResultSet example

PreparedStatement preparedStatement =
  connection.prepareStatement(
    "Select favoritefood from favoritefoods where catname = ?");
preparedStatement.setString(1, "Cappuccino");
                
ResultSet resultSet = preparedStatement.executeQuery();
    //now run the SQL
    
while (resultSet.next()) {
    System.out.println("Cappuccino's favorite food is " 
      + resultSet.getString(1));  
}
  //traverse the ResultSet, 
  //The favorite foods are in the first column.
  //note: the subscript of the first column is one, not zero.
} catch(SQLException sqlException) {   
    System.out.println(
      "The following error occured in reading " + 
      "from the favoritefoods table: " 
      + sqlException);
}    

General ResultSet methods

resultSetName.close();
  //closes the result set, releases resources
    
int columnNumber 
  resultSetName.findColumn(stringColumnName);    
    //gives position of column specified in stringColumnName     
    
int concurrencyMode = 
  resultSetName.getConcurrency();
    //JDBC 2.0 and above
    //gets concurrency mode, 
    //  either CONCUR_READ_ONLY or CONCUR_UPDATABLE 
    
string stringCursorName = 
  resultSetName.getCursorName();  

int intFetchDirection = resultSetName.getFetchDirection(); //JDBC 2.0 and above

int intFetchSize = resultSetName.getFetchSize(); //JDBC 2.0 and above java.sql.ResultSetMetaData resultSetMetaData = resultSetName.getMetaData(); //gets MetaData for all of a ResultSets columns java.sql.PreparedStatement preparedStatementUsed = resultSetName.getStatement(); //JDBC 2.0 and above //gets the PreparedStatement used to get this ResultSet int typeOfResultSet = resultSetName.getType(); //JDBC 2.0 and above //gets ResultSet type, either TYPE_FORWARD_ONLY, // TYPE_SCROLL_INSENSITIVE, or TYPE_SCROLL_SENSITIVE int typeOfResultSet = resultSetName.refreshRow(); //JDBC 2.0 and above //refresh current row with current value in the data base

resultSetName.setFetchDirection(intOfDirection); //JDBC 2.0 and above //possible values for intOfDirection include // FETCH_FORWARD and FETCH_REVERSE

resultSetName.setFetchSize(intOfSizeToFetch); //JDBC 2.0 and above //number or rows to fetch from the database // into the ResultSet as rows are needed boolean wasLastColumnReadNull = resultSetName.wasNull(); //JDBC 2.0 and above //returns true if the last column read was null

ResultSet methods to find the cursor position

int cursorPosition = 
  resultSetName.getRow();
    //JDBC 2.0 and above
    //gets position of current row, 
    //  returns 0 if there is no current row
    
boolean isAfterLastRow = 
  resultSetName.isAfterLast();    
    //JDBC 2.0 and above
    //is the current position after the last row    
    
boolean isBeforeFirstRow = 
  resultSetName.isBeforeFirst();
    //JDBC 2.0 and above
    //is the current position before the first row
    
boolean isBeforeFirstRow = 
  resultSetName.isFirst();
    //JDBC 2.0 and above
    //is the current position the first row    
    
boolean isAfterLastRow = 
  resultSetName.isLast();    
    //JDBC 2.0 and above
    //is the current position the last row   

ResultSet methods to move the cursor position

resultSetName.absolute(intOfPosition);
    //JDBC 2.0 and above
    //move to position of row specified

resultSetName.afterLast(); //JDBC 2.0 and above //move to position after the last row

resultSetName.beforeFirst(); //JDBC 2.0 and above //move to position before the first row

resultSetName.first(); //JDBC 2.0 and above //move to position of the first row

resultSetName.last(); //JDBC 2.0 and above //move to position of the last row resultSetName.moveToCurrentRow(); //JDBC 2.0 and above //move to "remembered" position resultSetName.moveToInsertRow(); //JDBC 2.0 and above boolean wasNextSuccessful = resultSetName.next(); //moves cursor position one row boolean wasPrevoiusSuccessful = resultSetName.previous(); //JDBC 2.0 and above //moves cursor position one row previous boolean wasRelativeSuccessful = resultSetName.relative(intRowsToMove); //JDBC 2.0 and above //moves cursor position number of rows indicated, // can be positive or negative

ResultSet methods to get fields from current row

String[] stringArrayToGet = 
  resultSetName.getArray(intOfColumnToGet);
String[] stringArrayToGet = 
  resultSetName.getArray(stringColumnNameToGet);
    //an example getting an Array of Strings, 
    //  could use any Array    

java.math.BigDecimal bigDecimalToGet = resultSetName.getBigDecimal(intOfPositionToGet); java.math.BigDecimal bigDecimalToGet = resultSetName.getBigDecimal(stringColumnNameToGet);

java.sql.Blob blobToGet = resultSetName.getBlob(intOfPositionToGet); java.sql.Blob blobToGet = resultSetName.getBlob(stringColumnNameToGet); //must be using JDBC 2.0 or above

byte byteToGet = resultSetName.getByte(intOfPositionToGet); byte byteToGet = resultSetName.getByte(stringColumnNameToGet); byte[] byteArrayToGet = resultSetName.getBytes(intOfPositionToGet); byte[] byteArrayToGet = resultSetName.getBytes(stringColumnNameToGet);

java.sql.Clob clobToGet = resultSetName.getClob(intOfPositionToGet); java.sql.Clob clobToGet = resultSetName.getClob(stringColumnNameToGet); //must be using JDBC 2.0 or above

java.sql.Date dateToGet = resultSetName.getDate(intOfPositionToGet); java.sql.Date dateToGet = resultSetName.getDate(stringColumnNameToGet);

java.sql.Date dateToGet = resultSetName.getDate(intOfPositionToGet, calendarToUse); java.sql.Date dateToGet = resultSetName.getDate(stringColumnNameToGet, calendarToUse); //JDBC 2.0 and above //uses java.util.Calendar

double doubleToGet = resultSetName.getDouble(intOfPositionToGet); double doubleToGet = resultSetName.getDouble(stringColumnNameToGet);

float floatToGet = resultSetName.getFloat(intOfPositionToGet); float floatToGet = resultSetName.getFloat(stringColumnNameToGet); int intToGet = resultSetName.getInt(intOfPositionToGet); int intToGet = resultSetName.getInt(stringColumnNameToGet); long longToGet = resultSetName.getLong(intOfPositionToGet); long longToGet = resultSetName.getLong(stringColumnNameToGet);

java.lang.Object objectToGet = resultSetName.getObject(intOfPositionToGet); java.lang.Object objectToGet = resultSetName.getObject(stringColumnNameToGet) // java.lang.Object objectToGet = resultSetName.getObject(intOfPositionToGet, mapToUse); java.lang.Object objectToGet = resultSetName.getObject(stringColumnNameToGet, mapToUse); //mapToUse is a java.util.Map, // which matches SQL type names to java classes java.sql.Ref refToGet = resultSetName.getRef(intOfPositionToGet); java.sql.Ref refToGet = resultSetName.getRef(stringColumnNameToGet); //JDBC 2.0 and above //java.sql.Ref specifyies a SQL structured // type value in the database.

short shortToGet = resultSetName.getShort(intOfPositionToGet); short shortToGet = resultSetName.getShort(stringColumnNameToGet); String stringToGet = resultSetName.getString(intOfPositionToGet); String stringToGet = resultSetName.getString(stringColumnNameToGet); java.sql.Time timeToGet = resultSetName.getTime(intOfPositionToGet); java.sql.Time timeToGet = resultSetName.getTime(stringColumnNameToGet); // java.sql.Time timeToGet = resultSetName.getTime(intOfPositionToGet, calendarToUse); java.sql.Time timeToGet = resultSetName.getTime(stringColumnNameToGet, calendarToUse); //JDBC 2.0 and above //uses java.util.Calendar java.sql.Timestamp timestampToGet = resultSetName.getTimestamp(intOfPositionToGet); java.sql.Timestamp timestampToGet = resultSetName.getTimestamp(stringColumnNameToGet); // java.sql.Timestamp timestampToGet = resultSetName.getTimestamp(intOfPositionToGet, calendarToUse); java.sql.Timestamp timestampToGet = resultSetName.getTimestamp(stringColumnNameToGet, calendarToUse); //JDBC 2.0 and above //uses java.util.Calendar

ResultSet methods to get streams

java.io.InputStream asciiStreamToGet = 
  resultSetName.getAsciiStream(intOfPositionToGet);

java.io.InputStream asciiStreamToGet = resultSetName.getAsciiStream(stringColumnNameOfToGet);

java.io.InputStream binaryStreamToGet = resultSetName.setBinaryStream(intOfPositionToGet);

java.io.InputStream binaryStreamToGet = resultSetName.setBinaryStream(stringColumnNameOfToGet);

java.io.Reader characterStreamToGet = resultSetName.getCharacterStream(intOfPositionToGet); //must be using JDBC 2.0 or above java.io.Reader characterStreamToGet = resultSetName.getCharacterStream(stringColumnNameOfToGet); //must be using JDBC 2.0 or above

General ResultSet methods to change rows

resultSetName.cancelRowUpdates();
    //JDBC 2.0 and above
    //cancels updates to a row

resultSetName.deleteRow(); //JDBC 2.0 and above //deletes current row from result set and the database resultSetName.insertRow(); //JDBC 2.0 and above //inserts current row into the result set and the database boolean wasRowDeleted = resultSetName.rowDeleted(); //JDBC 2.0 and above //returns true if current row has been deleted boolean wasRowInserted = resultSetName.rowInserted(); //JDBC 2.0 and above //returns true if current row has been inserted boolean wasRowUpdated = resultSetName.rowUpdated(); //JDBC 2.0 and above //returns true if current row has been updated resultSetName.updateRow(); //JDBC 2.0 and above //updates current row in the database

ResultSet update methods

resultSetName.updateArray(
  intOfColumnToUpdate, arrayToUpdateWith);
resultSetName.updateArray(
  stringColumnNameToUpdate, arrayToUpdateWith);    

resultSetName.updateBigDecimal( intOfPositionToUpdate, bigDecimalToUpdateWith); resultSetName.updateBigDecimal( stringColumnNameToUpdate, bigDecimalToUpdateWith); //uses java.math.BigDecimal

resultSetName.updateBlob( intOfPositionToUpdate, blobToUpdateWith); resultSetName.updateBlob( stringColumnNameToUpdate, blobToUpdateWith); //must be using JDBC 2.0 or above //uses java.sql.Blob

resultSetName.updateByte( intOfPositionToUpdate, byteToUpdateWith); resultSetName.updateByte( stringColumnNameToUpdate, byteToUpdateWith); //uses primitive byte resultSetName.updateBytes( intOfPositionToUpdate, byteArrayToUpdateWith); resultSetName.updateBytes( stringColumnNameToUpdate, byteArrayToUpdateWith); //uses an Array of primitive byte resultSetName.updateClob( intOfPositionToUpdate, clobToUpdateWith); resultSetName.updateClob( stringColumnNameToUpdate, clobToUpdateWith); //must be using JDBC 2.0 or above //uses java.sql.Clob resultSetName.updateDate( intOfPositionToUpdate, dateToUpdateWith); resultSetName.updateDate( stringColumnNameToUpdate, dateToUpdateWith); //uses java.sql.Date resultSetName.updateDate( intOfPositionToUpdate, dateToUpdateWith, calendarToUse); resultSetName.updateDate( stringColumnNameToUpdate, dateToUpdateWith, calendarToUse); //JDBC 2.0 and above //uses java.sql.Date and java.util.Calendar

resultSetName.updateDouble( intOfPositionToUpdate, doubleToUpdateWith); resultSetName.updateDouble( stringColumnNameToUpdate, doubleToUpdateWith); //uses primitive double resultSetName.updateFloat( intOfPositionToUpdate, floatToUpdateWith); resultSetName.updateFloat( stringColumnNameToUpdate, floatToUpdateWith); //uses primitive float resultSetName.updateInt( intOfPositionToGet, intToUpdateWith); resultSetName.updateInt( stringColumnNameToGet, intToUpdateWith); //uses primitive int resultSetName.updateLong( intOfPositionToUpdate, longToUpdateWith); resultSetName.updateLong( stringColumnNameToUpdate, longToUpdateWith); //uses primitive long

resultSetName.updateObject( intOfPositionToUpdate, objectToUpdateWith); resultSetName.updateObject( stringColumnNameToUpdate, objectToUpdateWith) //uses java.lang.Object resultSetName.updateObject( intOfPositionToUpdate, objectToUpdateWith, mapToUse); resultSetName.updateObject( stringColumnNameToUpdate, objectToUpdateWith, mapToUse); //uses java.lang.Object //mapToUse is a java.util.Map, // which matches SQL type names to java classes resultSetName.updateRef( intOfPositionToUpdate, refToUpdateWith); resultSetName.updateRef( stringColumnNameToUpdate, refToUpdateWith); //JDBC 2.0 and above //java.sql.Ref specifyies a SQL structured type // value in the database.

resultSetName.updateShort(intOfPositionToUpdate, shortToUpdateWith); resultSetName.updateShort(stringColumnNameToUpdate, shortToUpdateWith); //uses primitive short resultSetName.updateString(intOfPositionToUpdate, stringToUpdateWith); resultSetName.updateString(stringColumnNameToUpdate, stringToUpdateWith); java.sql.Time timeToGet = resultSetName.updateTime( intOfPositionToGet, timeToUpdateWith); java.sql.Time timeToGet = resultSetName.updateTime( stringColumnNameToGet, timeToUpdateWith); //uses java.sql.Time java.sql.Time timeToGet = resultSetName.updateTime( intOfPositionToGet, timeToUpdateWith, calendarToUse); java.sql.Time timeToGet = resultSetName.updateTime( stringColumnNameToGet, timeToUpdateWith, calendarToUse); //JDBC 2.0 and above //uses java.sql.Time and java.util.Calendar

resultSetName.updateTimestamp( intOfPositionToGet, timestampToUpdateWith); resultSetName.updateTimestamp( stringColumnNameToGet, timestampToUpdateWith); //uses java.sql.Timestamp resultSetName.updateTimestamp( intOfPositionToGet, timestampToUpdateWith, calendarToUse); resultSetName.updateTimestamp( stringColumnNameToGet, timestampToUpdateWith, calendarToUse); //JDBC 2.0 and above //uses java.sql.Timestamp and java.util.Calendar

ResultSet update methods for streams

resultSetName.updateAsciiStream(
  intOfPositionToUpdate, asciiStreamToUpdate);
    //uses java.io.InputStream

resultSetName.updateAsciiStream( stringColumnNameOfToUpdate, asciiStreamToUpdate); //uses java.io.InputStream

resultSetName.updateBinaryStream( intOfPositionToUpdate, binaryStreamToUpdate); //uses java.io.InputStream resultSetName.updateBinaryStream( stringColumnNameOfToUpdate, binaryStreamToUpdate); //uses java.io.InputStream

resultSetName.getCharacterStream( intOfPositionToUpdate); //must be using JDBC 2.0 or above //uses java.io.Reader java.io.Reader characterStreamToGet = resultSetName.getCharacterStream(stringColumnNameOfToGet); //must be using JDBC 2.0 or above //uses java.io.Reader

ResultSet methods for warnings

                                
resultSetName.clearWarnings();
    //returns all java.sql.Warning for this resultSet

SQLWarning sqlWarning = resultSetName.getWarnings(); //returns the first java.sql.Warning

References


online
The Java 2 Platform Specification
Books
Thinking in Java by Bruce Eckel
Core Java (TM) 2 Volume 2 - Advanced Features by Cay Horstmann and Gary Cornell
Comments
Sign in to be the first to comment on Java ResultSet.