∟Connecting JdbcRowSet with a Predefined Connection Object
This section describes how to connect a JdbcRowSet object to a database server with a predefined connection object.
Another way to connect a JdbcRowSet object to a database server is to pass a predefined connection object to the JdbcRowSetImpl constructor.
Here is a sample sequence of method calls:
/**
* OracleRowSetConnectionObject.java
* Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
*/
import java.sql.*;
public class OracleRowSetConnectionObject {
public static void main(String [] args) {
Connection con = null;
try {
// Create a Connection object
Class.forName("oracle.jdbc.OracleDriver") ;
con = DriverManager.getConnection(
"jdbc:oracle:thin:Herong/TopSecret@//localhost:1521/XE");
// Pass the Connection object to the new JdbcRowSet object
javax.sql.rowset.JdbcRowSet jrs
= new com.sun.rowset.JdbcRowSetImpl(con);
// Set a SQL statement
jrs.setCommand("SELECT 'Hello world!' FROM DUAL");
// Connect and run the statement
jrs.execute();
// Get the result
jrs.next();
System.out.println("Result: "+jrs.getString(1));
// Close resources
jrs.close();
con.close();
} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
}
}
}
Output of compilation and execution was recorded here:
C:\>javac OracleRowSetConnectionObject.java
OracleRowSetConnectionObject.java:18:
warning: com.sun.rowset.JdbcRowSetImpl is Sun proprietary API
and may be removed in a future release
= new com.sun.rowset.JdbcRowSetImpl(con);
^
1 warning
C:\>java -cp .;\local\lib\ojdbc14.jar OracleRowSetConnectionObject
Result: Hello world!