Thi is a example with datasource with oracle

//Server
package datasource;

import java.sql.*;
import javax.sql.*;
import oracle.jdbc.pool.OracleDataSource;
import javax.naming.*;
import java.util.*;

/**
 * A class to create a DataSource and bind it to a directory.
 */
public class DataSourceServer {
  static ResourceBundle bundle = null;

  public static void main(String[] args) {
    bundle = ResourceBundle.getBundle("DataSource");
    try {

      // create and store parameters which are used to create the
context
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY,
              bundle.getString("datasource.factory"));
      env.put(Context.PROVIDER_URL, bundle.getString
("datasource.url"));

      // create the context
      Context context = new InitialContext(env);

      // Create a DataSource object
      OracleDataSource dataSource = new OracleDataSource();

      // set the connection parameters
      String s = bundle.getString("datasource.username");
      dataSource.setUser(s);
      s = bundle.getString("datasource.password");
      dataSource.setPassword(s);
      s = bundle.getString("datasource.drivertype");
      dataSource.setDriverType(s);
      s = bundle.getString("datasource.netprotocol");
      dataSource.setNetworkProtocol(s);
      s = bundle.getString("datasource.server");
      dataSource.setServerName(s);
      dataSource.setPortNumber(getPort());
      s = bundle.getString("datasource.databasename");
      dataSource.setDatabaseName(s);

      // get the name
      String bindName = bundle.getString("datasource.bindname");

      // bind the DataSource with the name
      context.rebind(bindName, dataSource);
      System.out.println("DataSource completed");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  static int getPort() throws NumberFormatException {
    String s = bundle.getString("datasource.port");
    return Integer.parseInt(s);
  }
}

//Client
package datasource;

import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.util.*;
import oracle.jdbc.pool.OracleDataSource;

public class DataSourceClient {
  Connection conn;
  static ResourceBundle bundle = ResourceBundle.getBundle
("MusicStore");//schema
  BasicDataSource dataSource;

  public DataSourceClient() {
    Context context;

    try {

      // create and store parameters which are used to create the
context
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY,
              bundle.getString("datasource.factory"));
      env.put(Context.PROVIDER_URL, bundle.getString
("datasource.url"));

      // create the context
      context = new InitialContext(env);

      // call method to get DataSource and Connection
      String bindName = bundle.getString("datasource.bindname");
      dataSource = (BasicDataSource) context.lookup(bindName);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public Connection getConnection() throws SQLException {
    return dataSource.getConnection();
  }
}


On 17 ago, 23:23, pathak.ni...@gmail.com wrote:
> Datasource has properties which need to be altered for different containers  
> (container specific for physical connections in most of the cases), where  
> our datasource instance will be registered through JNDI API. There are  
> various ways for setting the datasource in Tomcat or WebSphere for that  
> matter. What is your requirement?
>
> On Aug 18, 2009 5:47am, raja sekhar <rajsekhartiruveedh...@gmail.com> wrote:
>
> > Hi friends, My application requires connection pooling to database. For  
> > that I am going for DataSource. Can any one help/guide me how to use with  
> > simple program kind of the thing
> > Thanks in advance
> > --
> > Raja.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Java EE (J2EE) Programming with Passion!" group.
To post to this group, send email to
java-ee-j2ee-programming-with-passion@googlegroups.com
To unsubscribe from this group, send email to
java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to