How to use datasource from weblogic/ How to access datasource from weblogic/ Type 3 connectivity

This program demonstrate , how to connect a java program to database using middle ware. this connectivity is known as type 3 connectivity and can be done using middle ware server  that provides the facilities to create datasource.
 Here , I am using weblogic 10.3 to create datasource and accessing MySql through core java application. This application will connect to abc data source created on weblogic and get Connection from datasource.
then do whatever we does on type 1 connectivity.
To run this program, you have to put weblogic.jar file into class-path otherwise it will create run-time problem.
Example :

import javax.sql.*;

import java.sql.*;

import java.util.*;

import javax.naming.*;

class Type4Conn{

    public static void main(String ar[])throws Exception{

        Properties p=new Properties();

        p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");

        p.put(Context.PROVIDER_URL,"t3://localhost:7001");

        InitialContext ctx=new InitialContext(p);

        DataSource ds=(DataSource)ctx.lookup("abc");

        

        Connection con=ds.getConnection();

        Statement st=con.createStatement();

        int a=st.executeUpdate("insert into user values('test','testuser',2)");

        System.out.println(a+" records inserted");


    }

}

2 comments:

Anonymous said...

Hello. im having problem in connecting my oracle web logic server.

this is my error, how can i resolved this im new in web logic.

Exception in thread "main" java.lang.ClassNotFoundException: weblogic.jndi.rmi.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.rest.client.Type4Conn.main(Type4Conn.java:27)

Hemraj said...

Add weblogic.jar in classpath.

Popular Posts