Connecting to a Database
Connecting to a database using the Database Explorer is a two step process. First, configure the Database Explorer driver for your database, and then open a database connection. Driver configuration is a one-time configuration task for each database you want to use. In this tutorial, you will learn to:
- Configure a JDBC database driver
- Create a database profile and connection
This feature is available in MyEclipse.
1. Configure the Database Connection
This tutorial uses an Oracle database as an example. The process of creating a connection is similar for all other JDBC drivers. To learn about JDBC visit the Sun website.
- Select Window>Perspective>Open Perspective>Database Explorer
This menu option was updated in MyEclipse 2017. For prior versions, click here.
. The first time you open the perspective, the Database Browser view shown on the left contains only the preconfigured MyEclipse Derby driver.
Database Explorer perspective tools
You need to create a new driver for each separate connection to a database. - Click on the DB Browser view, and select New.
Adding a new driver - Select Oracle (Thin driver) from the Driver Template drop-down.
- Type oracle in the Driver name field.
- Modify the template URL provided to the correct connection string. In this example, the local Oracle server is jdbc:oracle:thin:@localhost:1521:dbtest.
- Fill out the User name and Password fields with appropriate values for your server. If you do not want to save your password, there is no need to fill in the Password field.
Database driver details - Click the Add JARs button.
- Browse to location of the JDBC driver JAR file or classes folder, select it, and click Open.
Selecting Oracle JDBC driver JAR file
Note: In this case, the Oracle JDBC connector was found as part of the oracle installation files. - Inspect the value of the Driver Class Name. The ojdbc6.jar file contains two possible driver classes that can be selected from the Driver classname drop-down.
- Select oracle.jdbc.driver.OracleDriver from the Driver classname drop-down, and click Finish.
Field | Required | Description |
Driver name | Yes | A descriptive name that will appear Database Browser view. |
URL | Yes | The database connection string. Each database has its own URL structure, e.g. jdbc:oracle:thin:// |
User Name | No | The login name when creating a connection to the database. |
Password | No | The password when creating a connection to the database. |
Driver JARs | Yes | User managed JARs in this list are added to the Driver Manager’s classpath. If the database driver is not available on the the Java Classpath (see above) it should be added here. |
Driver Class Name | Yes | The fully qualified Java classname of the JDBC driver class. This class must be accessible on the combined classpath locations of the Java Classpath + Driver JARs lists. |
Save Password | No | Selecting this option instructs the Database Explorer to store the password locally. If this option is selected, you will not be prompted for your password when you attempt to connect. |
Open on Eclipse Startup | No | Selecting this option will instruct the Database Explorer to automatically create this connection during the Eclipse startup process. |
The new driver appears the DB Browser view.
DB Browser view with the Oracle driver
2. Open the Database Connection
Open a connection when MyEclipse is started or after any event that results in the database connection being dropped, such as a database server restart.
- Right click the driver, and select Open connection.
Opening a database connection
If the connection profile is configured with the Save Password option disabled, then the Open Database Connection wizard opens.
Entering the user name and password - Enter the password.
- Select the AutoCommit checkbox if you need changes to the database to be committed immediately. You can set the default auto-commit setting in the MyEclipse Database Explorer preferences.
- Click OK to initiate the database connection process. A new database connection opens and is displayed in the DB Browser.
New database connection in DB Explorer