How to connect to ORACLE, DB2, MYSQL databases from Jboss EAP / Jboss AS / Wildfly Using JDBC driver and DataSource.



How to connect to ORACLE, DB2, MYSQL databases from Jboss EAP / Jboss AS / Wildfly.

There are two ways.

  • Programmatically using JDBC Driver.
  • Using Datasource.
  • How to connect to ORACLE, DB2, MYSQL databases from Jboss EAP / Jboss AS / Wildfly. Using JDBC Driver


    Download the latest JDBC Jar if you are using oracle database download the driver from ORACLE


    Once you have downloaded the jar, add the downloaded jar to the development tool you are using ( eclipse, STS, Intellij ). If running from command prompt use -cp option set the jar class path.


    Once you are done with above steps, run the below code snippet. Below code snippet loads the jdbc driver and gets the connection


    DBUtility

    						 
    package com.database.util;
    
    import java.sql.DriverManager;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    /**
     * 
     * @author Techoral.com
     * 
     *         This program demonstrates how to use Jdbc7 driver to connect to
     *         ORACLE Database
     *
     */
    
    public class DBUtility {
    
    	public static void main(String[] args) {
    
    		try {
    
    			Class.forName("oracle.jdbc.driver.OracleDriver");
    
    		} catch (ClassNotFoundException e) {
    
    			System.out.println("No JDBC driver loaded!!!, check the wheather JDBC jar is in the classpath");
    			e.printStackTrace();
    
    		}
    
    		// create connection
    		Connection connection = null;
    
    		try {
    			// If the DB servers is on the another host, replace the localhost with that
    			// hostname
    			// make sure database shcema, port number and credentials are correct.
    			connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:stdev", "dbusername",
    					"dbpassword");
    
    		} catch (SQLException e) {
    
    			System.out.println("Error while connecting to DB please check the exception stackstrace!");
    			e.printStackTrace();
    
    		}
    
    		if (connection != null) {
    			System.out.println("##########################################");
    			System.out.println("connection successull.");
    		} else {
    			System.out.println("connection failed");
    		}
    	}
    
    }
    
    						


    Run the Snippet in Command Prompt:

    						 
    C:\jdbc>javac DBUtility.java C:\jdbc>java -cp c:\jdbc\lib\ojdbc7.jar;c:\jdbc DBUtility
    ########################################## 
    connection successfull. 
    						


    How to connect to ORACLE, DB2, MYSQL databases from Jboss EAP / Jboss AS / Wildfly. Using Datasource


    Standard way to connect to ORACLE, DB2, MYSQL databases from Jboss EAP / Jboss AS / Wildfly is to use the Datasource. Datasource can be configured from the respective application server's admin console or from the server configuration files


    In case of JBOSS, datasource can be configured in standalone.xml / standalone-ha.xml / standalone-full-ha.xml files which are available in configuration directory

    /JBOSS-HOME-DIR/standalone/configuration

    Datasource configuration is same in all standalone.xml / standalone-ha.xml / standalone-full-ha.xml files

    Here is how Datasource configurationion in standalone-ha.xml looks like


    standalone-ha.xml


    Make sure that ojdbc7.jar module.xml are present under below path :

    \jboss-eap-home-directory\modules\system\layers\base\com\oracle\ojdbc7\main


    Make sure that module.xml points to ojdbc7.jar



    Once you are done with above steps, start the jboss server and run the below code snippet in command prompt or in elipse or sts.

    Below code snippetDataSourceUtility.java gets the connection from data source

    						 
    package com.database.util;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.sql.DataSource;
    
    public class DataSourceUtility {
    
    	public static void main(String[] args) {
    
    		Connection connection = null;
    		try {
    			InitialContext context = new InitialContext();
    			DataSource dataSource = (DataSource) context.lookup("java:/PRDS");
    			connection = dataSource.getConnection();
    		} catch (NamingException e) {
    			e.printStackTrace();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    
    		if (connection != null) {
    			System.out.println("##########################################");
    			System.out.println("connection successfull.");
    		} else {
    			System.out.println("connection failed");
    		}
    	}
    }
     
    							
    							


    Run the below snippet in Command Prompt:

    Command Prompt

    						 
    C:\datasource>javac DataSourceUtility.java
    
    C:\datasource>java -cp c:\datasource DataSourceUtility
    ###################################### 
    connection successfull.