Derby - Connection with DataSource Objects

This section describes options of using DataSource objects to create database connections.

As I mentioned earlier, there are two different ways to create a connection object to access the database through a JDBC driver:

The JDBC documentation suggests several options on how to use DataSource classes provided by the JDBC driver:

1. Using JDBC driver's DataSource classes directly without using JNDI. Here is a sample code:

  AbcDataSource ds = new com.abc.jdbc.AbcDataSource();
  ds.setServerName("someHost");
  ds.setPortNumber("1527");
  ds.setDatabaseName("someDB");
  Connection con = ds.getConnection();

2. Using JDBC driver's DataSource classes indirectly through a JNDI directory service. One example is to use Sun's File System Service Provider as shown in the sample code:

  Hashtable env = new Hashtable();
  env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.fscontext.RefFSContextFactory");
  env.put(Context.PROVIDER_URL, "file:/temp");
  Context ctx = new InitialContext(env);

// Storing a DataSource object
  ClientDataSource dsIn
    = new org.apache.derby.jdbc.ClientDataSource();
  dsIn.setServerName("localhost");
  dsIn.setDatabaseName("TestDB");
  ctx.bind("DerbyTestDB", dsIn);

// Retrieving a DataSource object
  DataSource dsOut = (DataSource) ctx.lookup("DerbyTestDB");

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Derby (Java DB)

 Derby (Java DB) JDBC Driver

Derby (Java DB) JDBC DataSource Objects

Derby - Connection with DataSource Objects

 Derby - Using ClientDataSource Directly

 Installing JNDI File System Service Provider

 Derby - Storing ClientDataSource Objects on File System

 Derby - Looking Up ClientDataSource Objects on File System

 What Happens If Client JDBC DataSource JAR Is Missing?

 Derby (Java DB) - DML Statements

 Derby (Java DB) - ResultSet Objects of Queries

 Derby (Java DB) - PreparedStatement

 Summary of JDBC Drivers and Database Servers

 Using Connection Pool with JDBC

 Archived Tutorials

 References

 Full Version in PDF/EPUB