Connect to Weblogic Data Source using Java Program

Hi All,

Connecting to weblogic datasource is very easy, And it comes very handy when you want to debug some information on the fly. The below program will show how to connect to the weblogic data source and gets a connection from there.

public class ConnectWLSDataSource  {

public static void main(String[] args) {

Connection conn;
Statement stmt;
ResultSet rs;
String str1;

try {
Properties prop = new Properties();

prop.put(Context.INITIAL_CONTEXT_FACTORY, “weblogic.jndi.WLInitialContextFactory”);
prop.put(Context.PROVIDER_URL, “t3://HOST_NAME:PORT_NUMBER”);
Context ctx = new InitialContext(prop);
Object obj = ctx.lookup(“DATA_SOURCE_NAME”); // java:comp/env/CPDS
System.out.println(“Data Source Found….”);

DataSource ds = (DataSource) obj;
conn = ds.getConnection();
System.out.println(“Data Source User Name::”+conn.getMetaData().getUserName());
stmt = conn.createStatement();

String query = “select 1 from dual”;
System.out.println(“Query ” + query);
rs = stmt.executeQuery(query);
if (rs != null) {
System.out.println(“Some Data Found in Query”);
} else {
System.out.println(“No Data Found in Query”);
}
ctx.close();

} catch (Exception e) {
e.printStackTrace();
}

}
}


Thanks
R Vashi

4 thoughts on “Connect to Weblogic Data Source using Java Program

  1. Hi,

    I am getting an error that I can not connect to the WLS server.

    java.lang.NoClassDefFoundError: weblogic.security.acl.UserInfo.

    Thanks

    Andy

    • But the same code if i write in jsp then it works well:

      <%
      Connection conn;
      Statement stmt;
      ResultSet rs;
      String str1;

      try {
      Properties prop = new Properties();

      prop.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
      prop.put(Context.PROVIDER_URL, "t3://localhost:7001");
      Context ctx = new InitialContext(prop);
      Object obj = ctx.lookup("A"); // java:comp/env/CPDS
      out.println("Data Source Found….");

      DataSource ds = (DataSource) obj;
      conn = ds.getConnection();
      out.println("Data Source User Name::"+conn.getMetaData().getUserName());
      out.println("Data Source User Name::"+conn.getMetaData().getDriverName());
      stmt = conn.createStatement();

      String query = "SELECT * FROM CONTACT";
      out.println("Query " + query);
      rs = stmt.executeQuery(query);
      if (rs.next()) {
      out.println("\nSome Data Found in Query” + rs.getString(“email”));
      } else {
      out.println(“No Data Found in Query”);
      }
      ctx.close();

      } catch (Exception e) {
      e.printStackTrace();
      }

      %>

  2. Hi,

    I am getting below error:

    Data Source User Name::Oracle JDBC driver
    Query SELECT * FROM CONTACT
    java.lang.ClassCastException: [I cannot be cast to java.sql.ResultSet
    at weblogic.jdbc.rmi.internal.StatementImpl_weblogic_jdbc_wrapper_Statement_oracle_jdbc_driver_OracleStatement_810_WLStub.executeQuery(Unknown Source)
    at weblogic.jdbc.rmi.internal.StatementStub_weblogic_jdbc_rmi_internal_StatementImpl_weblogic_jdbc_wrapper_Statement_oracle_jdbc_driver_OracleStatement_810_WLStub.executeQuery(Unknown Source)
    at weblogic.jdbc.rmi.SerialStatement_weblogic_jdbc_rmi_internal_StatementStub_weblogic_jdbc_rmi_internal_StatementImpl_weblogic_jdbc_wrapper_Statement_oracle_jdbc_driver_OracleStatement_810_WLStub.executeQuery(Unknown Source)
    at myPack.Demo.main(Demo.java:113)

    When i am trying to run below by the below code:

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Properties;

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.sql.DataSource;

    public class Demo
    {
    public static void main(String [] args) throws Exception
    {

    Connection conn;
    Statement stmt;
    ResultSet rs;
    String str1;

    try {
    Properties prop = new Properties();

    prop.put(Context.INITIAL_CONTEXT_FACTORY, “weblogic.jndi.WLInitialContextFactory”);
    prop.put(Context.PROVIDER_URL, “t3://localhost:7001”);
    Context ctx = new InitialContext(prop);
    Object obj = ctx.lookup(“A”); // java:comp/env/CPDS
    System.out.println(“Data Source Found….”);

    DataSource ds = (DataSource) obj;
    conn = ds.getConnection();
    System.out.println(“Data Source User Name::”+conn.getMetaData().getUserName());
    System.out.println(“Data Source User Name::”+conn.getMetaData().getDriverName());
    stmt = conn.createStatement();

    String query = “SELECT * FROM CONTACT”;
    System.out.println(“Query ” + query);
    rs = stmt.executeQuery(query);
    if (rs.next()) {
    System.out.println(“\nSome Data Found in Query” + rs.getString(“email”));
    } else {
    System.out.println(“No Data Found in Query”);
    }
    ctx.close();

    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s