Tuesday, March 5, 2013

Connecting to a WebLogic Data source from Java

Version: WebLogic 10.3.5
Description: You can use Java to connect to any data source defined in WebLogic. An advantage to this is you do not need the database user credential hard-coded in your Java application. All you need to supply in order to establish a connection in your application are the name of the data source and the provider URL.
Requirements: Remote JDBC must be enabled if you are executing the code remotely. The instructions to enable remote JDBC are commented in the code. The data source you are using must be defined in WebLogic. Click here to learn how create a data source in WebLogic Administration Console.

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * Connects to the OIM data-source defined in the WebLogic Administration Console.
 * This code can be generalized to use any data-source defined in WebLogic Administration
 * Console. When supplying the the provider url (server instance url), make sure the data source is deployed
 * on the server instance. E.g. "oimOperationsDB" (jdbc/operationsDB) is deployed on the OIM and SOA instance.
 * You can use either the SOA or the OIM as the provider url.
 * 
 * Troubleshooting:
 * Error:
 * Exception in thread "main" javax.naming.NoInitialContextException: Cannot 
 * instantiate class: weblogic.jndi.WLInitialContextFactorty [Root exception 
 * is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactorty]
 * Fix:
 * Add "wlfullclient.jar" to the classpath of your project.
 * 
 * Error:
 * Exception in thread "main" java.lang.UnsupportedOperationException: Remote JDBC disabled
 * Fix:
 * Modify the setDomainEnv.sh located in "/home/oracle/Oracle/Middleware/user_projects/domains/oim_domain/bin"
 * Find "WLS_JDBC_REMOTED_ENABLED" and change value.
 * WLS_JDBC_REMOTE_ENABLED="-Dweblogic.jdbc.remoteEnabled=true"
 * Execute the "setDomainEnv.sh" script and restart the WebLogic ADmin Server.
 * 
 * Error:
 * Exception in thread "main" java.lang.NoClassDefFoundError: oracle/sql/BfileDBAccess
 * Fix:
 * Add "ojdbc6.jar" driver to the classpath of your project.
 * 
 */
public class OIMDataSourceConnection
{
    public static void main(String[] args)
    {
        String webLogicContext="weblogic.jndi.WLInitialContextFactory"; //Name of the WebLogic Context
        String providerURL="t3://localhost:14000"; //OIM URL
        String dataSourceName = "jdbc/operationsDB"; //JNDI Name
        Connection connection = null; //connection to database
        Statement st = null;
        ResultSet rs = null;

        try 
        {
            //Set your WebLogic Properties 
            Properties properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY,webLogicContext);
            properties.put(Context.PROVIDER_URL, providerURL);

            Context context = new InitialContext(properties); //create the initial WebLogic Context 
            DataSource dataSource = (DataSource) context.lookup(dataSourceName); //lookup a datasource in WebLogic
            System.out.println("Lookup dataSource returned: " + dataSource);

            connection = dataSource.getConnection(); //Establish connection to the OIM database
            System.out.println("Connection to \"" + dataSourceName + "\" Established: " + connection.toString());

            //Query from the USR table
            st = connection.createStatement();
            String query = "SELECT * FROM USR";
            rs = st.executeQuery(query);

            while(rs.next())
            {
                String userLogin = rs.getString("usr_login");
                System.out.println(userLogin);
            }
        } 

        catch (SQLException ex) 
        { 
            Logger.getLogger(OIMDataSourceConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

        catch (NamingException ex) 
        {
            Logger.getLogger(OIMDataSourceConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

        finally
        {
            if(rs != null)
            {
                try {
                    rs.close();
                } catch (SQLException ex) {
                    Logger.getLogger(OIMDataSourceConnection.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if(st != null)
            {
                try {
                    st.close();
                } catch (SQLException ex) {
                    Logger.getLogger(OIMDataSourceConnection.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if(connection != null)
            {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(OIMDataSourceConnection.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }//end main
}//end class

No comments:

Post a Comment