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:

/* DerbyDataSource.java
 * Copyright (c) HerongYang.com. 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();
      ds.setServerName("localhost");
      ds.setPortNumber(1527);
      ds.setDatabaseName("TestDB");

// 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.next()) {
         System.out.println(
           "  "+res.getInt("ID")
           + ", "+res.getString("StreetName")
           + ", "+res.getString("City"));
      }
      res.close();
      sta.close();

      con.close();
    } 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 DerbyDataSource.java
herong> java -cp $DERBY_HOME/lib/derbyclient.jar DerbyDataSource.java

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