JDBC tutorial
JDBC quick start tutorial for Java programmers
This tutorial helps Java programmers use the JDBC API to interact with FairCom servers.
Note
The <root>
directory for this driver is <faircom>\drivers\sql.jdbc\
Install and start up the FairCom server if it is not currently running.
Optionally read the FairCom JDBC API introduction.
Get started immediately with the source code found in the
<faircom>\drivers\sql.jdbc\tutorials
directory, or proceed with the following instructions.
The Java Database Connectivity (JDBC) API is the industry standard for database-independent connectivity between the Java programming language and the FairCom SQL Database Engine. The JDBC API provides a call-level API for FairCom SQL-based database access, which then allows you to use the Java programming language to exploit "Write Once, Run Anywhere" capabilities for applications that require access to enterprise data. An application written in the Java programming language alleviates the challenge of writing different applications to run on different platforms. For a closer look see the JDBC Developers Guide.
Beginning with FairCom DB V11.2 and FairCom RTG V2, the connection string is in the following format:
jdbc:ctree://<host>[:portnumber]/<dbname>[?param=value[¶m=value]...]
The valid values for param are:
characterEncoding - Replace encoding with a valid Java encoding name (e.g., US‑ASCII, ISO‑8859-1, UTF‑8, etc.).
password
User
ssl - Values of basic (no peer certificate authentication) or peerAuthentication (server certificate authentication)
When peerAuthentication is requested, the client’s trust store must contain the server’s certificate as shown in the example below.
The tutorials use a connection string that is set for the default configuration:
"jdbc:ctree://localhost:6597/ctreeSQL", "ADMIN", "ADMIN"
TLS/SSL Examples
Connection c = getConnection("jdbc:ctree://localhost:6597/ctreeSQL?ssl=basic"); System.setProperty("javax.net.ssl.trustStore","TrustStore.key"); System.setProperty("javax.net.ssl.trustStorePassword","mypassword"""); Connection c = getConnection("jdbc:ctree://localhost:6597/ctreeSQL?ssl=peerAuthentication");
For backward compatibility, the older format ("jdbc:ctree:6597@localhost:ctreeSQL", "ADMIN", "ADMIN"
) is still supported but should be considered deprecated.
You must compile the tutorial programs to run on Windows or Linux.
Compile and run the JDBC SQL tutorials from the Microsoft Windows command prompt.
If the FairCom Database Engine is not already running on your machine, start the server.
Run
set
from the command prompt to review and set your current settings.The PATH should be set to include the path to your Oracle JDK by using a command something like this, but referencing the proper JDK on your machine:
set JDKDIR="C:\Program Files\Java\jdk1.7.0_07"
set PATH=%PATH%;%JDKDIR%\bin;%JDKDIR%\jre\bin;
The CLASSPATH should be set for your JDK and for the FairCom SQL JDBC Driver by using a command something like this, but referencing the proper JDK on your machine:
set CLASSPATH="C:\Program Files\Java\jdk1.7.0_07"\jre\lib\rt.jar;
The CLASSPATH should also include the c-treeEDGE SQL JDK JAR file and the proper “tutorials” folder found in your <faircom> installation directory by using a command like this but referencing your c-treeEDGE installation directory.
set CLASSPATH=%CLASSPATH%;C:\FairCom\V*\win32\lib\sql.jdbc\ctreeJDBC.jar;<faircom>\tools\guitools.java\lib\ctreeJDBC.jar;<faircom>\drivers\sql.jdbc\tutorials
Compile and run the tutorial samples using a batch file or from the command-line .
Batch file:
Use the
<faircom>\drivers\sql.jdbc\tutorials\cmdline\BuildTutorials.bat
file to automatically build and then run the tutorials. The executable files with debugging information are placed in the<faircom>\drivers\sql.jdbc\tutorials
directory.Command-line:
Compile the tutorials from the command-line:
javac JDBC_Tutorial1.java javac JDBC_Tutorial2.java javac JDBC_Tutorial3.java javac JDBC_Tutorial4.java
Run the compiled tutorials from the command-line:
java JDBC_Tutorial1 java JDBC_Tutorial2 java JDBC_Tutorial3 java JDBC_Tutorial4
To fully observe JDBC in action, you can single-step through the code with a debugger (jdb):
jdb JDBC_Tutorial1 jdb JDBC_Tutorial2 jdb JDBC_Tutorial3 jdb JDBC_Tutorial4
The output from the first tutorial project should appear similar to the following:
INIT Logon to server... DEFINE Open table... Add fields... Create table... MANAGE Delete records... Add records... Display records... 1000 Bryan Williams 1001 Michael Jordan 1002 Joshua Brown 1003 Keyon Dooling DONE Close table... Logout... Press <ENTER> key to exit . . .
Troubleshooting
The examples below illustrate some of the errors that occur when these environment variables are not set properly. You may need to adjust the path to match your installation.
The most common cause of this error is that the FairCom Database Engine is not running. Remember that the FairCom evaluation license times out after 3 hours, so the server might have exited and needs to be re-started. See Start the FairCom server.
In this example we do not have the JDKDIR and PATH environment variable set properly, so it cannot find the Java JDK<faircom>\drivers\sql.jdbc\tutorials\cmdline>BuildTutorials.bat
In this example we do not have the Java environment variables set properly in our PATH. <faircom>\drivers\sql.jdbc\tutorials>javac JDBC_Tutorial1.java
INIT Exception: ctree.jdbc.ctreeDriver *** Execution aborted *** Press <ENTER> key to exit...
In this example we do not have the CLASSPATH pointing to the FairCom SQL JDBC Driver directory. <faircom>\drivers\sql.jdbc\tutorials>java JDBC_Tutorial1
In this example we attempt to execute a tutorial with the .class extension. Simply call the sample without this extension. Another source of this error is leaving the “drivers\sql.jdbc\tutorials” folder out of the CLASSPATH. <faircom>\drivers\sql.jdbc\tutorials>java JDBC_Tutorial1.class
This section explains the steps to compile and run the JDBC SQL tutorials from a Linux shell.
If the FairCom Database Engine is not already running on your machine, start the server.
Compiling
To compile the tutorials from the Linux command line, follow these steps:
CD into the proper folder:
drivers/sql.jdbc/tutorials/cmdline
Execute make:
>make
If make works, it prints:
The jdbc tutorials are ready to run.
Type 'make run' to run the tutorials.
Running
Be sure the FairCom Database Engine is running.
Run the tutorials from the Linux command line. Follow these steps from the same folder you compiled from:
Type the following:
>make run
The makefile runs each tutorial one-at-a-time.
Press <Enter> when prompted after each tutorial is run.
Output
The output of the first tutorial project should appear similar to the following:
INIT Logon to server... DEFINE Open table... Add fields... Create table... MANAGE Delete records... Add records... Display records... 1000 Bryan Williams 1001 Michael Jordan 1002 Joshua Brown 1003 Keyon Dooling DONE Close table... Logout... Press <ENTER> key to exit . . .
Compiling and Running the Tutorials “by Hand”
To compile and run the tutorials individually, without using the batch file, execute the following from the shell (in the parent folder - <faircom>/drivers/sql.jdbc/tutorials
):
javac JDBC_Tutorial1.java javac JDBC_Tutorial2.java javac JDBC_Tutorial3.java javac JDBC_Tutorial4.java
To execute the tutorials without using the batch file, run them as follows:
java -cp .:../ctreeJDBC.jar JDBC_Tutorial1 java -cp .:../ctreeJDBC.jar JDBC_Tutorial2 java -cp .:../ctreeJDBC.jar JDBC_Tutorial3 java -cp .:../ctreeJDBC.jar JDBC_Tutorial4
Troubleshooting
This section lists some of the errors that can occur when compiling and running these tutorials.
SQL Exception: 26003 - Connection refused (Connection refused)
The most common cause of this runtime error is that the FairCom Database Engine is not running on your machine. Remember that the FairCom evaluation license times out after 3 hours, so the server might have exited and you need to start the FairCom server.
This quick start has four programs to demonstrate how to create tables and indexes, insert, update, delete, and lock records, query across multiple tables, and process multiple operations as a single atomic transaction.
The source code can be found in <faircom>\drivers\sql.jdbc\tutorials\
Note
These tutorials create tables and indexes outside a database as independent data files in the <faircom>\data
directory. Thus, they are invisible in SQL, the JSON DB API, and the FairCom Data Explorer application. In addition, these tutorials do not delete these files and records when they finish running. These files include custmast.dat
, custmast.idx
, custordr.dat
, custordr.idx
, invent.dat
, invent.idx
, itemmast.dat
, itemmast.idx
, ordritem.dat
, and ordritem.idx
.
<faircom>\drivers\sql.jdbc\tutorials\JDBC_Tutorial1.java
This tutorial shows the basic use of the FairCom JDBC Interface. It creates a single customer table and inserts records into it. It then reads, displays, and deletes the records.
It organizes the work into four functions:
Initialize()
Connects to the FairCom Database Engine.
Define()
Creates a customer master table named custmast.
Manage()
Adds records.
Reads and displays the records.
Deletes records.
Done()
Disconnects from FairCom Database Engine.
<faircom>\drivers\sql.jdbc\tutorials\JDBC_Tutorial2.java
This tutorial expands the number of tables. It defines key columns/fields and creates specific indexes for each table to form a relational model database. It creates a simple order system with customer, order, order detail, and product tables then inserts records in the tables and runs a query that lists each customer's name and the total amount of each order.
It organizes the work into four functions:
Initialize()
Connects to the FairCom Database Engine.
Define()
Creates "custmast", "custordr", "ordritem" and the "itemmast" tables with related indexes.
Manage()
Adds records to each of these tables.
Lists each customer's name and the total amount of each order.
Done()
Disconnects from FairCom Database Engine.
<faircom>\drivers\sql.jdbc\tutorials\JDBC_Tutorial3.java
This tutorial shows how to lock records so that other concurrent processes cannot interfere with them. It begins by deleting preexisting records in the customer table. It then inserts new records and pauses after it places a lock on a record. You can then run another instance of this tutorial, which will automatically block while waiting for the first instance to release its lock. Press Enter in the first instance to remove the lock and allow the second instance to continue.
It organizes the work into four functions:
Initialize()
Connects to the FairCom Database Engine.
Define()
Creates a custmast table with a primary index.
Manage()
Deletes preexisting records.
Adds records.
Reads and displays the records.
Creates a session-wide write lock that locks all subsequently accessed records to prevent them from being modified.
Finds a record and updates it. It automatically locks the record.
Pauses until the user presses ENTER. When the user has pressed ENTER, it releases the session lock.
Reads and displays the records.
Done()
Disconnects from FairCom Database Engine.
Note
When you run a second instance of this tutorial, it immediately begins deleting all preexisting records. When the delete operation encounters the record locked by the first instance, the delete operation automatically blocks and will not return until the lock is released in the first instance. Once you release the lock in the first instance by pressing ENTER, the second instance deletes the record and continues its process.
<faircom>\drivers\sql.jdbc\tutorials\JDBC_Tutorial4.java
A transaction treats multiple operations as if they were a single atomic operation. In an atomic transaction, either all operations succeed or none succeed. You can start a transaction and execute multiple operations. You can then commit the transaction or roll it back.
This tutorial shows you how to process transactions in a simple order system with customer, order, order detail, and product tables. It shows you how to use transactions in a variety of use cases.
It organizes the work into four functions:
Initialize()
Connects to the FairCom Database Engine.
Define()
Creates "custmast", "custordr", "ordritem" and the "itemmast" tables with related indexes.
Manage()
In a transaction, it deletes existing records and inserts new records in the custmast and itemmast tables.
In a transaction, it deletes existing records in the custordr and ordritem detail tables.
In a loop that inserts multiple records, it uses transactions and transaction savepoints to commit good records and roll back bad records that it inserts in the custordr and ordritem tables. Because of bad data, only 2 of 3 orders and 3 of 6 ordered items are inserted.
Reads and displays the newly inserted records in the custordr table.
Reads and displays the newly inserted records in the ordritem table.
Done()
Disconnects from FairCom Database Engine.