JNDI的专业解释,大家自行去网络搜索吧,这里就不啰嗦了。
单纯从使用角度看,可以简称把它看成一个key-value的“哈希资源”容器。给定一个string类型的key,可以把任何类型的value,放入这个容器(通过bind/rebind方法);其它地方需要使用该资源时,根据key就能取出该资源(通过lookup方法)
JNDI使用示例:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package jmyang.weblogic; /** * <p>Title:JNDI示例(WebLogic环境) </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2012</p> * <p>Company:cnblogs </p> * @菩提下的杨过 * @version 1.0 */ import javax.naming.*; import java.util.Hashtable; public class JNDITest { static Context ctx = null ; public static void test() { String key = "jmyang" ; //先绑定 bind(key, "杨俊明" ); //再取出来 String value = (String) lookUp(key); System. out .print(key + "=" + value); } /* *绑定 */ public static void bind(String name, String object ) { Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" ); try { ctx = new InitialContext(ht); ctx.rebind(name, object ); } catch (NamingException e) { e.printStackTrace(); } finally { try { ctx.close(); } catch (Exception e) { e.printStackTrace(); } } } /* * */ public static Object lookUp(String name) { Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" ); try { ctx = new InitialContext(ht); Object object = ctx.lookup(name); return object ; } catch (Exception e) {} finally { try { ctx.close(); } catch (Exception e) { e.printStackTrace(); } } return null ; } } |
上述代码执行完以后,也可能通过weblogic控制台,查看jndi树来验证:
JDBC数据源,实际上,也是使用JNDI服务来访问的,下面是JDBC示例代码:(必须先在weblogic中创建数据源)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package jmyang.weblogic; /** * <p>Title:JDBC示例(WebLogic环境) </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2012</p> * <p>Company:cnblogs </p> * @菩提下的杨过 * @version 1.0 */ import java.sql.*; import javax.naming.*; import javax.sql.*; import java.util.Hashtable; public class JDBCTest { static final String webLogicINDIStr = "weblogic.jndi.WLInitialContextFactory" ; public static void test() { Connection myConn = null ; DataSource ds = null ; Context ctx = null ; Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY, webLogicINDIStr); ht.put(Context.PROVIDER_URL, webLogicServer); try { ctx = new InitialContext(ht); ds = (javax.sql.DataSource) ctx.lookup( "infoskysso" ); //取得名为infoskysso的数据源(注:infoskysso要在weblogic中设置数据源) } catch (NamingException e) { e.printStackTrace(); } Statement myStatement = null ; ResultSet myResultSet = null ; try { myConn = ds.getConnection(); //建立连接 myStatement = myConn.createStatement(); myResultSet = myStatement.executeQuery( "Select DEPTNO From DEPT where rownum<=10" ); while (myResultSet.next()) { System. out .println( "DEPTNO=" + myResultSet.getInt( "DEPTNO" )); } myResultSet.close(); } catch (SQLException e) { System. out .println( "Error code = " + e.getErrorCode()); System. out .println( "Error message = " + e.getMessage()); } finally { //关闭查询 if (myStatement != null ) { try { myStatement.close(); } catch (SQLException e) { System. out .println( "Error code = " + e.getErrorCode()); System. out .println( "Error message = " + e.getMessage()); } } //关闭连接 if (myConn != null ) { try { myConn.close(); } catch (SQLException e) { System. out .println( "Error code = " + e.getErrorCode()); System. out .println( "Error message = " + e.getMessage()); } } } } } |
附: weblogic中创建jdbc数据源的方法