我有这个Java代码
我的属性文件具有以下内容:jndi.lookup.base = jdbc:mysql://localhost:3306/mydb
public static Connection getDBConnection(String jndiName) throws NamingException, SQLException {
Context initContext = new InitialContext();
Context webContext = (Context)initContext.lookup(MyProperties.getInstance().getProperty("jndi.lookup.base", "java:comp/env"));
DataSource ds = (DataSource) webContext.lookup(jndiName);
return ds.getConnection();
}
我收到此错误:
javax.naming.NameNotFoundException:名称[jdbc:mysql:// localhost:3306 / mydb]在此上下文中未绑定。找不到[jdbc:mysql:]。
我在Internet上看到了一些内容,看来我需要在Tomcat的context.xml中添加一些内容。怎么样?
我的context.xml
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->