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 WildFly in Eclipse

First, you need an Eclipse IDE up and running.
Since WildFly 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, under Java EE perspective, select Servers tag. Right click on the blank area, then New -> Server.
If you haven’t ever installed WildFly, you need to install WildFly tools First. Here, you go to JBoss by Red Hat folder, and select JBoss/WildFly tools. Hit next, it will start downloading and installation. While it’s installing the WildFly tools, you can download WildFly server if you haven’t had one. Here is the link for version 10.0.0.Final: WildFly.
After you successfully installed WildFly tools and restart your Eclipse, go back to this wizard. Then, you will find JBoss Community folder. Select the WildFly version of whichever you downloaded. Hit next. In this step, for a typical development, you want to select local and Filesystem and shell operations. Uncheck Server lifecycle is externally managed, and choose Create new runtime (if you just download WildFly, then this is the only option. Otherwise, you can select an exist one, which you can view the Runtime Details).
Next, name your server, and specify the WildFly Home Directory, which is the WildFly folder of wherever you unziped your server files, and it should contain the bin folder. Select Runtime JRE. By default, it will pick up the one your Eclipse is using. You can associate Java versions with JREs/JDKs by clicking on Environments. You can also add JREs/JDKs by clicking on Installed JREs. To select a Runtime JRE, you can ether select the java version which you’ve assoicated with a JRE/JDK as the Excution Environment, or directly select the actual JRE/JDK as the Alternate JRE. (If you are using lower version wildfly, you can select a higher version JDK. But no vice versa) The Server base directory is where you want to make a copy of the server, just like a domain in GlassFish. In base directory, the instance of your server specified files will be stored. The configuration file by default is standalone.xml, which stores the configurations of this server instance. Click on Next, and then Finish. Now, you have your WildFly server ready to run.

Bonus Component:

You’ll be probably asked to create user to login WildFly console at first time. Simply go to the bin folder and double click on add-user.bat. Then follow the propt to create a management user.
Note whenever it asks yes/no, type yes/no insteand of y/n. When it asks if you want to add the user to a group, just leave blank and hit Enter. When it asks if the user is going to be used for one AS process to connect to another AS process, you need to type no (unless you know what you are doing).

Once you add a management user, reenter localhost:9990 to login with the user.