JDBC Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.11

Connecting JdbcRowSet with JNDI Directory Service

This section describes how to connect a JdbcRowSet object to a database server using JNDI directory service to lookup DataSource name.

If a JdbcRowSet object is created without a Connection object, you can provide a DataSource name to the JdbcRowSet object so that it can use a JNDI directory service to lookup a DataSource object by name. The DataSource object will be then used to create a connection object. See the sample statements below:

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

// Set the DataSource name for JNDI lookup
  jrs.setDataSourceName("jdbc/mysqlDS");

// Set a SQL statement
  jrs.setCommand("SELECT 'Hello world!'");

// Lookup, connect and run the statement
  jrs.execute();

I wrote the following sample to connect a JdbcRowSet object using a JNDI directory service:

/**
 * MySqlRowSetDataSource.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
public class MySqlRowSetDataSource {
  public static void main(String [] args) {
    Connection con = null;
    try {

// Get a new JdbcRowSet object with Run's implementation
      javax.sql.rowset.JdbcRowSet jrs 
        = new com.sun.rowset.JdbcRowSetImpl();

// Set the DataSource name for JNDI lookup
      jrs.setDataSourceName("jdbc/mysqlDS");

// Set a SQL statement
      jrs.setCommand("SELECT 'Hello world!'");

// Connect and run the statement
      jrs.execute();

// Get the result
      jrs.next();
      System.out.println("Result: "+jrs.getString(1));
      
// Close resources
      jrs.close();
    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
      e.printStackTrace();
    }
  }
}

But I failed to set up the JNDI directory service. So I got an exception when running this program:

C:\>javac -cp .;\local\lib\rowset.jar MySqlRowSetDataSource.java

MySqlRowSetDataSource.java:13: warning: com.sun.rowset.JdbcRowSetImpl is Sun pro
prietary API and may be removed in a future release
        = new com.sun.rowset.JdbcRowSetImpl();
                            ^
1 warning

C:\>java -cp .;\local\lib\rowset.jar;
  \local\lib\mysql-connector-java-5.0.7-bin.jar 
  MySqlRowSetDataSource

Exception: JdbcRowSet (connect) JNDI unable to connect
java.sql.SQLException: JdbcRowSet (connect) JNDI unable to connect
  at com.sun.rowset.JdbcRowSetImpl.connect(JdbcRowSetImpl.java:634)
  at com.sun.rowset.JdbcRowSetImpl.prepare(JdbcRowSetImpl.java:654)
  at com.sun.rowset.JdbcRowSetImpl.execute(JdbcRowSetImpl.java:551)
  at MySqlRowSetDataSource.main(MySqlRowSetDataSource.java:22)

Sections in This Chapter

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

Dr. Herong Yang, updated in 2007
Connecting JdbcRowSet with JNDI Directory Service