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");


    }

}

Popular Posts