我先描述一下环境
环境::使用 Netbeans ID 配置 Netbeans 7.2 和 Tomcat 7.0.27.0
当我单独进行构建并将其放入 webapps
文件夹中并运行时,没有任何问题,但是当我在 IDE 本身中运行应用程序时,我得到 javax.naming.NameNotFoundException:名称 [jdbc/eswastha] 在此上下文中未绑定(bind)。无法找到[jdbc]。
此异常。
conf/context.xml
<Resource name="jdbc/eswastha" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
url="jdbc:mysql://localhost:3306/eswastha"
driverClassName="com.mysql.jdbc.Driver"
username="root" password="r14@17*" />
和 web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jjdbc/eswastha</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
和java类:
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class JDBCManager {
public Connection mysqlConnection() {
Connection dbConnection = null;
try {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/eswastha");
dbConnection = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return dbConnection;
}
}
请帮我找出问题所在。
问候
最佳答案
是不是不见了? 检查
<res-ref-name>jjdbc/eswastha</res-ref-name>
and
<Resource name="jdbc/eswastha"..../>
根据您的评论: 无法为连接 URL“null”创建类“”的 JDBC 驱动程序
确保您有 JDBC 驱动程序
,并将驱动程序 jar 文件复制到 tomcat-home/lib
下。
供您引用。 here
关于javax.naming.NameNotFoundException : Name [jdbc/eswastha] is not bound in this Context. 无法找到 [jdbc],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12928030/