Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 단축키
- Eclipse
- JavaScript
- web.xml
- context
- netsh
- maVen
- GIT
- IntelliJ
- profile
- xargs
- find
- bash
- port
- Windows
- resource
- 네트워크
- Quartz
- VirtualBox
- Mac
- vscode
- lsof
- Source
- plugin
- ssh
- grep
- Windows 10
- tomcat
- 줄바꿈 문자
- import
Archives
- Today
- Total
develog
jndi test 본문
commons-dbcp-1.4.jar
commons-pool-1.6.jar
fscontext-4.4.jar
ojdbc14.jar
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import javax.sql.DataSource;
public class TestJndi {
public static void main(String[] args) {
Logger.info("START");
Logger.info("------------------------------------------------");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
try {
conn = getConnection();
sql = new String("SELECT 456 CNT FROM DUAL");
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
System.out.println(rs.getString("CNT"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeStatement(pstmt, rs);
closeConnection(conn);
}
Logger.info("------------------------------------------------");
Logger.info("END");
}
private static void setUpJndi() throws Exception {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
// System.setProperty(Context.PROVIDER_URL, "file:///jndiTemp");
InitialContext ic = new InitialContext();
Reference ref = null;
ref = new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null);
ref.add(new StringRefAddr("driverClassName", "oracle.jdbc.driver.OracleDriver"));
ref.add(new StringRefAddr("url", "jdbc:oracle:thin:@127.0.0.1:1521:ORCL"));
ref.add(new StringRefAddr("username", "scott"));
ref.add(new StringRefAddr("password", "tiger"));
ref.add(new StringRefAddr("maxActive", "1"));
ref.add(new StringRefAddr("maxWait", "1"));
ic.rebind("jdbc/dev1", ref);
ref = new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null);
ref.add(new StringRefAddr("driverClassName", "oracle.jdbc.driver.OracleDriver"));
ref.add(new StringRefAddr("url", "jdbc:oracle:thin:@127.0.0.2:1521:ORCL"));
ref.add(new StringRefAddr("username", "scott"));
ref.add(new StringRefAddr("password", "tiger"));
ref.add(new StringRefAddr("maxActive", "1"));
ref.add(new StringRefAddr("maxWait", "1"));
ic.rebind("jdbc/dev2", ref);
}
public static Connection getConnection() throws Exception {
setUpJndi();
return getConnectiondev1();
}
private static Connection getConnectiondev1() throws Exception {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("jdbc/dev1");
return ds.getConnection();
}
private static Connection getConnectiondev2() throws Exception {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("jdbc/dev2");
return ds.getConnection();
}
public static void closeStatement(PreparedStatement pstmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (Exception e) {
}
}
if (pstmt != null) {
try {
pstmt.close();
pstmt = null;
} catch (Exception e) {
}
}
}
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
conn = null;
} catch (Exception e) {
}
}
}
}
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.Reference; import javax.naming.StringRefAddr; import javax.sql.DataSource; public class TestJndi { public static void main(String[] args) { Logger.info("START"); Logger.info("------------------------------------------------"); Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String sql = null; try { conn = getConnection(); sql = new String("SELECT 456 CNT FROM DUAL"); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if (rs.next()) { System.out.println(rs.getString("CNT")); } } catch (Exception e) { e.printStackTrace(); } finally { closeStatement(pstmt, rs); closeConnection(conn); } Logger.info("------------------------------------------------"); Logger.info("END"); } private static void setUpJndi() throws Exception { System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); // System.setProperty(Context.PROVIDER_URL, "file:///jndiTemp"); InitialContext ic = new InitialContext(); Reference ref = null; ref = new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null); ref.add(new StringRefAddr("driverClassName", "oracle.jdbc.driver.OracleDriver")); ref.add(new StringRefAddr("url", "jdbc:oracle:thin:@127.0.0.1:1521:ORCL")); ref.add(new StringRefAddr("username", "scott")); ref.add(new StringRefAddr("password", "tiger")); ref.add(new StringRefAddr("maxActive", "1")); ref.add(new StringRefAddr("maxWait", "1")); ic.rebind("jdbc/dev1", ref); ref = new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory", null); ref.add(new StringRefAddr("driverClassName", "oracle.jdbc.driver.OracleDriver")); ref.add(new StringRefAddr("url", "jdbc:oracle:thin:@127.0.0.2:1521:ORCL")); ref.add(new StringRefAddr("username", "scott")); ref.add(new StringRefAddr("password", "tiger")); ref.add(new StringRefAddr("maxActive", "1")); ref.add(new StringRefAddr("maxWait", "1")); ic.rebind("jdbc/dev2", ref); } public static Connection getConnection() throws Exception { setUpJndi(); return getConnectiondev1(); } private static Connection getConnectiondev1() throws Exception { InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup("jdbc/dev1"); return ds.getConnection(); } private static Connection getConnectiondev2() throws Exception { InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup("jdbc/dev2"); return ds.getConnection(); } public static void closeStatement(PreparedStatement pstmt, ResultSet rs) { if (rs != null) { try { rs.close(); rs = null; } catch (Exception e) { } } if (pstmt != null) { try { pstmt.close(); pstmt = null; } catch (Exception e) { } } } public static void closeConnection(Connection conn) { if (conn != null) { try { conn.close(); conn = null; } catch (Exception e) { } } } }
'Dev > Java' 카테고리의 다른 글
자바 web to web 통신 (0) | 2013.05.07 |
---|---|
synchronized (0) | 2013.03.17 |
dbcp test (0) | 2013.03.01 |
variable arguments (0) | 2013.02.23 |
XML DOM (0) | 2013.01.27 |
Comments