Lesson 4 | More methods for accessing data |
Objective | Describe how to use other ResultSet methods to manipulate data. |
Methods for accessing Data(JDBC)
The examples you have seen so far have not demonstrated much flexibility in how JDBC supports working with the data in a database. This lesson will look at how you can work with the data retrieved in a ResultSet
.
"Getter" methods
"Getter" methods are provided by the
ResultSet
interface that allow you to refer to the columns of data in a row.
These "getter" methods take the form
get
XXX(argument) where
XXX is a particular Java data type. The "getter" methods allow you to do several of the following:
- Use the index number of a particular column or use the column name as the argument to the method.
- Use column index referencing since they are usually more efficient and little translation needed.
- Address column numbers that are numbered sequentially beginning with 1. Column names used as arguments are not case sensitive.
- Address two columns with the same name, though the data from the first name-matched column will be returned.
When the driver gets these requests it will attempt to return the appropriate column's data converted to the specified Java type. For example, rs.getInt
('integerData'
) would return the data stored in the integerData
column as a Java Int and, if integerData
was the first column in the row, rs.getInt(1)
would retrieve the same data. (Assume 'rs'
is a valid ResultSet
.)
Driver responsibilities
SQL data types are not precisely the same as Java data types. Java doesn't have a data type to directly represent an unsigned char value, to name one example. The responsibility for translating
these types appropriately is given to the driver. However, the mapping is not done arbitrarily. There is a table in the JDBC
specification that shows the allowed mappings between SQL data types and Java data types. See the full table in the SDK docs set
for the JDBC specification. Currently, the page documenting this is in
"DOC_ROOT/docs/guide/jdbc/spec/jdbc-spec.frame.html", where DOC_ROOT is either the local root directory location for your Oracle JDK.
If you attempt to retrieve data that doesn't map to the data type specified in the getXXX
method you use, you will
get an error. If, for example, you refer to a non-integer type in the getInt()
example above, the driver will issue a complaint.
update Methods
The JDBC 2.0 specification also provided for a set of updateXXX
methods for the ResultSet
interface. These methods take the same sorts of arguments as the getXXX
methods. They, of course, assume you have an updateable ResultSet
.
The updateXXX
methods can be used to change column values in a row or they can be used to insert a row in a table. In the first case, if you need to change the name of a hospital stored in our Hospital Project database, you might create an updateable ResultSet
, position the cursor to the row that needs to be changed, invoke the appropriate method to change the hospital name, and then call the method to update that row.
The following Slide Show explains what the code might look like.
JDBC Methods Code
In the second case, you need to use a special row provided by an updateable ResultSet
. There is an insert row available and you use that to create the new row. You update the defined columns in the row and then invoke the insertRow() method on the ResultSet
to put the record into the database. In the next lesson, you will examine methods that provide information about the database.