java学习:weblogic下JNDI及JDBC连接测试(weblogic环境)
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" ); ht.put(Context.PROVIDER_URL, "t3://localhost:7001" ); 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" ); ht.put(Context.PROVIDER_URL, "t3://localhost:7001" ); 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 webLogicServer = "t3://localhost:7001" ; //weblogic服务器地址 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数据源的方法
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:
15.Java/Scala
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· Open-Sora 2.0 重磅开源!
2010-11-15 电子商务之购物车杂谈