Friday, 17 November 2017

Using weblogic data source from deployed applications

In this blog I am going to show how weblogic datasource can be use to call execute SQL.
To use the weblogic internal datasource, the best option is to use it inside the deployed application.
Here in this example I have created an Web Service application which is deployed in weblogic admin server
and I used the datasource of OIM application which is implemented under oim server (port is 8005).


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Hashtable;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.naming.Context;


@WebService
public class WeblogicDS {
    public String userkey = "";

    public WeblogicDS() {
        super();
    }
    @WebMethod
    public String connectDS(String applicationPort) {

        Hashtable props = new Hashtable();
        props.put(Context.INITIAL_CONTEXT_FACTORY,
                  "weblogic.jndi.WLInitialContextFactory");
        props.put(Context.PROVIDER_URL, "t3://localhost:" + applicationPort);
        System.out.println("1");
        Connection connection = null;
        javax.naming.Context ctx = null;
        javax.sql.DataSource dataSource = null;

        try {

            ctx = new javax.naming.InitialContext(props);
            System.out.println("2");
            dataSource = (javax.sql.DataSource)ctx.lookup("jdbc/ApplicationDBDS"); //JNDI name of the datasource
            System.out.println(dataSource);
            connection = dataSource.getConnection();
            System.out.println("connected");

            PreparedStatement pstmt = null;
            ResultSet appInstRS = null;

            String query = "select * from usr where usr_login='XELSYSADM'";
            pstmt = connection.prepareStatement(query);
            System.out.println("Status:" + pstmt.execute());
            pstmt.getResultSet().next();
            userkey = pstmt.getResultSet().getString("USR_KEY");

        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return userkey;

    }
}

Here the webmethod connectDS is taking 1 input which is the application port is 8005.



No comments:

Post a Comment

Followers

OIM API for adding process task and retry failed task

 In this blog you can find how to add new process task and retry any failed/rejected tasks using API. Adding new process task: /************...