ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Configuring Database Access in Eclipse 3.0 with SQLExplorer

by Deepak Vohra

SQLExplorer is an Eclipse IDE database plugin that may be used to connect to a database from Eclipse. The SQLExplorer plugin adds a graphical user interface (GUI) to access a database with SQL. With SQLExplorer, you can display the tables, table structure, and data in the tables, and retrieve, add, update, or delete table data. SQLExplorer can also generate SQL scripts to create and query tables. Thus, using SQLExplorer may be preferable to using a command-line SQL client. In this tutorial, we shall establish a JDBC connection with the open source MySQL database from Eclipse 3.0 with the SQLExplorer plugin.

This tutorial has the following sections:

  1. Overview
  2. Preliminary Setup
  3. Configuring SQLExplorer
  4. Accessing Database Data


The SQLExplorer plugin configures Eclipse for SQL client access to a database, by adding an SQLExplorer "perspective" to the IDE.

To demonstrate the SQLExplorer plugin, we shall create an example table in the open source MySQL database and establish a JDBC connection to the MySQL database from the Eclipse IDE. Next, we shall retrieve and display the example data in the SQLExplorer GUI SQL client. We shall also update and delete the example table data to demonstrate the different features of the SQLExplorer plugin.

Preliminary Setup

  1. Download and install Eclipse 3.02.
  2. Download the SQLExplorer file net.sourceforge.sqlexplorer_2.2.3.zip.
  3. Extract the SQLExplorer .zip file to the <Eclipse>/eclipse directory. <Eclipse> is the directory in which Eclipse is installed. This adds the features and plugins directories of the SQLExplorer plugin to the features and plugins directories in Eclipse.
  4. Restart the Eclipse IDE.
  5. Install the MySQL database.
  6. Download Connector/J, which is used to connect to the MySQL database via a JDBC driver contained in a .jar file.
  7. Log in to the MySQL database with as root with the command:
    >mysql -u root

    A password is not required for the root user. To log in to the database with a password, specify the command:

    >mysql -u root -p
  8. Connect to the test database, an example database instance, with the command:
    >use test
  9. Create an example table in the test database. The example table, Catalog, is composed of ONJava articles. The SQL script to create the example table is listed below:

    CREATE TABLE Catalog(CatalogId INTEGER, Journal VARCHAR(25),
     Publisher Varchar(25), 
     Date VARCHAR(25), Title Varchar(45), Author Varchar(25));
    INSERT INTO Catalog VALUES('1', 'onjava',  
    'OReilly', 'April 2005', 'Five Favorite Features from 5.0', 
    'David Flanagan');
    INSERT INTO Catalog VALUES('2', 'onjava',    
    'OReilly', 'Feb 2005', 'Introducing JBoss Remoting', 
    'John Mazzitelli');
    INSERT INTO Catalog VALUES('3', 'onjava',   
     'OReilly', 'March 2005', 'Aspect-Oriented Annotations', 
    'Bill Burke');

Configuring SQLExplorer

Having installed the SQLExplorer plugin, we shall configure the SQLExplorer plugin in the Eclipse 3.01 IDE. First, set the SQLExplorer perspective in the Eclipse IDE. Click on the "Open a perspective" button in the Eclipse IDE to open a perspective. Figure 1 illustrates the "Open a perspective" button.

Opening a Perspective
Figure 1. Opening a perspective

In the item list, select "Other..." to display the SQLExplorer plugin as shown in Figure 2.

The 'Other...' Perspective Menu Item
Figure 2. The "Other..." perspective menu item

In the Select Perspective frame, select the SQLExplorer perspective, as shown in Figure 3. By selecting the SQLExplorer perspective, the SQLExplorer plugin features become available in the Eclipse IDE.

SQLExplorer Perspective
Figure 3. SQLExplorer perspective

Selecting the SQLExplorer perspective displays the features of the SQLExplorer plugin in Eclipse. The Drivers tab displays the different database drivers that may be used to connect to different databases. The available databases include DB2, MySQL, Oracle, Sybase, HSQLDB, SQL Server, and PostgreSQL. We shall configure the SQLExplorer with the MySQL database. To configure the MySQL driver, right-click on the MMMySQL Driver node and select Change the Selected Driver, as illustrated in Figure 4.

Modifying the Driver
Figure 4. Modifying the Driver

In the Modify Driver frame, select the Extra Class Path tab and click on the Add button to add the MySQL driver .jar file (which you downloaded as part of Connector/J) to the classpath. Figure 5 illustrates adding the MySQL JDBC driver to the Eclipse classpath.

Setting the Driver
Figure 5. Setting the driver

Add the MySQL Connector/J driver .jar file, mysql-connector-java-3.0.16-ga-bin.jar, to the classpath. In the Example URL field, specify the connection URL to connect to the database. A JDBC connection will be created with the test database, which is preconfigured in the MySQL install. The connection URL for the test database is jdbc:mysql://localhost/test. In the Driver Class Name field, specify the MYSQL JDBC driver as com.mysql.jdbc.Driver. The MMMySQL driver gets configured with the settings shown in Figure 6.

Setting the MySQL JDBC settings
Figure 6. Setting the MySQL JDBC settings

A connection alias is required to connect to the MySQL database and retrieve the database tables. A connection alias specifies the connection settings; JDBC driver, URL, username, and password. Select the Aliases tab in the SQLExplorer perspective. Click on the "Create new Alias" button to create a new Alias, as shown in Figure 7.

Create a New Alias
Figure 7. Create a new alias

In the "Create new Alias" frame, specify an alias name. Select the MMMySQL Driver to create a alias for the MySQL database. Specify the connection URL for the MySQL database test, jdbc:mysql://localhost/test, in the URL field. Figure 8 shows the MySQL alias settings.

Creating a New Alias
Figure 8. Creating a new alias

This adds a MySQL alias to the Aliases tab frame, which is illustrated in Figure 9. To modify an alias, right-click on the alias node and select "Change the selected Alias."

MySQL Alias
Figure 9. MySQL alias

The MySQL connection alias connects to the MySQL database and retrieves the database data. To connect to the database, right-click on the MySQL alias node and select Open, as shown in Figure 10.

Opening an Alias
Figure 10. Opening an alias

In the Connection frame, specify the User name and Password to log in to the MySQL database, and click on the OK button. Figure 11 shows the login settings. By default, a password is not required for the root user.

Opening a Connection
Figure 11. Opening a connection

A JDBC connection gets established with the MySQL database. Once connected, Eclipse displays the different database schemas in the MySQL database, as illustrated in Figure 12.

Listing the Database Schemas
Figure 12. Listing the database schemas

Pages: 1, 2

Next Pagearrow