Using JDBC to Create Database Objects
By James W. Cooper2003-05-24
The ResultSet
The ResultSet object is the most important single object in JDBC. It is essentially an abstraction of a table of general width and unknown length. Nearly all methods and queries return data as a ResultSet. A ResultSet contains any number of named columns that you can ask for by name. It also consists of one or many rows, which you can move through sequentially from top to bottom one at a time. Before you can use a ResultSet, you need to ask how many columns it contains. This information is stored in the ResultSetMetaData object.
|
When you obtain a ResultSet, it points just before the first row. You use the next() method to obtain each additional row, and the method returns false when no more rows remain. Since fetching data from a database may result in errors, you must always enclose your result set manipulations in a try block.
|
You can fetch data in a ResultSet in many forms, depending on the data type stored in each column. Further, you can obtain the contents of a column either by column number or by column name. Note that column numbers start at 1, not at 0. Some of the more common methods for the ResultSet object are shown below.
getInt(int); | Returns contents of the column numbered int as an integer. |
getInt(String); | Returns contents of the column named String as an integer. |
getFloat(int); | Return contents of the column numbered int as a float.. |
getFloat(String); | Returns contents of the column named String as a float. |
getDate(int); | Return contents of the column numbered int as a date. |
getDate(String); | Return contents of the column named String as a date. |
next(); | Moves the row pointer to the next row. Returns false if no rows remain. |
close(); | Closes the result set. |
getMetaData(); | Returns the ResultSetMetaData object. |
Tutorial Pages:
» What is a Database?
» Getting Data out of Databases
» Kinds of Databases
» ODBC
» What Is JDBC?
» Installing and Using JDBC
» Types of JDBC Drivers
» Two-Tier and Three-Tier Models
» Writing JDBC Code to Access Databases
» Registering Your Database with ODBC
» Connecting to a Database
» Accessing the Database
» The ResultSet
» ResultSetMetaData
» DatabaseMetaData
» Getting Information on Tables
» Executing SQL Queries, Printing out ResultSets, A Simple JDBC Program
» Building Higher Level JDBC Objects
» Building a Database Object, A Visual Database Program
» Executing a Query
» The Query Result Dialog
» Example Files
» Summary
First published by IBM DeveloperWorks
