Create a Data Access Object for Btrieve Data using JDBI

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Btrieve JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Pervasive SQL (Btrieve) databases.



A brief overview of creating a SQL Object API for Btrieve 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 Btrieve integrates connectivity to live Btrieve data in Java applications. By pairing these technologies, you gain simple, programmatic access to Btrieve data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write Btrieve data.

Create a DAO for the Btrieve Billing 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 MyBillingDAO { //insert new data into Btrieve @SqlUpdate("INSERT INTO Billing (Student_ID, Transaction_Number) values (:student_ID, :transaction_Number)") void insert(@Bind("student_ID") String student_ID, @Bind("transaction_Number") String transaction_Number); //request specific data from Btrieve (String type is used for simplicity) @SqlQuery("SELECT Transaction_Number FROM Billing WHERE Student_ID = :student_ID") String findTransaction_NumberByStudent_ID(@Bind("student_ID") String student_ID); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Btrieve

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

The PSQL v13 client will need to be installed on the same machine as the driver. To connect, set the User, Password, Server, and Database properties.

Built-in Connection String Designer

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

java -jar cdata.jdbc.btrieve.jar

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

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

jdbc:btrieve:User=myuser;Password=mypassword;Server=myserver;Database=mydatabase;

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:btrieve:User=myuser;Password=mypassword;Server=myserver;Database=mydatabase;"); MyBillingDAO dao = dbi.open(MyBillingDAO.class); //do stuff with the DAO dao.close();

Read Btrieve Data

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

//disply the result of our 'find' method String transaction_Number = dao.findTransaction_NumberByStudent_ID("22"); System.out.println(transaction_Number);

Write Btrieve Data

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

//add a new entry to the Billing entity dao.insert(newStudent_ID, newTransaction_Number);

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