Thursday, October 31, 2013

Eclipse RCP and Database Connection Example



In this tutorial, we’re developing an Eclipse Plugin project that connects to a database (HSQLDB) and display the data from the table in the database.

SET 1: SET UP ECLIPSE FOR PLUG-IN DEVELOPMENT

1.  Install Eclipse SDK:

For this tutorial, Eclipse Indigo (3.7) is used, install Eclipse RCP Plug-in Developer Resources* as shown

STEP 2: CREATE ECLIPSE RCP Plug-in PROJECT NAMED com.icode.rcp.demo

1. New > Plug-in Project 



2. Run test the Plug-in

Right click on com.icode.rcp.demo > Run As > Eclipse Application (or open plugin.xml > Overview > Launch an Eclipse application)


So far so good!... the Plug-in is running. Now let's add a view into the GUI.

3. Add a view to the Plug-in


 - plugin.xml > Extensions > Add > org.eclipse.ui.views



- right click on org.eclipse.ui.views > New > view
 - fill info as shown (click on class to create the class CustomerView)



4. Activate the view:
 - add this line into the CustomerView.java
            public final static String ID = "com.icode.rcp.demo.views.customer";
- add this into Perspective.java
          String editorArea = layout.getEditorArea();
         layout.setEditorAreaVisible(false);
         layout.addView(CustomerView.ID, IPageLayout.LEFT, 1.0f, editorArea);

as shown


5. Run test the plug-in for the newly-create view



So far so good!... Let's move on to the next step.


STEP 3: CONNECT TO THE DB

1. Setup HSQLDB
- download hsqldb and start up the server
- create CUSTOMER table 
- insert these sample data
as shown

      



2. Create class Customer.java and CustomerProvider as follows



3. Create class JdbcUtil.java  as shown



If you run test JdbcUtil.java now, you will get this error:


To fix that error, we need to add hsqldb.jar into the com.icode.rcp.demo class path. In order to that, we have to create another plugin project. Let's do it.

4.  Create a plugin project from existing archive named org.jdbc.hsqldb as shown:


5. Export org.jdbc.hsqldb into the target location 

Note: make sure org.jdbc.hslqd plugin shown up in the target definition as shown (You may have to restart your Eclipse if reloading  not working)




6. Add org.jdbc.hsqldb into com.icode.rcp.demo project as a dependency


Run test JdbcUtil.java again





So far so good! The back end of the plug-in (com.icode.rcp.demo) can connect to the db. Now let’s modify the views to display the data from the db.

7.  Modify the CustomerView



8. Run test the plugin com.icode.rcp.demo



There you go! Now you have a Eclipse Plugin app connected with the db. Enjoy it!

Note: Along the process, if you run into the following error. Don't panic. 

error - could not found in the registry

solution: clean project, or restart Eclipse

No comments:

Post a Comment