Saturday, April 02, 2016

Retrieving Data from DataSource Configured in WebLogic 12.1

Suppose I have MySQL Database called RetailStore and have table Order containing order related data which customer submitted via online applications. Our requirement is to write java program which will read data from DataSource which is configured in Weblogic for our MYSQL Database.

You need to follow below steps.
  1. Create DataSource in WebLogic. I have used MySQL as DB and Weblogic 12.1.1 as Application Server.
  2. Test DataSource in weblogic
  3. Create HashMap consisting Initial Context Factory and Provider URL
  4. Create initial context Factory
  5. Do Lookup on context using JNDI configured for DataSource and have dataSource instance
  6. Get Connection from DataSource and Perform common JDBC Logic to read data from Table.

DataSource in WebLogic
Go to Services -> DataSource-> Create new DataSource. you will be asked to follow series of steps.
If you have MYSQL Db then please provide below configuration


MySql DB Configuration to be used in Weblogic
Ex. I have datasource RetailStoreDS with JNDI as DS/RetailStoreDS
URL - jdbc:mysql://localhost:3306/retailstore
Driver - com.mysql.jdbc.Driver
user/password - your MSQL DB Credentials

Before submitting you will be asked to test configuration. Upon Successful creation, you can view DataSource as below.









How to Retrieve Data from DataSource Configured in WebLogic 12.1
package com.anuj.dataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;

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

/**
 * 
 * @author Anuj Patel
 * @source goldenpackagebyanuj.blogspot.com
 */
public class DataSourceExample {
 public static void main(String[] args) {

  Hashtable<String, String> ht = new Hashtable<>();
  ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
  ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
  
  // Prepare Initial Context
  try {
   Context ctx = new InitialContext(ht);
   
   DataSource dataSource = (DataSource) ctx.lookup("DS/RetailStoreDS");
   Connection con = dataSource.getConnection();
   Statement stmt = con.createStatement();
   
   ResultSet rs = stmt.executeQuery("select * from retailstore.order");
   while(rs.next()){
    System.out.println("Order Id : " + rs.getString("Order_Id"));
    System.out.println("Order Status : " +  rs.getString("Order_Status"));
    System.out.println("OrderCreationDate : " + rs.getDate("Creation_Time"));
    System.out.println("ServiceRequrieDate : " + rs.getDate("ServiceRequire_Date"));
    System.out.println("--------------------------------------------------------");
   }
   
   stmt.close();
   con.close();
  } catch (NamingException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
}


Output


 
Order Id : Order2012912103137667
Order Status : New
OrderCreationDate : 2012-09-12
ServiceRequrieDate : 2012-09-12
--------------------------------------------------------
Order Id : Order2012912103228820
Order Status : New
OrderCreationDate : 2012-09-12
ServiceRequrieDate : 2012-09-12
--------------------------------------------------------
Order Id : Order2012912104540968
Order Status : New
OrderCreationDate : 2012-09-12
ServiceRequrieDate : 2012-09-12
--------------------------------------------------------
Order Id : Order201291531854563
Order Status : New
OrderCreationDate : 2012-09-15
ServiceRequrieDate : 2012-09-15
--------------------------------------------------------
Order Id : Order2012915103140138
Order Status : New
OrderCreationDate : 2012-09-15
ServiceRequrieDate : 2012-09-15
--------------------------------------------------------
If You liked my tutorials, You can subscribe my blog or share with your friends.

No comments:

Post a Comment