NoInitialContextException

时间:2012-06-22 07:01:08

标签: java tomcat jdbc database-connection jndi

我有问题。 我想使用JDBC连接到数据库,我有Tomcat服务器。为此,我使用连接池。

根据互联网教程,我写道: context.xml中:

<?xml version="1.0" encoding="UTF-8"?>

<Context antiJARLocking="true" path="/Server" docBase="dbcp" debug="5"
         reloadable="true" crossContext="true">

    <Resource name="jdbc/TestDB" auth="Container"
          type="javax.sql.DataSource" removeAbandoned="true"
          removeAbandonedTimeout="30" maxActive="100"
          maxIdle="30" maxWait="10000" username="root"
          password="newpass"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/delta_server"/>

</Context>

web.xml:

<resource-ref>
      <description>DB Connection Pooling</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

我尝试连接......

Connection conn=null;
DataSource ds;
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:comp/env");
ds = (DataSource) envContext.lookup("jdbc/TestDB");
conn = ds.getConnection();

但是我弄错了: javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或在applet参数中或在应用程序资源文件中指定类名:java.naming.factory.initial

怎么做???

0 个答案:

没有答案