通过后台管理界面添加连接池后,访问还是有问题
最后发现是个全局资源的问题
连接全局资源到本Context即可。
这样就不会出现找不到驱动和URL了
E:/tomcat/conf/Catalina/localhost/myweb.xml
里边修改ResourceLink为:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="myweb" path="/myweb" workDir="work/Catalina/localhost/myweb">
<!--<Resource auth="Container" description="My Datasource" name="jdbc/InfoDS" type="javax.sql.DataSource"/>-->
<ResourceLink name="jdbc/InfoDS" type="javax.sql.DataSource" global="jdbc/InfoDS"/>
</Context>
接下来在Bean里初始化连接池
-------------------------------------------------------------------------------------
/**
* 初始化DataSource
*/
private void init() {
InputStream inputstream = getClass().getResourceAsStream("./system.properties");
Properties properties = new Properties();
try {
properties.load(inputstream);
}
catch (Exception ex) {
log("Can't read the properties file. Make sure system.properties is in the CLASSPATH");
}
System.out.println(properties.getProperty("DATASOURCE"));
try {
initCtx = new InitialContext();
myds = (DataSource) initCtx.lookup(properties.getProperty("DATASOURCE"));
}
catch (Exception e) {
e.printStackTrace();
log("获取DataSource jndi错误:" + e.getMessage());
}
finally {
try {
if (initCtx != null)
initCtx.close();
initCtx = null;
}
catch (NamingException e){
e.printStackTrace();
log("关闭initCtx错误:" + e.getMessage());
}
}
}
---------------------------------------------------------------------------------
system.properties 内容
[DATABASE]
DATASOURCE=java:comp/env/jdbc/InfoDS