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/OracleDS");

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

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

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

/* OracleRowSetDataSource.java
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
 */
import java.sql.*;
import javax.sql.rowset.RowSetProvider;
import javax.sql.rowset.JdbcRowSet;
public class OracleRowSetDataSource {
  public static void main(String [] args) {
    Connection con = null;
    try {

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

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

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

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

C:\herong>\Progra~1\java\jdk1.8.0_45\bin\java
   -cp .;\local\lib\ojdbc6.jar
   OracleRowSetDataSource

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:556)
   at OracleRowSetDataSource.main(OracleRowSetDataSource.java:25)

1 warning

Last update: 2015.

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 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

 Microsoft SQL Server 2005 Express Edition

 Microsoft JDBC Driver for SQL Server - sqljdbc42.jar

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

 Additional Tutorial Notes to Be Added

 Outdated Tutorials

 References

 PDF Printing Version