【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”
我讲c3p0-config.xml放在src目录下,程序以app运行没有错误,但是以web server运行,就会解析不到配置~官网上有以下一段话:
By default, c3p0 will look for an XML configuration file in its classloader's resource path under the name "/c3p0-config.xml". That means the XML file should be placed in a directly or jar file directly named in your applications CLASSPATH, in WEB-INF/classes, or some similar location. If you prefer not to bundle your configuration with your code, you can specify an ordinary filesystem location for c3p0's configuration file via the system property com.mchange.v2.c3p0.cfg.xml.
意思是配置文件必须放在classpath路径中,在web环境下就是classes,这个目录中也就是src中的java文件编译过来的,按照上面的说明试过了,还是不行,听说oschina使用c3p0做数据连接,求教大家,这个问题怎么解决?
引用来自“红薯”的答案
import java.io.InputStream;
import java.util.Properties;
public class DBConfig
{
private static String db_congfig = "db_config.properties";
public static String DRIVERS = null;
public static String URL = null;
public static String USER = null;
public static String PASSWORD = null;
static
{
Properties props = new Properties();
InputStream is = null;
try
{
is = ClassLoader.getSystemResourceAsStream(db_congfig);
props.load(is);
DRIVERS = props.getProperty("drivers");
URL = props.getProperty("url");
USER = props.getProperty("user");
PASSWORD = props.getProperty("password");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
我用的是Properties文件。
XML的话就用:
System.setProperty("com.mchange.v2.c3p0.cfg.xml","configs/c3p0-config.xml");
参考这里:
http://blog.csdn.net/willability/article/details/8467515