Ready to get started?

Download a free trial of the Oracle Driver to get started:

 Download Now

Learn more:

Oracle Icon Oracle JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Oracle databases.

Create a Data Access Object for Oracle Data using JDBI



A brief overview of creating a SQL Object API for Oracle data in JDBI.

JDBI is a SQL convenience library for Java that exposes two different style APIs, a fluent style and a SQL object style. The CData JDBC Driver for Oracle integrates connectivity to live Oracle data in Java applications. By pairing these technologies, you gain simple, programmatic access to Oracle data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write Oracle data.

Create a DAO for the Oracle Customers Entity

The interface below declares the desired behavior for the SQL object to create a single method for each SQL statement to be implemented.

public interface MyCustomersDAO { //insert new data into Oracle @SqlUpdate("INSERT INTO Customers (Country, City) values (:country, :city)") void insert(@Bind("country") String country, @Bind("city") String city); //request specific data from Oracle (String type is used for simplicity) @SqlQuery("SELECT City FROM Customers WHERE Country = :country") String findCityByCountry(@Bind("country") String country); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Oracle

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Oracle.

To connect to Oracle, you'll first need to update your PATH variable and ensure it contains a folder location that includes the native DLLs. The native DLLs can be found in the lib folder inside the installation directory. Once you've done this, set the following to connect:

  • Port: The port used to connect to the server hosting the Oracle database.
  • User: The user Id provided for authentication with the Oracle database.
  • Password: The password provided for authentication with the Oracle database.
  • Service Name: The service name of the Oracle database.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the Oracle JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.oracleoci.jar

Fill in the connection properties and copy the connection string to the clipboard.

A connection string for Oracle will typically look like the following:

jdbc:oracleoci:User=myuser;Password=mypassword;Server=localhost;Port=1521;

Use the configured JDBC URL to obtain an instance of the DAO interface. The particular method shown below will open a handle bound to the instance, so the instance needs to be closed explicitly to release the handle and the bound JDBC connection.

DBI dbi = new DBI("jdbc:oracleoci:User=myuser;Password=mypassword;Server=localhost;Port=1521;"); MyCustomersDAO dao = dbi.open(MyCustomersDAO.class); //do stuff with the DAO dao.close();

Read Oracle Data

With the connection open to Oracle, simply call the previously defined method to retrieve data from the Customers entity in Oracle.

//disply the result of our 'find' method String city = dao.findCityByCountry("US"); System.out.println(city);

Write Oracle Data

It is also simple to write data to Oracle, using the previously defined method.

//add a new entry to the Customers entity dao.insert(newCountry, newCity);

Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Oracle by integrating with the CData JDBC Driver for Oracle. Download a free trial and work with live Oracle data in custom Java applications today.