Connecting JdbcRowSet with a Connection URL

This section describes how to connect a JdbcRowSet object to a database server with a connection URL.

If a JdbcRowSet object is created without a connection to the database server, you can provide a connection URL string by calling the setUrl() method. A connection object will be created later by the JdbcRowSet object with DriverManager when you call the execute() method. Here is a sample sequence of method calls:

// Creating a JdbcRowSet object without connection
  JdbcRowSet jrs = new JdbcRowSetImpl();

// Provide a connection URL for future use
  jrs.setUrl("connection_url");

// Provide a SQL statement for future use
  jrs.setCommand("SQL_statement");

// Create a connnection using DriverManager
// Execute the SQL statement
  jrs.execute();

Note that the name of method setUrl() is very confusing with another method setURL(), which sets a URL value to a parameter.

Note also that the connection URL and user information can also be provided through another constructor:

  JdbcRowSet jrs = new JdbcRowSetImpl("connection_url",
    "user", "password");

To test connection method, I wrote the following tutorial program:

/* OracleRowSetConnectionUrl.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.sql.*;
import javax.sql.rowset.RowSetProvider;
import javax.sql.rowset.JdbcRowSet;
public class OracleRowSetConnectionUrl {
  public static void main(String [] args) {
    try {

// Not needed anymore with JDK 1.8
//      Class.forName("oracle.jdbc.OracleDriver") ;

// RowSetFactory should be used now
//      JdbcRowSet jrs
//        = new com.sun.rowset.JdbcRowSetImpl();
      JdbcRowSet jrs
        = RowSetProvider.newFactory().createJdbcRowSet();

// Set the connection URL for the DriverManager
      jrs.setUrl(
        "jdbc:oracle:thin:Herong/TopSecret@//localhost:1521/XE");

// 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 resource
      jrs.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Make sure the Oracle server is running on the local machine, then run this program. The result with JDK 1.8, Oracle 11.2 and ojdbc6.jar confirms that the connection is working.

herong> java -cp .;ojdbc11.jar OracleRowSetConnectionUrl

Result: Hello world!

But if you are JDK 1.6, Oracle 11.2 and ojdbc14.jar and use "new com.sun.rowset.JdbcRowSetImpl()" explicitly, you will get a compilation warning:

herong> Progra~1\java\jdk1.6.0_2\bin\javac \
   -cp .;\local\lib\ojdbc14.jar \
   OracleRowSetConnectionUrl.java

OracleRowSetConnectionUrl.java:15:
warning: com.sun.rowset.JdbcRowSetImpl is Sun proprietary API
and may be removed in a future release
        = new com.sun.rowset.JdbcRowSetImpl();
                           ^
1 warning

herong> Progra~1\java\jdk1.6.0_2\bin\java \
   -cp .;14.jar OracleRowSetConnectionUrl

Result: Hello world!

Also notice that class com.sun.rowset.JdbcRowSetImpl seems to be included in JDK 1.6 package, because I executed the program successfully without including rowset.jar file in the classpath.

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

Oracle - Reference Implementation of JdbcRowSet

 Overview of RowSet Objects

 Installation of JdbcRowSet Reference Implementation

 Connecting JdbcRowSet to Database Servers

Connecting JdbcRowSet with a Connection URL

 Connecting JdbcRowSet with a Predefined Connection Object

 Connecting JdbcRowSet with a Predefined ResultSet Object

 Connecting JdbcRowSet with JNDI Directory Service

 JdbcRowSet Query Statement with Parameters

 Inserting Rows with JdbcRowSet Objects

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Using Connection Pool with JDBC

 Archived Tutorials

 References

 Full Version in PDF/EPUB