读书人

使用配置文件连接数据库时的一个小疑点

发布时间: 2012-02-12 17:16:34 作者: rapoo

使用配置文件连接数据库时的一个小问题
我连接SQL SERVER 2000 数据库 是写的一个java类来读取硬盘中的配置文件,问题是在此程序中,配置文件的路径是一个绝对路径,我想使用相对路径,这样就便于我开发的项目的移植. 大家看懂我说的话没有啊?
我还是把我的代码贴上来吧.

这是连接数据库的JAVA类:

package com.qichunren.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

//DatabaseUtil类用来取得数据库连接和释放数据库边接
public class DatabaseUtil {

private static String dbDriverName = null;// 数据库驱动程序名称

private static String dbHost = null;// 数据库主机名或者IP地址

private static String dbPort = null;// 数据库服务器端口号

private static String dbUser = null;// 数据库用户名

private static String dbPwd = null;// 数据库用户名的密码

private static String dbName = null;// 要连接的数据库的名称

private static String dbUrl = null;// 数据库连接地址

private static Connection conn = null;

// 读取配置文件中的各项参数
public static void loadProperties() {
Properties prop = new Properties();
try {
String fileName = "D:\\SystemConfig.ini ";// SystemConfig.ini是配置文件,位于D盘根目录
FileInputStream fis = new FileInputStream(fileName);
prop.load(fis);
dbDriverName = prop.getProperty( "DatabaseDriverName ");
dbHost = prop.getProperty( "DatabaseHost ");
dbPort = prop.getProperty( "DatabasePort ");
dbUser = prop.getProperty( "DatabaseUser ");
dbPwd = prop.getProperty( "DatabasePwd ");
dbName = prop.getProperty( "DatabaseName ");
dbUrl = "jdbc:microsoft:sqlserver:// " + dbHost + ": " + dbPort;
// eg. ddUrl: jdbc:microsoft:sqlserver://172.16.140.102:1433
fis.close();// 读取参数完毕后,关闭文件输入流

} catch (FileNotFoundException e) {
System.out.println( "配置文件对路径不对!请检查... ");
e.printStackTrace();
} catch (IOException e) {
System.out.println( "读取配置文件时出现错误... ");
e.printStackTrace();
}
}

// 取得数据库连接
public synchronized static Connection getConn() throws SQLException,
ClassNotFoundException {
loadProperties();
Class.forName(dbDriverName);
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
conn.setCatalog(dbName);// 将dbName设为当前连接的数据库
return conn;
}

// 释放数据库连接
public synchronized static void releaseConnection(Connection connection) {
try {
if (connection != null && !connection.isClosed())
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
connection = null;
}

}


大家看程序代码就知道我的配置文件是放在D盘根目录的(这样多不方便啊),我想要把它放在和下此JAVA类同目录下,程序就报错说找不到配置文件.


我的配置文件(我发现扩展名是任意的,我的是ini)如下:
#database config set!
#www.qichunren.com
#QichunRen Soft

#驱动程序名称
DatabaseDriverName=com.microsoft.jdbc.sqlserver.SQLServerDriver

#数据库服务器名称或者IP地址
DatabaseHost=localhost


#数据库服务器端口号
DatabasePort=1433


#数据库用户名
DatabaseUser=sa


#数据库用户名的密码
DatabasePwd=123456


#数据库名
DatabaseName=jspBlog


有没有哪位大侠可以帮我解决这个小问题啊?

补充:在JAVA类中不能像JSP那样,可以获取WEB应用程序的路径:
<%
String path = request.getContextPath();//WEB应用程序在容器中的目录
String basePath = request.getScheme() + ":// "
+ request.getServerName() + ": " + request.getServerPort()
+ path + "/ ";//WEB应用程序的URL
%>
在JAVA类中要是有个什么方法可以这样就好啦...

[解决办法]
给lz一个例子:

*************************************
/**
* 读取properties文件信息
*/
private String getPropertiesValue(String fileName, String key) {
Properties properties = new Properties();
try {
String value = " ";

// 参数fileName可以是 "/filename ",这里的/代表web发布根路径下WEB-INF/classes
// 默认使用该方法的路径是:WEB-INF/classes。已经在Tomcat中测试。
InputStream inputFile = this.getClass().getResourceAsStream(
fileName);
properties.load(inputFile);
inputFile.close();
if (properties.containsKey(key)) {
value = properties.getProperty(key);
return value;
} else
return value;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}

/**
* 获得Connection
*/
public Connection getConnection() {

// 读取properties文件信息
String driverclass = this.getPropertiesValue( "/config/conn.properties ",
"driverClass ");
String url = this.getPropertiesValue( "/config/conn.properties ", "url ");
String user = this
.getPropertiesValue( "/config/conn.properties ", "user ");
String password = this.getPropertiesValue( "/config/conn.properties ",
"password ");

try {

Class.forName(driverclass);
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return null;
}

读书人网 >Java Web开发

热点推荐