Eclipse3.0配置SQLExplorer访问数据库

原文地址:http://onjava.com/pub/a/onjava/2005/05/11/sqlexplorer.html?page=1

中文版地址:http://www.qqread.com/java/r620278060.html

Configuring 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 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 testjdbc: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

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 ClassCOM.ibm.db2.jdbc.app.DB2Driver
    • Connection URLjdbc:db2:<database>
    • Driver .jar/.zipdb2java.zip
  • Sybase
    • Driver Classcom.sybase.jdbc2.jdbc.SybDriver
    • Connection URLjdbc:sybase:Tds:<host>:<port>/<database>
    • Driver .jar/.zipjconn2.jar
  • Oracle
    • Driver Classoracle.jdbc.driver.OracleDriver
    • Connection URLjdbc:oracle:thin:@ <host>:<port>:<sid>
    • Driver .jar/.zipclasses12.zip
  • SQLServer
    • Driver Classcom.microsoft.jdbc.sqlserver.SQLServerDriver
    • Connection URLjdbc:microsoft:sqlserver://localhost:1433
    • Driver .jar/.zipmssqlserver.jarmsbase.jarmsutil.jar
  • PostgreSQL
    • Driver Classorg.postgresql.Driver
    • Connection URLjdbc:postgresql://<server>:<port>/<database>
    • Driver .jar/.zippostgresql.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 table Catalog. 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 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.

Database Structure View
Figure 13. Database Structure View

The Columns tab displays the columns listed in the Table below:

HeaderDescription
Column NameThe column name in the table.
Data TypeThe data type for the column.
SizeThe column size.
Decimal DigitsThe decimal digits in the column data.
Default ValueThe default value of the able column.
Accept Null ValueSpecifies if the column takes null values.
CommentsComments on the table column.

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.

Listing the table Data
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.

Creating Table Script
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.

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

Generating default Select Query
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.

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

Selecting table data with custom SELECT query
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.

Update SQL Script
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.

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

DELETE SQL Script
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.

Table Data with Row Deleted
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.

内容概要:本文围绕EKF SLAM(扩展卡尔曼滤波同步定位与地图构建)的性能展开多项对比实验研究,重点分析在稀疏与稠密landmark环境下、预测与更新步骤同时进行与非同时进行的情况下的系统性能差异,并进一步探讨EKF SLAM在有色噪声干扰下的鲁棒性表现。实验考虑了不确定性因素的影响,旨在评估不同条件下算法的定位精度与地图构建质量,为实际应用中EKF SLAM的优化提供依据。文档还提及多智能体系统在遭受DoS攻击下的弹性控制研究,但核心内容聚焦于SLAM算法的性能测试与分析。; 适合人群:具备一定机器人学、状态估计或自动驾驶基础知识的科研人员及工程技术人员,尤其是从事SLAM算法研究或应用开发的硕士、博士研究生和相关领域研发人员。; 使用场景及目标:①用于比较EKF SLAM在不同landmark密度下的性能表现;②分析预测与更新机制同步与否对滤波器稳定性与精度的影响;③评估系统在有色噪声等非理想观测条件下的适应能力,提升实际部署中的可靠性。; 阅读建议:建议结合MATLAB仿真代码进行实验复现,重点关注状态协方差传播、观测更新频率与噪声模型设置等关键环节,深入理解EKF SLAM在复杂环境下的行为特性。稀疏 landmark 与稠密 landmark 下 EKF SLAM 性能对比实验,预测更新同时进行与非同时进行对比 EKF SLAM 性能对比实验,EKF SLAM 在有色噪声下性能实验
内容概要:本文围绕“基于主从博弈的售电商多元零售套餐设计与多级市场购电策略”展开,结合Matlab代码实现,提出了一种适用于电力市场化环境下的售电商优化决策模型。该模型采用主从博弈(Stackelberg Game)理论构建售电商与用户之间的互动关系,售电商作为领导者制定电价套餐策略,用户作为跟随者响应电价并调整用电行为。同时,模型综合考虑售电商在多级电力市场(如日前市场、实时市场)中的【顶级EI复现】基于主从博弈的售电商多元零售套餐设计与多级市场购电策略(Matlab代码实现)购电组合优化,兼顾成本最小化与收益最大化,并引入不确定性因素(如负荷波动、可再生能源出力变化)进行鲁棒或随机优化处理。文中提供了完整的Matlab仿真代码,涵盖博弈建模、优化求解(可能结合YALMIP+CPLEX/Gurobi等工具)、结果可视化等环节,具有较强的可复现性和工程应用价值。; 适合人群:具备一定电力系统基础知识、博弈论初步认知和Matlab编程能力的研究生、科研人员及电力市场从业人员,尤其适合从事电力市场运营、需求响应、售电策略研究的相关人员。; 使用场景及目标:① 掌握主从博弈在电力市场中的建模方法;② 学习售电商如何设计差异化零售套餐以引导用户用电行为;③ 实现多级市场购电成本与风险的协同优化;④ 借助Matlab代码快速复现顶级EI期刊论文成果,支撑科研项目或实际系统开发。; 阅读建议:建议读者结合提供的网盘资源下载完整代码与案例数据,按照文档目录顺序逐步学习,重点关注博弈模型的数学表达与Matlab实现逻辑,同时尝试对目标函数或约束条件进行扩展改进,以深化理解并提升科研创新能力。
内容概要:本文介绍了基于粒子群优化算法(PSO)的p-Hub选址优化问基于粒子群优化算法的p-Hub选址优化(Matlab代码实现)题的Matlab代码实现,旨在解决物流与交通网络中枢纽节点的最优选址问题。通过构建数学模型,结合粒子群算法的全局寻优能力,优化枢纽位置及分配策略,提升网络传输效率并降低运营成本。文中详细阐述了算法的设计思路、实现步骤以及关键参数设置,并提供了完整的Matlab仿真代码,便于读者复现和进一步改进。该方法适用于复杂的组合优化问题,尤其在大规模网络选址中展现出良好的收敛性和实用性。; 适合人群:具备一定Matlab编程基础,从事物流优化、智能算法研究或交通运输系统设计的研究生、科研人员及工程技术人员;熟悉优化算法基本原理并对实际应用场景感兴趣的从业者。; 使用场景及目标:①应用于物流中心、航空枢纽、快递分拣中心等p-Hub选址问题;②帮助理解粒子群算法在离散优化问题中的编码与迭代机制;③为复杂网络优化提供可扩展的算法框架,支持进一步融合约束条件或改进算法性能。; 阅读建议:建议读者结合文中提供的Matlab代码逐段调试运行,理解算法流程与模型构建逻辑,重点关注粒子编码方式、适应度函数设计及约束处理策略。可尝试替换数据集或引入其他智能算法进行对比实验,以深化对优化效果和算法差异的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值