import java.sql.*;
import java.util.Hashtable;
import javax.naming.*;

/*
* This code is for test purposes only.
*/

public class MultiDSTest {

    public static void main(String args[]) {

        // NOTE: change the values of these Strings
        //       to match your system
        String serverHost = "localhost";
        String serverPort = "7001";
        String dbusername = "system";
        String dbpassword = "oracle_4U";
        String dsname = "jdbc/racDS";

        Hashtable env = new Hashtable();

        // put information into the Hashtable to be able to 
        // retrieve the server's initial context
        env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        env.put(Context.PROVIDER_URL, "t3://" + serverHost + ":" + serverPort);
        env.put("user", dbusername); 
        env.put("password", dbpassword);

        String information = "";

        try {
            // get initial context of server
            Context context = new InitialContext(env);
            // retrieve the multi data source by its JNDI name
            javax.sql.DataSource ds =
                (javax.sql.DataSource) context.lookup(dsname);
            // get a db connection from the multi data source
            Connection  connection = ds.getConnection();
	    // from the connection create a statement
            Statement   statement = connection.createStatement();
            ResultSet   resultSet;
            String      sql;

            System.out.println("Got a connection from...");

            // do a select to see which db instance, which host, what service
            sql = "select sys_context('userenv', 'instance_name'), sys_context('userenv', 'server_host'), sys_context('userenv', 'service_name') from dual";
            resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                information  = resultSet.getString(1) + "   " + resultSet.getString(2) + "   " + resultSet.getString(3);
            }
            // print out the information
	    System.out.println(information);
	    
            statement.close();
            resultSet.close();
            connection.close();
        } catch (Exception e) {
            System.out.println("Error:" + e);
        }
    }
}
