Connecting to a database using JDBC

The Database component handles the JDBC connection to a SQL server and is required for all database applications involving server data. JDBC is the JavaSoft(tm) Database Application Programmer Interface, a library of components and classes developed by JavaSoft to access remote data sources. The components are collected in the java.sql package and represent a generic, low-level SQL database access framework. For more information about JDBC, visit the JavaSoft Database Access Web page at http://splash.javasoft.com/jdbc/.

JBuilder uses the JDBC API to access the information stored in databases. Many of JBuilder's data-access components and classes use the JDBC API. Therefore, these classes must be properly installed in order to use the JBuilder database connectivity components. (See Installing JBuilder, JDBC, and the JDBC-ODBC Bridge.)

In addition, to connect your database application to a remote server, you need an appropriate JDBC driver. Drivers can be grouped into two main categories: drivers implemented using native methods that bridge to existing database access libraries or all-Java based drivers. Drivers that are not all-Java must run on the client (local) system. All-Java based drivers can be loaded from the server or locally. The advantages to using a driver entirely written in Java is that it can be downloaded as part of an applet and is cross-platform.

This tutorial assumes you are familiar with the JBuilder design tools. For more information on these tools, see in the JBuilder's visual design tools in the User Guide.

This tutorial outlines

Note: When you no longer need a Database connection, you should explicitly call the Database.closeConnection() method in your application. This ensures that Database classes which hold references to JDBC connections automatically close the connection when the Database object is garbage collected.

Adding a Database component to your application

To add the Database component to your application,

  1. Create a new project and application files using the Project and Application Wizards. (You can optionally follow this tutorial to add data connectivity to an existing project and application.)

  2. Open the Designer by highlighting the Frame file and selecting the Design tab at the bottom of the AppBrowser.

  3. Click the Database component from the Data Access tab of the Component Palette.

  4. Click anywhere on the Component Tree window to add the Database component to your application. This adds the following line of code to the Frame class:

    Database database1 = new Database();

Setting Database connection properties

For the Database object to be usable, you need to set its connection properties. The ConnectionDescriptor object holds the values to these properties. You can access this object programmatically or set connection properties through the user interface by the following steps:
  1. Select the Database object in the Component Tree. Click beside the connection property in the Inspector window. Click the ellipsis button that appears to open the connection property editor.

  2. Set the following properties:
    PropertyDescription

    ConnectionURL The Universal Resource Locator (URL) of the database, for example, jdbc:odbc:DataSet Tutorial.
    Username The user name authorized to access the server database, for example, SYSDBA.
    Password The password for the authorized user, for example, masterkey.
    Prompt user for password Whether to prompt the user for a password each time.
    Driver Class The class name of the JDBC driver that corresponds to the URL, for example, sun.jdbc.odbc.JdbcOdbcDriver

    Tip: The connection dialog includes a Test Connection button. Click this button to check that the connection properties have been correctly set. Results of the connection attempt display directly beneath the Test Connection button.

    Tip: The connection dialog includes a Choose URL button. Click this button to select from a list of recently-accessed URLs.

What to do next?

Now that your application includes the Database component, you'll want to use a component that needs it. JBuilder uses queries and stored procedures to return a set of data. The components implemented for this purpose are QueryDataSet and ProcedureDataSet. These components work with the Database component to access the SQL server database. For tutorials on how to use the QueryDataSet component, see: If you work with a complex data model or need to change data components in your application, consider encapsulating the Database and other Data Access components in a DataModule instead of directly adding them to an application's Frame. For more information on using the JBCL DataModule, see Encapsulating your data model for reuse.