Sometime java can be cumbersome, there are many things to do to make it works even for simple thing like connecting database to application server, it may take a whole day. If you know the proper way, it’s not that time consuming and you can run the java web application without facing unexpected behaviour. Here I try to make the summary, well at least I don’t need to visit many places on the browser when I just want to do set up and start to code rigth away. Hopefully this sumary may help you as well. The following are 7 steps in having Netbeans on your computer integrated with WildFly as the application server and Postgresql as your database.
1. Download and Install Java Development Kit (JDK)
We need JDK, as a software development environment for developing Java applications. Download the latest JDK here: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
2. Download and Install Netbeans IDE
Download the latest Netbeans here: https://netbeans.org/downloads/
In the installation process, klik Customize then uncheck the GlassFish Server Open Source Edition because we want to use WildFly instead, then continue the process until the installation is complete.
3. Get the Latest WildFly Application Server
If you don’t have WildFly on your computer, download one which has description Java EE7 Full & Web Distribution here: http://wildfly.org/downloads/
After you get the file, you just need to extract and move it into any directory you want, I prefer putting it on C:.
4. Integrate WildFly with Netbeans
Open the Netbeans application, right click Servers on the Services menu then click the Add Server...
Choose WildFly Application Server then click Next.
For the Server Location, browse to where you put the WildFly folder. Let the Server Configuration as its default (standalone-full) so we can use all its functions. Click Next and let all the fields as its default.
When it brings you back to your Netbeans window, right click again on WildFly Application Server then click Start.
On your browser, go to http://localhost:8080/. When you see the page below, it means your WildFly is running!
5. Integrate Postgresql with WildFly
Get the latest Postgresql here: https://www.postgresql.org/download/
Lets assume that you already have Postgresql as your database. Now you need Postgresql JDBC (Java Database Connectivity). Get the JDBC here: https://jdbc.postgresql.org/download.html, I put the file on the same directory with WildFly.
Run the following command:
from c:\wildfly-10.1.0.Final\bin using terminal.
Run the following command (red is your JDBC file path).
module add\ --name=org.postgresql\ --resources=C:\wildfly-10.1.0.Final\postgresql-9.4.1211.jar\ --resource-delimiter=,\ --dependencies=javax.api,javax.transaction.api
Now you need to register JDBC Postgresql as datasource. Input the command bellow on the terminal:
/subsystem=datasources/jdbc-driver=postgresql:add(driver-name=postgresql,\ driver-module-name=org.postgresql,\ driver-class-name=org.postgresql.Driver,\ driver-datasource-class-name=org.postgresql.ds.PGSimpleDataSource,\ driver-xa-datasource-class-name=org.postgresql.xa.PGXADataSource)
6. Create Datasource
Open your PG Admin application (it’s included in you Postgresql installation) and create your own database. Now go to C:\wildfly-10.1.0.Final\standalone\configuration and open standalone-full.xml file. Copy the following text into datasource part in that file (adjust the red with yours) then save the change.
<datasource jndi-name="java:jboss/datasources/mydb" pool-name="mydb" enabled="true" use-java-context="true"> <connection-url>jdbc:postgresql://localhost:5432/mydatabase</connection-url> <connection-property name="ServerName">localhost</connection-property> <connection-property name="PortNumber">5432</connection-property> <connection-property name="DatabaseName">mydatabase</connection-property> <driver>postgresql</driver> <security> <user-name>postgres</user-name> <password>postgres</password> </security> <validation> <check-valid-connection-sql>select 1</check-valid-connection-sql> <background-validation>true</background-validation> <background-validation-millis>5000</background-validation-millis> </validation> </datasource>
Select 1 is used as query to do connection test. In every 5000 ms the connection will be checked and reconnected.
7. Test Connection
On the terminal, run the following command to test your datasource connection.
The ritual is done! Now you can start to code as you got success from the outcome.