今天用properties类连接数据库,总是找不到src根目录下的配置文件db.properties
我用的是MySql数据库
db.properties中的内容如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bbsPrj?useUnicode=true&characterEncoding=GBK
user=root
password=123456
读取db.properties配置文件的代码如下:
public final class ProReader extends Properties{
//定义ProReader类静态对象instance
private static ProReader instance;
/**
* 类的构造函数 读取属性文件
*/
private ProReader() {
//将属性文件读取到InputStream流中
InputStream is=getClass().getResourceAsStream("db.properties");
Properties p=new Properties();
try{
p.load(is); //从InputStream流中读取属性列表(键-值对)
//处理属性文件读取异常
}catch(Exception e){
System.err.println("错误:没有读取属性文件,"+"请确认db.properties文件是否存在");
return;
}
} /**
* 获取instance值
* @return
*/
public static ProReader getInstance(){
if(instance!=null){
return instance;
}else{
makeInstance();
return instance;
}
} /**
* 创建instance值
*/
private static synchronized void makeInstance() {
// TODO Auto-generated method stub
if(instance==null){
instance=new ProReader();
}
}
}
然后在DBConnection类中编写方法得到数据库连接
public static synchronized Connection getConn() throws DBAccessException{
//读出配置信息
String driverClassName=ProReader.getInstance().getProperty("driver");
String url=ProReader.getInstance().getProperty("url");
String user=ProReader.getInstance().getProperty("user");
String password=ProReader.getInstance().getProperty("password");
Connection conn=null;
try{
Class.forName(driverClassName);
conn=DriverManager.getConnection(url,user,password);
}catch(Exception ex){
throw new DBAccessException("不能取得数据库连接");
}
return conn;
}运行时怎么找不到db.properties文件呢?getConn()方法中的driverClassName之类字段值都是null
望各位仁兄指点一下。
我用的是MySql数据库
db.properties中的内容如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bbsPrj?useUnicode=true&characterEncoding=GBK
user=root
password=123456
读取db.properties配置文件的代码如下:
public final class ProReader extends Properties{
//定义ProReader类静态对象instance
private static ProReader instance;
/**
* 类的构造函数 读取属性文件
*/
private ProReader() {
//将属性文件读取到InputStream流中
InputStream is=getClass().getResourceAsStream("db.properties");
Properties p=new Properties();
try{
p.load(is); //从InputStream流中读取属性列表(键-值对)
//处理属性文件读取异常
}catch(Exception e){
System.err.println("错误:没有读取属性文件,"+"请确认db.properties文件是否存在");
return;
}
} /**
* 获取instance值
* @return
*/
public static ProReader getInstance(){
if(instance!=null){
return instance;
}else{
makeInstance();
return instance;
}
} /**
* 创建instance值
*/
private static synchronized void makeInstance() {
// TODO Auto-generated method stub
if(instance==null){
instance=new ProReader();
}
}
}
然后在DBConnection类中编写方法得到数据库连接
public static synchronized Connection getConn() throws DBAccessException{
//读出配置信息
String driverClassName=ProReader.getInstance().getProperty("driver");
String url=ProReader.getInstance().getProperty("url");
String user=ProReader.getInstance().getProperty("user");
String password=ProReader.getInstance().getProperty("password");
Connection conn=null;
try{
Class.forName(driverClassName);
conn=DriverManager.getConnection(url,user,password);
}catch(Exception ex){
throw new DBAccessException("不能取得数据库连接");
}
return conn;
}运行时怎么找不到db.properties文件呢?getConn()方法中的driverClassName之类字段值都是null
望各位仁兄指点一下。
String url=ProReader.getInstance().getProperty("url");
String user=ProReader.getInstance().getProperty("user");
String password=ProReader.getInstance().getProperty("password");
我吧上段代码改成
String driverClassName=ProReader.getInstance().getProperty("driver","com.mysql.jdbc.Driver");
String url=ProReader.getInstance().getProperty("url","jdbc:mysql://localhost:3306/restrant?useUnicode=true&characterEncoding=GBK");
String user=ProReader.getInstance().getProperty("user","root");
String password=ProReader.getInstance().getProperty("password","123456");
数据库连接成功 那就是说:“从db.properties中读取不到键值啊!
怎么回事啊?1楼能不能再说的详细些。
(1)文件放置的位置是否正确,deploy的时候是否copy过去了
(2)如果用Class.getResourceAsStream() 时,路径应该是以"/"开头的,"/"表示根路径(即classpath,如:工程文件中的classes下)
如果不是以"/"开头的,则表示是当前类的路径;3、如果可以找到db.properties文件,则检查db.properties文件中的key值是否正确,比如是否多空格或者有全角字符等;
你放到根目录下面,应该有个斜杠的吧,像这样InputStream is=getClass().getResourceAsStream("/db.properties");