Friday, December 31, 2010

Database connection using DataSource in Oracle using JNDI names, Server running on Weblogic 9.2

//Database connection using DataSource in Oracle using JNDI names, Server running on Weblogic 9.2:

JARs required:

classes12.jar
weblogic.jar
xbean_bea.jar

//Java code
import java.sql.Connection;
import java.util.Hashtable;

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

public class DataSourceConnection {
public static void main(String[] args) {
Connection connection = null;
try {
Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
jndiProperties.put("java.naming.provider.url", "t3://10.200.101.156:7001");

InitialContext context = new InitialContext(jndiProperties);
DataSource dataSource = (DataSource) context.lookup("jdbc/obodb_XA_ds");
connection = dataSource.getConnection();
System.out.println("Connection successful.."+connection.isReadOnly());

System.out.println(dataSource);
}
catch (Exception e) {
e.printStackTrace();
}
}
}

No comments:

Post a Comment