我写了一个数据库连接类:
连接函数如下,我把他放在WEB-INF/classes/data/目录下
但是我不想让
InputStream in=new FileInputStream("E:/sql.properties");中的
E:/sql.properties写成绝对路径,如写成/WEB-INF/sql.properties
但是在jsp页面调用数据库的时候提示找不到路径,我该怎么写呢??
但是不能写成绝对路径,请大家帮忙public static synchronized Connection getCon()throws Exception{
try{
Properties props=new Properties();
InputStream in=new FileInputStream("E:/sql.properties");
props.load(in);
in.close();
/**
*加载驱动程序
*/
Class.forName(props.getProperty("connection.driver"));
/**
*获取数据库连接
*/
con =DriverManager.getConnection(
props.getProperty("connection.url"),props);
return con;
}catch (SQLException e){
System.out.println(e.getMessage());
throw e;
}
}
连接函数如下,我把他放在WEB-INF/classes/data/目录下
但是我不想让
InputStream in=new FileInputStream("E:/sql.properties");中的
E:/sql.properties写成绝对路径,如写成/WEB-INF/sql.properties
但是在jsp页面调用数据库的时候提示找不到路径,我该怎么写呢??
但是不能写成绝对路径,请大家帮忙public static synchronized Connection getCon()throws Exception{
try{
Properties props=new Properties();
InputStream in=new FileInputStream("E:/sql.properties");
props.load(in);
in.close();
/**
*加载驱动程序
*/
Class.forName(props.getProperty("connection.driver"));
/**
*获取数据库连接
*/
con =DriverManager.getConnection(
props.getProperty("connection.url"),props);
return con;
}catch (SQLException e){
System.out.println(e.getMessage());
throw e;
}
}
你得相对路径得基准路径就应该是tomcat得bin目录。比如%TOMCAT_HOME%\bin\test\test.jsp得相对路径就是./test/test.jsp
ResourceBundle bundle=ResourceBundle.getBundle("sql");//sql.properties要放在classes目录下面。
System.out.println(bundle.getString("键名"));
是不是改成ResourceBundle bundle=ResourceBundle.getBundle("sql.properties");
我把sql.properties放在/classes/com/jac/database/下,com.jac.database是类的包名
但是tomcat下提示
Can't find bundle for base name sql.properties, locale zh_CN
这是怎么回事??
我试了不可以
String res="sql.properties"
ClassLoader cl=LoadResources.class.getClassLoader();
InputStream is=cl.getResourceAsStream(res);
Properties props=new Properties();
props.load(is);
测试一般应用程序是可以的,但就是用到jsp里就不行了