TestDSLookUp.java Source code

Java tutorial

Introduction

Here is the source code for TestDSLookUp.java

Source

/*
    
Java Programming with Oracle JDBC
by Donald Bales 
ISBN: 059600088X
Publisher: O'Reilly
    
*/

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class TestDSLookUp {

    public static void main(String[] args) throws SQLException, NamingException {

        Context ctx = null;
        try {
            Properties prop = new Properties();
            prop.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
            prop.setProperty(Context.PROVIDER_URL, "file:/JNDI/JDBC");
            ctx = new InitialContext(prop);
        } catch (NamingException ne) {
            System.err.println(ne.getMessage());
        }

        DataSource ds = (DataSource) ctx.lookup("joe");
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery(
                "select 'Hello Thin driver data source tester '||" + "initcap(USER)||'!' result from dual");
        if (rset.next())
            System.out.println(rset.getString(1));
        rset.close();
        stmt.close();
        conn.close();
    }
}