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

Derby - Connection with DataSource Objects

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

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

  • Using DrirverManager Class: DriverManager.getConnection(connection_url) - The driver manager passes the connection URL to all loaded JDBC drivers, hoping that one of them will recognize the URL and creates a connection.
  • Using DataSource Object: ds.getConnection() - The JDBC driver provides DataSource classes, which can be initialized directly or indirectly through JNDI (Java Naming and Directory Interface). A connection can be then created from the DataSource object.

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

 Downloading and Installing JDK - Java SE

 Installing and Running Java DB - Derby

 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?

 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

 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 - sqljdbc.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

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2007
Derby - Connection with DataSource Objects