Setup Oracle Database Connection in Eclipse

To setup Oracle Database Connection in Eclipse is very much similar to any other Databases. There are 3 steps you need to follow.

Install Oracle Database

Since I didn’t find a non-install Oracle Database package, I assume that there is no such thing. So you have to install it, which takes a little bit more time but makes it way easier to setup.

  1. Download from Oracle (the link might be changing over time, just search on google)
  2. Install it

Note, for 11g express, the default Database Service Name is XE. To check that out, execute the following code in SQL Command Line.

select value from v$parameter where name like '%service_name%';

Oracle Database shipped with a cheapo Command Line tool which you can find once the installation finished. You can type connect or conn in it to connect to database.

Install Oracle Database Driver in Eclipse

You need to download Oracle Database Driver first.

Then in the Eclipse, open Preferences, and go to Data Management -> Connectivity -> Driver Definitions. You should probably not find any Oracle drivers there if you didn’t install Oracle Database Tools previously. Click add, it will pop up a dialog asking you what driver template you want to use. Again, if you don’t have Oracle Database Tools installed, you would just see Oracle Database Driver for 10g, Oracle Thin Driver for 8g to 11g and Other Driver for 8g to 11g. I found it doesn’t really matter to choose either one, but I choosed Thin Driver for 11g just because I’m using 11g. Switch to JAR List tab, and add JAR/ZIP you just download. Leave properties there, and done.

If you ever installed Oracle Database Tools, you’ll be having drivers installed. I’ll talk about that in another article: Introduction of Oracle Database Tools.

Setup Database Connection

Switch to Database Development Perspective and you will find Data Source Explorer window. Right click on Database Connections folder -> new. Choose the Oracle then next. Choose the driver you created earlier and fill out the properties as follows:

