Java Reference
In-Depth Information
With just these four methods you have enough information to display the results of any query in a
meaningful way.
Using SELECT to return RecordSets with JDBC
The procedure for retrieving data from a database is very similar to the procedure you used to insert
data, with the exception that, since this is a query, you need to define a ResultSet to hold the
returned data. In addition to the ResultSet , you are also defining a ResultSetMetaData object,
which will hold information about the ResultSet . You will use this object to get the number of
columns returned, since the getData method does not have any information regarding the query it is
executing.
For the purposes of the example in Listing 7-1 , you will simply loop through the ResultSet and print
the data to the system console.
Listing 7-1: Data Retrieval using JDBC
package java_databases.part2;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import sun.jdbc.odbc.JdbcOdbcDriver;
public class DataRetriever{
static String jdbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
static String dbName = "Contacts";
static String urlRoot = "jdbc:odbc:";
private ActionListener exceptionListener = null;
public DataRetriever(){
registerDriver();
}
public void setDatabaseName(String dbName){
this.dbName=dbName;
}
public void registerDriver(){
try {
Search WWH ::




Custom Search