Derby - Using ClientDataSource Directly

This section describes how to use ClientDataSource class to connect to Derby Network Server without JNDI.

Derby JDBC driver offers several DataSource classes. One of them is the org.apache.derby.client.BasicClientDataSource class, which can be used to create a connection to a Derby Network Server. BasicClientDataSource class can be used directly to create a connection without using JNDI (Java Naming Directory Interface) as shown in the following sample program:

 * Copyright (c) All Rights Reserved.
import java.sql.*;
import javax.sql.*;
// import org.apache.derby.jdbc.*;
import org.apache.derby.client.*;
public class DerbyDataSource {
  public static void main(String [] args) {
    Connection con = null;
    try {

// Setting up the DataSource object
      // ClientDataSource ds
      // = new org.apache.derby.jdbc.ClientDataSource();
      BasicClientDataSource ds
        = new org.apache.derby.client.BasicClientDataSource();

// Getting a connection object
      con = ds.getConnection();

// Running a query
      Statement sta = con.createStatement();
      ResultSet res = sta.executeQuery(
        "SELECT * FROM HY_Address");
      System.out.println("List of Addresses: ");
      while ( {
           "  "+res.getInt("ID")
           + ", "+res.getString("StreetName")
           + ", "+res.getString("City"));

    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());

The output I got from this program confirms that ClientDataSource class can be used directly without JNDI.

herong> java -cp %DERBY_HOME%\lib\derbyclient.jar
herong> java -cp $DERBY_HOME/lib/derbyclient.jar

List of Addresses:
  1, 5 Baker Road, Bellevue
  2, 25 Bay St., Hull
  3, 251 Main St., W. York

Note that Derby JDBC driver has change its DataSource class names in newer Derby versions. For example, in Derby 10.11 One of the DataSource classes is org.apache.derby.jdbc.ClientDataSource class, which no longer supported in Derby 10.15. This is an issue of using the DataSource class. You need to change your source code to use the new DataSource class, if it is changed.

See next tutorial on ways to avoid this issue.

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

 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

 Summary of JDBC Drivers and Database Servers

 Using Connection Pool with JDBC

 Archived Tutorials


 Full Version in PDF/EPUB