How to Load MySQL Database Driver in Wildfly Java Application Server

Posted on

In this article, the main purpose is solely to explain the steps taken in order to load MySQL Database Driver in Wildfly Java Application Server. It is needed because the connection made from the application deployed,  in the case of the application explained in the context of this article, it needs to be connected to MySQL Database Server. Somehow it depends to the driver loaded and defined in Wildfly Java Application Server for the connection from the application to the MySQL Database Server can be made. So, the steps for loading the MySQL Database Driver can be enlisted in the following sequences :

1. Download MySQL Database Server connector. The following is the display of the official website of MySQL where it can be found in this link and the site itself is providing links of connector that can be downloaded :

How to Load MySQL Database Driver in Wildfly Java Application Server
How to Load MySQL Database Driver in Wildfly Java Application Server

 

Still on the same page, just choose the Platform Independent in the Select Operating System dropdown list as shown in the following picture below :

2. Extract the downloaded file representing the MySQL Database Server which has been stored locally. In the context of this article, the successfully downloaded file has the name of ‘mysql-connector-java-8.0.11.zip’.

3. Get the jar file and place it in the location which is specified in the specific location of the Wildfly Java Application Server. In the context of this article, for an example, the jar file is located in the following location of the already extracted zip file :

user@hostname:~/Downloads/mysql-connector/mysql-connector-java-8.0.11$ tree -L 2
.
├── build.xml
├── CHANGES
├── lib
│   ├── c3p0-0.9.1-pre6.jar
│   ├── c3p0-0.9.1-pre6.src.zip
│   ├── jboss-common-jdbc-wrapper.jar
│   ├── jboss-common-jdbc-wrapper-src.jar
│   ├── protobuf-java-2.6.0.jar
│   └── slf4j-api-1.6.1.jar
├── LICENSE
├── mysql-connector-java-8.0.11.jar
├── nohup.out
├── README
└── src
    ├── build
    ├── demo
    ├── generated
    ├── legacy
    ├── main
    └── test

8 directories, 12 files
user@hostname:~/Downloads/mysql-connector/mysql-connector-java-8.0.11$ 

Copy the file to the specific location for instance, if the root folder of the Wildfly Java Application Server is located in ‘/opt/wildfly-12.0.0.Final’, the location for storing the jar file will be in ‘/opt/wildfly-12.0.0.Final/modules/system/layers/base/com/mysql/main’. If it isn’t made, just create it for instance there is no folder ‘mysql/main’ inside the com folder, so to create the folder, just execute the following command :

user@hostname:/opt/wildfly-12.0.0.Final/modules/system/layers/base/com$ pwd
/opt/wildfly-12.0.0.Final/modules/system/layers/base/com
user@hostname:/opt/wildfly-12.0.0.Final/modules/system/layers/base/com$ mkdir -p mysql/main
user@hostname:/opt/wildfly-12.0.0.Final/modules/system/layers/base/com$ cd mysql/main/
user@hostname:/opt/wildfly-12.0.0.Final/modules/system/layers/base/com/mysql/main$ pwd
/opt/wildfly-12.0.0.Final/modules/system/layers/base/com/mysql/main

4.  After creating the folder and then further more copying the file mentioned before, create another file named ‘module.xml’ in the same exact location specified in the previous step :

user@hostname:/opt/wildfly-12.0.0.Final/modules/system/layers/ase/com/mysql/main$ ls
module.xml  mysql-connector-java-8.0.11.jar
user@hostname:/opt/wildfly-12.0.0.Final/modules/system/layers/base/com/mysql/main$ pwd
/opt/wildfly-12.0.0.Final/modules/system/layers/base/com/mysql/main
user@hostname:/opt/wildfly-12.0.0.Final/modules/system/layers/base/com/mysql/main$ 

The content of the file named ‘module.xml’ is shown below :

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-8.0.11.jar" />
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>

Things which needs to be looked out for is the value of the <resource-root> tag where the path attribute is pointing to the exact location of the jar file. Furthermore there is the module name which in the context of this article, it is ‘com.mysql’. It must be properly defined as if it is not done correctly, the driver cannot be loaded.

3 thoughts on “How to Load MySQL Database Driver in Wildfly Java Application Server

Leave a Reply