SID:xe
Service name:XE (ether one works by default)
Host: localhost
Port number: 1521
User name: SYSTEM (you can change users if you want)
Password: the password you set when installing the database (or created with your other users)
Connection URL: jdbc:oracle:thin:@localhost:1521:xe (auto complete, don't change)

You can try connection before created. If everything is right, you should be good to go.

Advertisements

Introduction of Oracle Database Tools

A Brief Description why you want Oracle Database Tools

This article will introduce you how to setup a GUI for your oracle database in Eclipse with Oracle Database Tools.

Note that most Databases actually have their own GUI tool programs, but Oracle 11g express is not shipped with one. For general purposes with almost every database, you can use Beaver DB, which is a really good tool you can also find in Eclipse Marketplace. But Oracle Database Tools are optimized specifically for Oracle DB, means they give you more capabilities to work with Oracle DB.

Installation

To setup Oracle Database Tool, go to Eclipse Marketplace within your Eclipse. Search Oracle Database Tools, and you will find it. It is developed by Oracle, and is free.

Once you done installation, you’ll find some icons in the Data Source Explorer are actually changed. If you haven’t setup Database Connection yet, check Setup Oracle Database Connection in Eclipse.

Start using it

If you have tables created by users other than SYSTEM, and you logged in using SYSTEM, you can find the tables under that user’s schema (the schema name is the user’s name). The following SQL code shows how to create a user:

CREATE USER <username> IDENTIFIED BY <password>
DEFAULT TABLESPACE users;
GRANT [privileges] TO <username>;

Once you created a user, you can expand that user’s schema in the hierarchy and explore all the contents of it. By right click on Tables and Edit Schema, you can view and edit the physical ERD of all tables this user has. The Tools can actually generate DDL as you editing the table on GUI.

Setup MySQL connection in WildFly and Eclipse

You need to Install JDBC Driver on your Application Server, and make sure your database is up and running properly before you start.

Configurations in WildFly

In web panel, go to Configuration -> Subsystems -> Datasources -> Non-XA, and click Add. Basically, Non-XA is generally for development environment or small systems, which does not control transaction across servers. Whereas, XA will take control of transactions across servers, which is more suitable for production environment. Once you select Non-XA and click Add button, it will prompt a list of datasource types. Here I use MySQL as an example, so choose MySQL Datasource and hit next. Then give a name and JNDI name. Click on next button and it turns to JDBC Driver. Because I’ve installed/deployed MySQL Driver, I go to Detected Driver tab, and select something like this: mysql-connector-java-5.1.39-bin.jar_com.mysql.jdbc.Driver_5_1 (don’t do the Fabric one). Next, specify the connection URL, which should include your database name. Also fill out the database username and password, and leave security domain blank. Then go next and test connection. If everything goes right, it should return success. Click finish to store this datasource.

Setup DB Development Connection in Eclipse

First, open Eclipse and switch to Database Development Perspective. In the Data Source Explorer panel, right click on Database Connection -> new. Choose MySQL and hit next. Click on New Driver Definition icon and select the corresponding version of your downloaded connector JAR file. Go to JAR List tab and Clear All the Driver files. Click Add JAR/Zip and select the JAR file you downloaded. Go to Properties and specify Connection URL, Database Name, Password and User ID. Leave Driver Class as it is. Click OK. You should now see everything setup in the previous window, and click on Test Connection. If something gets wrong, compare the to the example I have here.

Database: Your database name
URL: jdbc:mysql://localhost:3306 (by default)
Username: database username
Password: database password
Save password: checked

Click finish to store the configures. (You can hit next to review your settings, but there is nothing to change.) You will see your new database connection listed under Database Connections. Right click on it and connect to database. Database tables are listed under Connection Name/Database Name/Schemas/Database Name/Table. To run a SQL, right click on the connection name to open SQL scrapbook. In SQL Scrapbook, select connection name (database type will be auto selected once you select a name), and then select database. Then you can write and execute any SQL in this scrapbook by right click on any line of your SQL. You can execute all, or few selected lines.

* I find if you use oracle database, you can integrate Beaver DB which is in Eclipse Marketplace in order to create tables and constraints in GUI. In my personal experience, to build a new project, I usually start with creating tables in database, then generate Entity Beans in project in the first place. But if I’m going to switch database types afterward, I just use Entity Beans to generate tables in the new database, instead of writing SQL for all the different Databases.

 

Setup GlassFish in Eclipse

First, you need an Eclipse IDE up and running.
Since GlassFish needs JDK, you need to add JDK into your Eclipse environment. Do the following to add it:

  1. Make sure you have JDK installed/unzipped on your computer. I like to just unzip it, so that I can have multiple versions of JDKs. Check my another article on how to Unzip JDK from installation package.
  2. Go to Window -> Preferences -> Java -> Installed JREs
  3. Click Add, then select Standard VM, and hit next
  4. Give the directory of your JDK to JRE home. It’ll list all the libraries, and in the JRE name, you should see it start with jdk, not jre. Then, click Finish
  5. Check the jdk which you just added, and apply

Once you added JDK, still in Preferences window, go to Server -> Runtime Environment, and click Add to start a wizard to setup the server.
If you haven’t ever installed GlassFish, you need to install GlassFish tools First. Note that, you can also install it in Eclipse Marketplace. Here, you go to Oracle folder, and select GlassFish tools. Hit next, it will start downloading and installation. While it’s installing the GlassFish tools, you can download GlassFish server if you haven’t had one. Here are the links for different versions: GlassFish 3.1.2, GlassFish 4.1.1, or Payara.
After you successfully installed GlassFish tools and restart your Eclipse, go back to this wizard. Then, you will find GlassFish folder. Select a GlassFish version. Check Create a new local server. By creating a new local server (domain), you are prevented to mess arround with the original copy of your GlassFish server, so that you can always setup a new domain using this server. Hit next.
Name your server, and specify the GlassFish location, which is the glassfish folder of wherever you unziped your server files, e.g. glassfish4.1.1\glassfish. You may specify the Java location if you need one different to the default one (the one you set for Eclipse). Next.
Click on New Domain icon on the top right line, and set the Domain path to the one you just create. Configure the admin name and password (you can leave password blank, it’s just metter of production environment security). Debug port if for debuging the server (8009). Preserve sessions across redeployment will prevent losing sessions from redeployment (better to uncheck it). Use JAR to deploy is to use JAR instead of WAR, EAR etc (uncheck). Finish.

Finally, you want to create a server by using the Server runtime environment you just set. to do that, switch Eclipse prespective to Java EE and select Servers tag. Right click to New -> Server. Then you can create a server by using the runtime you’ve just created.