Connecting to Databases

This section explains the basics of creating a connection to a database in the IDE that you can then use in your project.

This section covers the following topics:

Once you have set up the connection to your database, you can perform the following simple operations that are related to JDBC-compliant databases:

You can use the database connection you create when developing projects in the IDE. You can also use the IDE to help you set up a database connection pool and register the resources with the Sun Java System Application Server to use in your application.

Setting Up Your Resources

You use Java? Database Connectivity (JDBC?) technology to connect to a database. The JDBC application programming interface (API) is Sun Microsystems' API for connecting to databases that support Structured Query Language (SQL). The JDBC API is a package of object-oriented objects that includes Connection , ResultSet , and Statement . Each object contains various API methods, for example, connect() , close() , and prepareStatement() . You will use these objects and methods later in this section. To do so, you need to use a database that supports SQL.

A database runs in a database server. You can use the database server bundled with the Sun Java System Application Server or any JDBC-compliant SQL server. Once you have your server resources, you need to make them available to the IDE and your project.

Bundled databases

If your IDE is bundled with the Sun Java System Application Server, or you have installed and registered the Sun Java System Application Server with the IDE, you already have one of the following databases servers. The database servers include several sample databases.

Server is ready to accept connections on port 1527.
Starting Server C:\Program Files\Java\jdk1.5.0_01\bin\java
Server started, listening on port 9092, display level: 0 ...

Other databases

You are not limited to working with the Pointbase or Derby databases. For the IDE to communicate with a database server, the IDE requires a driver supporting the JDBC API ("JDBC driver"), which translates JDBC calls into the network protocols that are used by SQL databases. To work with other databases, you need to install the database server and the JDBC driver.

Connecting to Databases

You use the Databases node in the Runtime window when creating a connection to a database. If the Runtime window is not open, you can open it by choosing Window > Runtime in the main menu (Ctrl-5). Expand the Databases node to see the available drivers and database connections.

The Drivers node in the Runtime window displays the drivers available to the IDE. Depending on your system configuration, you may already have Derby or Pointbase database drivers available. If the driver for your database server is not listed, you need to add the driver before you can work with your database in the IDE and your projects.

Icon

Description

 

The driver can be loaded by the IDE and you can connect to the database.

 

The IDE cannot connect to the database using this driver because the driver's JAR or ZIP file isn't placed in the specified location. You can correct the location by right-clicking the driver node and choosing Customize.

Table showing database driver icons

You create a database connection in the Runtime window using the driver for your database server. Once you have created a database connection, you can connect to and modify the database and use the database in your application.

Adding Database Drivers

To add a database driver, right-click the Drivers node and choose New Driver from the pop-up menu. The New JDBC Driver dialog box appears. Click Add and browse to the location of your database driver and select the database driver's JAR or ZIP file. When you select the driver, the Driver Class and Name appear in the dialog box.

If the Driver Class field is empty or incorrect, click Find. The IDE searches the JAR file that you selected and finds all classes that implement the JDBC API Driver interface ( java.sql.Driver ). Select the correct driver class from the Driver Class drop-down list and click OK. After adding the new driver, when you expand the Drivers node a new node is displayed for the new driver. The nodes under the Drivers node represent the registered drivers you can use.

Note that when you have made your database driver available to the IDE, you still need to make it available to your project. At this stage, you can use the IDE to access and modify your database, but you cannot access and modify your database from your project. To access your database from your project, you need to create a database connection that your project can use.

Establishing a Database Connection

In order to establish a connection to a database, first make sure your database server is up and running. If you are using one of the database servers bundled with the IDE, you can start the database by choosing the Derby or Pointbase database from the Tools menu and then choosing Start. When the database server is ready, a message indicating the server has started appears in the Output window.

Before you can open a connection to a database on your database server, you first need to supply the connection details for the connection. You only need to supply the details to create a connection once, and after setting up the initial connection you can easily use the database connection node in the Runtime window to connect to and disconnect from a database.

To create a database connection, right-click the driver node and choose Connect Using to open the New Database Connection dialog box.

New Database Connection dialog box

Specify the Database URL and the username and password for your database server. For example, if you are connecting to the database sample on your local installation of the bundled Derby database, your Database URL will look like this:

jdbc:derby://localhost:1527/sample

Click OK after you enter the required connection details. When you click OK, two things happen:

Working with a Database Connection

Each database connection node in the Runtime window represents a connection to a database. You need to set up a separate connection to each database on your database server that you want to use. You can use the database connection node to do the following:

Databases node in the Runtime window showing drivers and database connections

When connected to a database, you can expand the database connection node to view the structure of the database. By right-clicking nodes under the database connection node you can access commands enabling you to modify the structure of the database, including creating, populating and deleting tables and columns. You can also easily view the data in the tables and columns by right-clicking the table or column and choosing View Data from the pop-up menu.

Setting Up a Database Connection Pool

A database connection pool is a group of reusable connections that a server maintains for a particular database. Applications requesting a connection to a database obtain that connection from the pool. When an application closes a connection, the connection is returned to the pool. Connection pool properties may vary with different database vendors. Some common properties are the URL for the database name, user name, and password.

When setting up a database connection pool, you also create a JDBC resource (also called a data source). A JDBC resource provides applications with a connection to a database. Typically, there is at least one JDBC resource for each database accessed by an application. It is possible to have more than one JDBC resource for a database.

To create a connection pool, you need to check that you have the following:

If you are deploying your application to the Sun Java System Application Server, the IDE enables you to easily set up a database connection pool and the required data sources using the New File wizard. You can create JDBC resources and connection pools for your application by opening the New File wizard in the IDE and choosing the Sun Resources category and then selecting the type of resource you want to create. You can use the New File wizard to do the following:

When you use the New File wizard to set up your connection pool, the IDE generates the necessary files based on the specified connection. The resources are registered with the Sun Java System Application Server when you deploy your application to the server.

If you are deploying your application to a server other than the Sun Java System Application Server, you need to set up your resources by editing the source file for the resource.


Previous - TOC - Next


Copyright and Trademark Notice