[英]jdbc javax.naming.NameNotFoundException
I have been having a problem with this exception for a long time. 很长时间以来,我一直对此异常有疑问。 I am trying to connect to an external mysql database but I keep getting this exception.
我正在尝试连接到外部mysql数据库,但我不断收到此异常。 I have looked at many examples and I still have not found the answer.
我看了很多例子,但仍然没有找到答案。 Here is my code, please bare in mind that this is all the code I use for this so please say if I've missed something out:
这是我的代码,请记住这是我使用的所有代码,因此请说出是否错过了一些事情:
<?xml version="1.0" encoding="UTF-8"?>
<context>
<!-- Registers Database with JNDI Naming Service as a pooled DataSource -->
<Resource
name="jdbc/DBNAME"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
validationInterval="30000"
timeBetweenEvictionRunsMillis="30000"
maxActive="100"
minIdle="10"
maxWait="10000"
initialSize="10"
removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
jmxEnabled="true"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
username="****"
password="********"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://****:3306/****"/>
</context>
And here is my code for calling it: 这是我调用它的代码:
try
{
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/DBNAME");
Connection c = ds.getConnection();
return c;
}
catch(NamingException ne)
{
System.out.println(ne.toString());
return null;
}
catch (SQLException se)
{
System.out.println(se.toString());
return null;
}
Usernames, passwords, db urls and connection names have been redacted but the rest is correct. 用户名,密码,数据库url和连接名已被删除,但其余都是正确的。
From Tomcat doc at: 从Tomcat文档中:
http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
Try using this code: 尝试使用以下代码:
Context ic= new InitialContext();
Context envCtx = (Context) ic.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/DBNAME");
Regards. 问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.