Following steps are in weblogic 12c.Screens may vary in another versions but everything
should be same as below.
1)Go to weblogic console
http://localhost:7001/console
2)Click on DataSources link
3)Click on New->GenericDatasource
4)Provide testDs in name text field and jndiName text field
5)Choose Database Type as oracle(If you want another database
choose that database name)
6)Choose oracle's driver thin
7)Click next next
8)Provide Database Name,hostname,port,username and password

9)Click TestConfiguration,you should get connection test succeded
10)Click next and don't forget to check the checkbox in the Servers section.
This step is very important don't forget

11)Click Finish
Now we see the code to get the datasource from weblogic jndi tree using JNDI code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hibex;
import java.sql.SQLException;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
/**
*
* @author Rishitha
*/
public class InitialContextEx {
private static InitialContext ctx = null;
public static void main(String args[]) throws NamingException, SQLException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
//If your weblogic is not running on 7001 provide that port
//no instead of 7001
env.put(Context.PROVIDER_URL,
"t3://localhost:7001");
ctx = new InitialContext(env);
//The string argument is jndi name for the datasource
//If you have provided another name for the datasource
//change testDs to the name you have given while configuring
//the datasource
DataSource ds = (DataSource) ctx.lookup("testHr");
System.out.println("Connection From JNDI DS: " + ds.getConnection());
}
}
Note:
1)provider_url:This specifies the the URL of the server whose jndi tree we want to
access.
Simply,it is the place where your datasource is configured.In our example
since we configured datasource in weblogic we have to provide the
port where weblogic is runnin.t3://localhost:7001.
2)INIITAL_CONTEXT_FACTORY:While connecting to weblogic it is
weblogic.jndi.WLInitialContextFactory.For other servers this class name
will change
3)Add weblogic.jar to classpath.weblogic.jar contains class WLInitialContextFactory.
And finally call getConnection on DataSource to get the jdbcconnection and from here
onwards everything is same....
should be same as below.
1)Go to weblogic console
http://localhost:7001/console
2)Click on DataSources link
3)Click on New->GenericDatasource
4)Provide testDs in name text field and jndiName text field
5)Choose Database Type as oracle(If you want another database
choose that database name)
6)Choose oracle's driver thin
7)Click next next
8)Provide Database Name,hostname,port,username and password
9)Click TestConfiguration,you should get connection test succeded
10)Click next and don't forget to check the checkbox in the Servers section.
This step is very important don't forget
11)Click Finish
Now we see the code to get the datasource from weblogic jndi tree using JNDI code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hibex;
import java.sql.SQLException;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
/**
*
* @author Rishitha
*/
public class InitialContextEx {
private static InitialContext ctx = null;
public static void main(String args[]) throws NamingException, SQLException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
//If your weblogic is not running on 7001 provide that port
//no instead of 7001
env.put(Context.PROVIDER_URL,
"t3://localhost:7001");
ctx = new InitialContext(env);
//The string argument is jndi name for the datasource
//If you have provided another name for the datasource
//change testDs to the name you have given while configuring
//the datasource
DataSource ds = (DataSource) ctx.lookup("testHr");
System.out.println("Connection From JNDI DS: " + ds.getConnection());
}
}
Note:
1)provider_url:This specifies the the URL of the server whose jndi tree we want to
access.
Simply,it is the place where your datasource is configured.In our example
since we configured datasource in weblogic we have to provide the
port where weblogic is runnin.t3://localhost:7001.
2)INIITAL_CONTEXT_FACTORY:While connecting to weblogic it is
weblogic.jndi.WLInitialContextFactory.For other servers this class name
will change
3)Add weblogic.jar to classpath.weblogic.jar contains class WLInitialContextFactory.
And finally call getConnection on DataSource to get the jdbcconnection and from here
onwards everything is same....
No comments:
Post a Comment