【Java】JNDI

来源:互联网 发布:安迅网络 编辑:程序博客网 时间:2024/06/14 10:20

有关概念参考:http://www.cnblogs.com/xdp-gacl/p/3951952.html

Tomcat配置数据源的方法

第一步: 在tomcat,配置文件 context.xml中加入:<Resource name="jdbc/newsDB" auth="Container" type="javax.sql.DataSource"maxActive="100"maxIdle="30" maxWait="10000"username="root" password="cs401" driverClassName="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost:3306/news"/>第二步,在web.xml配置资源引用s <resource-ref>      <res-ref-name>jdbc/newsDB</res-ref-name>      <res-type>javax.sql.DataSource</res-type>      <res-auth>Container</res-auth>  </resource-ref>第三步,在jsp或servlet中加入代码。 javax.naming.ContextContext initContext = new InitialContext();Context envContext  = (Context)initContext.lookup("java:/comp/env");DataSource ds = (DataSource)envContext.lookup("jdbc/newsDB");Connection conn = ds.getConnection();
原创粉丝点击