Java Reference
In-Depth Information
Table 4-3: ResultSet getter Methods
Data Type
Method
boolean
getBoolean(String columnName)
byte
getByte(String columnName)
byte[]
getBytes(String columnName)
double
getDouble(String columnName)
float
getFloat(String columnName)
int
getInt(String columnName)
java.io.InputStream
getAsciiStream(String columnName)
java.io.InputStream
getUnicodeStream(String columnName)
java.io.InputStream
getBinaryStream(String columnName)
java.sql.Date
getDate(String columnName)
java.sql.Time
getTime(String columnName)
java.sql.Timestamp
getTimestamp(String columnName)
long
getLong(String columnName)
Object
getObject(String columnName)
short
getShort(String columnName)
String
getString(String columnName)
Data can be retrieved using either the column name or the column number. For
example, the previous example uses column names. However, you can get the same
results using column numbers, assuming you know what they are. Here's an
example:
ResultSet rs = stmt.executeQuery("SELECT First_Name, Last_Name FROM Customers");
while (rs.next()){
System.out.println(rs.getString(2)+•, •+rs.getString(1));
}
The option of using column names is provided so that if you specify column names in
a query, you can use the same names as the arguments to getter methods.
A problem that can arise using column names is that if you do a join on two tables, it
is possible for a SQL query to return a result set that has more than one column with
the same name. If you then use the column name as the parameter to a getter
method, it will return the value of the first matching column name.
Search WWH ::




Custom Search