Project Notes


Project: JDataStore: Hello JDBC sample
Author: JBuilder team
Company: borland.com
Description:The purpose of this sample is to demonstrate how to create a JDataStore file and connect to it using the JDataStore JDBC driver. JDataStore is the all Java database shipped with JBuilder Professional and Enterprise. A single JDBC driver supports both local and remote access to JDataStore files.

You can easily create a JDataStore file using the JDataStore Explorer, which you will find on the Tools menu. Select JDataStore Explorer from the tools menu now. From the JDataStore Explorer, select File | New. Enter a filename and click the "Install" checkbox next to TxManager, then click OK. You now have a JDataStore file that can be used with the JDataStore JDBC Driver.

There are two programs in this sample. Together, they demonstrate how to go about creating a JDBC application using JDataStore. The program "CreateDataStore.java", in this sample, demonstrates a fast programmatic way to create a JDataStore database. The other program in this sample, "HelloJDBC.java" demonstrates the basics of a JDBC application that accesses JDataStore.

Please take a look at CreateDataStore.java first. As you look at this program, there are a few important lines which you should note. First, the following line instantiates a new JDataStore:

DataStore store = new DataStore();

Second, a filename is assigned to the JDataStore with the line:

store.setFileName("TEMPORARY.jds");

A bit later, a default user name is assigned to the JDataStore.

store.setUserName("CreateTx");

Then a transaction manager is assigned to the JDataStore. Please note that a transaction manager is required when using the JDataStore JDBC driver.

store.setTxManager(new TxManager());

The JDataStore is then created using the DataStore.create() method. Also note that all of the JDataStore code is placed within a try block so that an exceptions which occur will be caught.

To write a JDBC application using JDataStore, you will need to supply two strings: a driver string and a URL for the JDataStore file. The driver string is: "com.borland.datastore.jdbc.DataStoreDriver" The same driver string is used for access to both the local and remote files.

URLs for local JDataStore files have this form: jdbc:borland:dslocal:(path to local file) for example: jdbc:borland:dslocal:c:\proj\data\mydb.jds

URLs for remote JDataStore files, ie. those accessed through the JDataStore server, have this form: jdbc:borland:dsremote://(host name)/(path to remote file)" for example: jdbc:borland:dsremote://localhost/temporary.jds"

In this sample, the program "HelloJDBC.java", shows how to connect to a JDataStore file through the JDBC driver. When you take a look at HelloJDBC.java, there are a few important lines you should note. The formatResultSet() method simply demonstrates one way to format the result set which is returned when you query a table in a JDataStore. We won't discuss this method much here, since none of the code in this method is essential to writing a JDBC application. What you do with your result sets in your own applications is entirely up to you.

All of the important lines of code which we want to point out are in the main() method of HelloJDBC.java. First, note the line that specifies the driver string:

String DRIVER = "com.borland.datastore.jdbc.DataStoreDriver";

Next, note the line that specifies the URL. In this case we are using the local driver:

String URL = "jdbc:borland:dslocal:";

The following two lines are also common to many JDBC applications:

Class.forName(DRIVER);
con = DriverManager.getConnection(URL + FILE, "user", "");

The rest of the program deals with creating a table in the JDataStore, inserting values into the table, querying the table, and deleting the table.

Take a look at code inside the last try block. It closes the connection even if exceptions were thrown earlier. This is important, because you may have trouble reopening a JDatastore file after leaving a connection to it open.

If you haven't done so already, please take a moment to look at CreateDataStore.java and HelloJDBC.java now.