Friday 28 August 2015

How to create a DataSource in WebLogic Server 11g console.



In this post you will learn how to create a Datasource in the Weblogic server 11g console.

To achieve this follow below steps:

1.  Login to Weblogic server console using http://<hostname>:<port>/console
2. Expand Services and click on Data Sources
3. Click on New and select Generic Data Source
 












4 Enter below entries and click Next











 
5. Keep default value and click Next


 









 6. Click Next












  7. Enter below entries according to your DB details and click Next












8. Click on Test Configuration, if below entries are correct then Connection would get succeeded and click Next












9. Select Server on which you want to deploy datasource. In this case we have only DefaultServer, click DefaultServer and click Finish.












10. testDS will get created and same can be used to get database connection.
 



Thursday 27 August 2015

Configure Oracle DataSource in Tomcat

Here is the procedure, how to configure Oracle datasource in Tomcat and lookup the same.

     1. Get Oracle driver

Download Oracle driver from here and copy it to $TOMCAT\lib folder

     2. Create META-INF/context.xml and add following entry.

 <?xml version="1.0" encoding="windows-1252" ?>
<Context override="true">

    <Resource name="jdbc/hr" auth="Container"
        type="javax.sql.DataSource" maxActive="100" maxIdle="30"
        maxWait="10000" username="hr" password="welcome1"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@localhost:1521:orcl" />   
    </Context>



    3. web.xml configuration

<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
   
  <resource-ref>
    <description>Oracle Datasource</description>
    <res-ref-name>jdbc/hr</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
</web-app>



  4. Lookup datasource via context lookup service

Below class lookup datasource and established connection with Oracle DB

import javax.naming.Context;
import javax.naming.InitialContext;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class GetConnection {

    private static DataSource ds;

    public static DataSource getDS() {
        try {
            Context ctx = new InitialContext();
            ds = (DataSource)ctx.lookup("java:comp/env/jdbc/hr");
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return ds;
    }

    public static void main(String[] args) {
        try {

            DataSource ds = GetConnection.getDS();

            Connection connection = ds.getConnection();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }

}




ADF BC With No Database Connection

Sometimes user need programmatic View object with no database connection.

If you want to disable database access for ADF BC application, you will need to update bc4j.xcf. I would recommend to update this file from outside, not from JDeveloper - otherwise you may experience errors - seems like JDeveloper IDE is constantly scanning changes from bc4j.xcfg. Make sure to update bc4j.xcfg, for example RequiresConnection=false





Update <Custom JDBCDataSource="java:comp/env/jdbc/Connection1DS"/> with <Custom RequiresConnection="false" password=""/> at both the places. 

Get Current Logged in User

Most of the time we need to get current logged in user in ADF application.

Below is code snippet to get current user:


public static String getCurrentUser(){
        String userName = ADFContext.getCurrent().getSecurityContext().getUserName();
       
        return userName;
    }

Get Current Date using Java

Below code give current date in String.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class CurrentDate {
    public CurrentDate() {
        super();
    }

    public static void main(String[] args) {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                Calendar cal = Calendar.getInstance();             
                System.out.println(dateFormat.format(cal.getTime()));
    }
}

Current Date in GregorianCalendar using java

Code snippet that will give Current date in GregorianCalendar format:


import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class CurrentDate {
    public CurrentDate() {
        super();
    }

    public static void main(String[] args) {
            XMLGregorianCalendar xgcal = null;
                try {
                    GregorianCalendar gcal =
                        (GregorianCalendar)GregorianCalendar.getInstance();
                     xgcal =
                        DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
                    System.out.println("xgcal "+xgcal.toString());
                } catch (DatatypeConfigurationException dce) {
                   System.out.println("Exception occurred while getting XMLGregorianCalendar "+dce);
                 }
    }
}

Wednesday 26 August 2015

Java Jndi datasource lookup

JNDI style is typical when using an application server(Weblogic, JBOSS) or a web container(Apache Tomcat).

Here's a example of the lookup Datasource created in Server.

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DatabaseConnection {
    public DatabaseConnection() {
        super();
    }
    // JNDI name created in Server

    final static String DATA_SOURCE="jdbc/PortalDS";
    public static void main(String[] args) {

        Connection connection = DatabaseConnection.init(DATA_SOURCE);
       
        System.out.println("Connection "+connection);

    }
   
    private static Connection init(String dsName) {
              Connection con = null;
              DataSource datasource = null;
      
              Context initialContext = null;
              try{
                initialContext = new InitialContext();
                if (initialContext == null) {
                }
                datasource = (DataSource)initialContext.lookup(dsName);
                if (datasource != null) {
                     con = datasource.getConnection();
                } else {
                    System.out.println("Failed to lookup datasource.");

                }
              }catch (SQLException sqle) {
                     System.out.println("SQL Exception occurred while getting DataSource : " +
                               sqle);
                    
                 } catch (NamingException ne) {
                     System.out.println("Naming Exception occurred while getting DataSource : " +
                               ne);
               

                 }
            
              return con;
          }
}