如何从properties配置文件中对去jdbc链接配置
2009-11-24 16:30
686 查看
我们在读取jdbc链接时,通常将链接信息写死在java文件中,或者从数据库链接池中读取。其实还有另外一种方法,将数据库链接信息写到properties文件中。这样做的好处是如果将来需要更换数据库,只需要修改properties文件,并且不需要重新编译即可执行。
例如有一数据库配置文件db.properties。其中的内容如下:
DBDriver=com.mysql.jdbc.Driver
Connection=jdbc:mysql://127.0.0.1:3306/user
User=root
Password=root
建立一个单例模式的类,用于获得数据库链接。
public class DBConnection {
private static DBConnection instance;
public static synchronized Connection getConnection() throws Exception{
if(instance==null){
instance=new DBConnection();
}
return instance._getConnection();
}
private DBConnection() {
}
public Connection _getConnection() throws Exception {
String sDBDriver=null;
String sConnection=null;
String sUser=null;
String sPassword=null;
Properties p=new Properties();
//db.properties放在cn.com.showd.struts包中
InputStream is=getClass().getResourceAsStream("/cn/com/showd/struts/db.properties");
p.load(is);
sDBDriver=p.getProperty("DBDriver");
sConnection=p.getProperty("Connection");
sUser=p.getProperty("User");
sPassword=p.getProperty("Password");
Properties pr=new Properties();
pr.put("user", sUser);
pr.put("password", sPassword);
pr.put("characterEncoding", "UTF-8");
pr.put("useUnicode","true");
Class.forName(sDBDriver).newInstance();
return DriverManager.getConnection(sConnection,pr);
}
}
这样我们就可以通过 _getConnection() 方法去得到一个connection了。
例如有一数据库配置文件db.properties。其中的内容如下:
DBDriver=com.mysql.jdbc.Driver
Connection=jdbc:mysql://127.0.0.1:3306/user
User=root
Password=root
建立一个单例模式的类,用于获得数据库链接。
public class DBConnection {
private static DBConnection instance;
public static synchronized Connection getConnection() throws Exception{
if(instance==null){
instance=new DBConnection();
}
return instance._getConnection();
}
private DBConnection() {
}
public Connection _getConnection() throws Exception {
String sDBDriver=null;
String sConnection=null;
String sUser=null;
String sPassword=null;
Properties p=new Properties();
//db.properties放在cn.com.showd.struts包中
InputStream is=getClass().getResourceAsStream("/cn/com/showd/struts/db.properties");
p.load(is);
sDBDriver=p.getProperty("DBDriver");
sConnection=p.getProperty("Connection");
sUser=p.getProperty("User");
sPassword=p.getProperty("Password");
Properties pr=new Properties();
pr.put("user", sUser);
pr.put("password", sPassword);
pr.put("characterEncoding", "UTF-8");
pr.put("useUnicode","true");
Class.forName(sDBDriver).newInstance();
return DriverManager.getConnection(sConnection,pr);
}
}
这样我们就可以通过 _getConnection() 方法去得到一个connection了。
相关文章推荐
- 如何在JDBC中使用.properties文件配置驱动
- java通过配置文件jdbc.properties链接Oracle数据库工具类
- 如何读取DBConfig.properties文件,配置 JDBC ?
- Spring+mybatis+maven整合过程加载配置文件jdbc.properties
- 如何在SpringBoot下读取自定义properties配置文件
- 如何在SpringBoot下读取自定义properties配置文件
- jdbc基础 (二) 通过properties配置文件连接数据库
- JDBC工具类抽取方法(使用properties配置文件)
- properties 配置文件如何换行
- 配置文件中如何读取properties的配置文件
- Cocos2d-x win7 + vs2010 配置(如何创建cocos2d-x工程,解决各种头文件包含错误、lib库引用错误 及dll链接错误)
- Java中如何获取spring中配置的properties文件内容
- springboot如何读取配置文件中的参数(例如:application-consts.properties) 又结合maven读取配置文件的顺序
- jdbc基础 (二) 通过properties配置文件连接数据库
- java Spring使用配置文件读取jdbc.properties
- 复习课程jdbc:使用配置文件properties进行连接数据库,数据库存取图片,批处理,时间戳,事物回滚等等
- 如何配置properties文件和读取properties文件参数
- JAVA JDBC 读取配置文件链接数据库(oracle)
- jdbc链接mysql的第二个例子。使用properties文件存放连接信息。使用jar文件一个
- JDBC 通过从properties配置文件获取信息得到orcale数据库接口