站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题Configuring Database Access in Eclipse 3.0 with SQLExplorer

Configuring Database Access in Eclipse 3.0 with SQLExplorer

摘要: Configuring Database Access in Eclipse 3.0 with SQLExplorer

Configuring Database Access in Eclipse 3.0 with SQLExplorer

by Deepak Vohra
05/11/2005

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

  • Connect to the test database, an example database instance, with the command:
    >use test
    
    
  • 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.


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


    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.


    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.


    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 database test, 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 illustrated 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 for the root user.


    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.


    Figure 12. Listing the database schemas

    <table cellspacing="0" cellpadding="0" width="100%" border="0"><tr><td>

    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 with the DESC command, 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, select the Database>test>TABLE>Catalog node 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:

    <table border="1"><tr><td>Header</td><td>Description</td></tr><tr><td>Column Name</td><td>The column name in the table.</td></tr><tr><td>Data Type</td><td>The data type for the column.</td></tr><tr><td>Size</td><td>The column size.</td></tr><tr><td>Decimal Digits</td><td>The decimal digits in the column data.</td></tr><tr><td>Default Value</td><td>The default value of the able column.</td></tr><tr><td>Accept Null Value</td><td>Specifies if the column takes null values.</td></tr><tr><td>Comments</td><td>Comments on the table column.</td></tr></table>

    To display the data in the table selected in the TABLE node, 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 in the SQL Editor of 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 includes 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 illustrates. Note that the SELECT queries 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 the CatalogId column. 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 custom SELECT query

    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 with a DELETE SQL statement in the SQL Editor, as shown in Figure 22. The table row with CatalogId='3' gets deleted from the table.


    Figure 22. DELETE SQL 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 configuring the SQLExplorer plugin in 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

    Deepak Vohra is a NuBean consultant and a web developer.



    ↑返回目录
    前一篇: 利用XMLBuddy在Eclipse中开发XML
    后一篇: Eclipse 使用技巧