【Java】JNDI

有关概念参考: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.Context

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/newsDB");

Connection conn = ds.getConnection();

猜你喜欢

转载自blog.csdn.net/jiojio_/article/details/75045072