Login ing database access in eclipse 3.0 with sqlexplorer

Source: Internet
Author: User
Tags sql client sybase
Document directory
  • Overview
  • Preliminary setup
  • Configuring sqlexplorer
  • Accessing Database Data
  • Conclusion
Login ing database access in eclipse 3.0 with sqlexplorer

By Deepak Vohra
05/11/2005

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
Overview

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 FileNet.sourceforge.sqlexplorer_2.2.3.zip.
  3. Extract the sqlexplorer. ZIP file to<Eclipse>/eclipseDirectory.<Eclipse>Is the directory in which eclipse is installed. This addsFeaturesAndPluginsDirectories of the sqlexplorer plugin toFeaturesAndPluginsDirectories 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 withRootWith the command:
    >mysql -u root

    A password is not required forRootUser. to log in to the database with a password, specify the command:

    >mysql -u root -p
  8. Connect totestDatabase, an example database instance, with the command:
    >use test
  9. Create an example table intestDatabase. 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 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 into strates the "open a perspective" button.


Figure 1. Opening a perspective

In the item list, select "other..." to display the sqlexplorer plugin as shown in figure 2.


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.


Figure 3. sqlexplorer perspective

Selecting the sqlexplorer perspective displays the features of the sqlexplorer plugin 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 your strated in Figure 4.


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 into strates adding the MySQL JDBC driver to the eclipse classpath.


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 withtestDatabase, which is preconfigured In the MySQL install. The connection URL fortestDatabase isjdbc:mysql://localhost/test. In the Driver Class Name field, specify the MySQL JDBC drivercom.mysql.jdbc.Driver. The mmmysql driver gets configured with the settings shown in figure 6.


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.


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 databasetest,jdbc:mysql://localhost/test, In the URL field. Figure 8 shows the MySQL alias settings.


Figure 8. Creating a New Alias

This adds a MySQL alias to the aliases tab frame, which is already strated in Figure 9. to modify an alias, right-click on the alias node and select "change the selected 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.


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 forRootUser.


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 replicated strated in Figure 12.


Figure 12. Listing the database schemas

Support for other databases

The example JDBC connection is configured with the MySQL database. a jdbc connection may be configured with another database by selecting the driver node for the database in the drivers tab. by specifying the driver class and connection URL for the selected database, a JDBC connection gets configured with the database. the driver class, the connection URL, and the driver. JAR file for some of the other databases are listed below:

  • DB2
    • Driver Class:COM.ibm.db2.jdbc.app.DB2Driver
    • Connection URL:jdbc:db2:<database>
    • Driver. Jar/. Zip:db2java.zip
  • Sybase
    • Driver Class:com.sybase.jdbc2.jdbc.SybDriver
    • Connection URL:jdbc:sybase:Tds:
    • Driver. Jar/. Zip:jconn2.jar
  • Oracle
    • Driver Class:oracle.jdbc.driver.OracleDriver
    • Connection URL:jdbc:oracle:thin:@
    • Driver. Jar/. Zip:classes12.zip
  • Sqlserver
    • Driver Class:com.microsoft.jdbc.sqlserver.SQLServerDriver
    • Connection URL:jdbc:microsoft:sqlserver://localhost:1433
    • Driver. Jar/. Zip:mssqlserver.jar,msbase.jar,msutil.jar
  • PostgreSQL
    • Driver Class:org.postgresql.Driver
    • Connection URL:jdbc:postgresql://<server>:<port>/<database>
    • Driver. Jar/. Zip:postgresql.jar

In the above list,<database>Is the database instance,<port>Is the database port,<sid>Is the database Sid, and<server>Is the database server.

Accessing Database Data

We configured the Eclipse IDE with the sqlexplorer Plugin in the previous section. Next, we shall retrieve and modify the data from the example tableCatalog. If a database is accessed from a command-line SQL client, table data is retrieved with the following (all on one line ):

SQL>SELECT catalogId, journal, publisher, date,    title, author from Catalog;

This displays the data as a text table. with the gui SQL client sqlexplorer, the data is displayed as a structured table. sqlexplorer also generates the SQL scripts to create a table and select from it. if a table structure is displayed in a command-line client withDESCCommand, only the column name, column type, column size, and "not null" values get displayed. With sqlexplorer, the indexes, primary key, and foreign key values are also displayed.

Select the database structure view tab in the sqlexplorer perspective in eclipse. to display the structure of the catalog table, selectDatabase>test>TABLE>CatalogNode in the database structure view. Figure 13 shows the catalog table structure.


Figure 13. Database Structure View

The columns tab displays the columns listed in the table below:

Header Description
Column name The column name in the table.
Data Type The data type for the column.
Size The column size.
Decimal Digits The decimal digits in the column data.
Default Value The default value of the able column.
Accept Null Value Specifies if the column takes null values.
Comments Comments on the table column.

To display the data in the table selected inTABLENode, select the preview tab. Figure 14 shows the table data for the catalog table. Additional information about a table is displayed with the indexes, primary key, foreign key, and row count tabs.


Figure 14. Listing the table data

To create a SQL script to create the table, right-click on the table node and select CREATE TABLE script, as shown in Figure 15.


Figure 15. Creating table script

This creates the SQL script to create the selected table and displays it inSQL EditorOf the sqlexplorer perspective, which is shown in Figure 16.


Figure 16. Table script

The data displayed in the preview tab of the database structure view is retrieved with the default SELECT query, which has des all of the columns in the table. to display the default SELECT query, right-click on the table node and select "generate select in SQL Editor," as shown in figure 17.


Figure 17. Generating the default SELECT query

The default query to retrieve data from the catalog table gets displayed in the SQL Editor, as figure 18 catalog strates. Note thatSELECTQueries displayed in the SQL editor do not have a semicolon (;) At the end of the SQL statement.


Figure 18. SELECT query

The query may be customized to display only some of the columns in the table. For example, modify the SELECT query to display all of the columns except tCatalogIdColumn. To run the SQL script, select the Execute SQL button. The data from the modified SELECT query gets displayed in the SQL results frame, as shown in Figure 19.


Figure 19. Selecting table data with customSELECTQuery

Next, the catalog table shall be updated with an SQL script in the SQL editor. for example, modify the title from "five favorite features from 5.0" to "new features in JDK 5.0. "the SQL script to update the catalog table is run in the SQL editor as shown in Figure 20.


Figure 20. Update SQL script

The table data gets updated. Run the default SELECT query on the modified table to display the modified data in the SQL results frame. Figure 21 shows the modified catalog table data.


Figure 21. Modified table data

Next, delete a row from the table withDELETESQL statement in the SQL Editor, as shown in Figure 22. The table rowCatalogId='3'Gets deleted from the table.


Figure 22.DELETESQL script

Run the default SELECT query to display the modified table data. The SQL results frame table does not include the deleted row, as shown in Figure 23.


Figure 23. Table data with row deleted

By grouping the sqlexplorer plugin eclipse, the IDE acquires the advantages of a gui SQL client over a command-line client.

Conclusion

For the example database table, a JDBC connection was established with the MySQL database. the SQL Explorer may also be used to configure a connection with other databases, which include DB2, Sybase, Oracle, HSQLDB, SQL Server, and PostgreSQL.

Resources
  • Sqlexplorer
  • Eclipse IDE
  • MySQL database

Deepak Vohra is a nubean consultant and a web developer.

Related reading

Eclipse cookbook
By Steve holzner

Table of contents
Index
Sample chapter

Read online -- Safari
Search this book on safari:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.