请查看下面的MSSQL Server 2005代码。
属性文件
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://127.0.0.1:1433;databaseName=LibMgmtSys
user=sa
password=passwrod
连接文件
public class DBConnection {
static Properties dbproperties;
public static Connection getConnection() throws Exception {
Connection conn = null;
InputStream dbInputStream = null;
dbInputStream = DBConnection.class.getResourceAsStream("jdbc.properties");
try {
dbproperties.load(dbInputStream);
Class.forName(dbproperties.getProperty("driver"));
conn = DriverManager.getConnection(dbproperties.getProperty("url"),
dbproperties.getProperty("user"),
dbproperties.getProperty("password"));
} catch (Exception exp) {
System.out.println("error : " + exp);
}
return conn;
}
}
当我尝试执行dbproperties.load(dbInputStream)时,以上代码为我提供了NullPointException。难道我做错了什么???
最佳答案
您没有实例化dbproperties
,因此当您尝试取消引用(dbproperties.load(dbInputStream);
)时它为null。更改为:
try {
dbproperties = new Properties();
dbproperties.load(dbInputStream);