develog

jndi test 2 본문

Dev/Java

jndi test 2

냐옴 2013. 12. 11. 13:57

commons-dbcp-1.4.jar

commons-pool-1.6.jar

fscontext-4.4.jar

ojdbc14.jar

 

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

public class TestJndi {

    public static void main(String[] args) throws Exception {
        System.out.println("START");
        System.out.println("-------------------------------------------");
        TestJndi main = new TestJndi();
        main.setUpJndi();
        main.testJndi();
        System.out.println("-------------------------------------------");
        System.out.println("END");
    }

    public void testJndi() throws Exception {
        InitialContext ic = new InitialContext();
        DataSource ds = null;
        ds = (DataSource) ic.lookup("jdbc/oracle");
        System.out.println(ds.getConnection());
        ds = (DataSource) ic.lookup("jdbc/mysql");
        System.out.println(ds.getConnection());
    }

    public void setUpJndi() throws Exception {
        setUpJndiTemplate("jdbc/oracle", "oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@127.0.0.1:1521:ORCL",
                "scott", "tiger");
        setUpJndiTemplate("jdbc/mysql", "com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1/myDbSchema", "scott", "tiger");
    }

    private void setUpJndiTemplate(String jndiName, String driverClassName, String url, String username, String password) throws NamingException {
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
        Reference ref = new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null);
        ref.add(new StringRefAddr("driverClassName", driverClassName));
        ref.add(new StringRefAddr("url", url));
        ref.add(new StringRefAddr("username", username));
        ref.add(new StringRefAddr("password", password));
        InitialContext ic = new InitialContext();
        ic.rebind(jndiName, ref);
    }

}

 

 package test;  import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.naming.Reference; import javax.naming.StringRefAddr; import javax.sql.DataSource;  public class TestJndi {  	public static void main(String[] args) throws Exception { 		System.out.println("START"); 		System.out.println("-------------------------------------------"); 		 		TestJndi main = new TestJndi(); 		main.setUpJndi(); 		main.testJndi(); 		 		System.out.println("-------------------------------------------"); 		System.out.println("END"); 	} 	 	public void testJndi() throws Exception { 		InitialContext ic = new InitialContext(); 		DataSource ds = null; 		 		ds = (DataSource) ic.lookup("jdbc/oracle"); 		System.out.println(ds.getConnection()); 		 		ds = (DataSource) ic.lookup("jdbc/mysql"); 		System.out.println(ds.getConnection()); 	} 	 	public void setUpJndi() throws Exception { 		setUpJndiTemplate("jdbc/oracle", 				"oracle.jdbc.driver.OracleDriver", 				"jdbc:oracle:thin:@127.0.0.1:1521:ORCL", 				"scott", 				"tiger"); 		 		setUpJndiTemplate("jdbc/mysql", 				"com.mysql.jdbc.Driver", 				"jdbc:mysql://127.0.0.1/myDbSchema", 				"scott", 				"tiger"); 	} 	 	private void setUpJndiTemplate(String jndiName, String driverClassName, String url, String username, String password) throws NamingException { 		System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); 		 		Reference ref = new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null); 		ref.add(new StringRefAddr("driverClassName", driverClassName)); 		ref.add(new StringRefAddr("url", url)); 		ref.add(new StringRefAddr("username", username)); 		ref.add(new StringRefAddr("password", password)); 		 		InitialContext ic = new InitialContext(); 		ic.rebind(jndiName, ref); 	}  } 

'Dev > Java' 카테고리의 다른 글

Spring 테스트 Log4j 초기화  (0) 2013.12.11
Quartz test  (0) 2013.12.11
applicationContext.xml, sqlMapConfig, configLocation (classpath or file)  (0) 2013.12.09
@ContextConfiguration  (0) 2013.12.09
tail  (0) 2013.12.05
Comments