Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用加载属性文件_Java_Sql Server 2005 - Fatal编程技术网

Java 使用加载属性文件

Java 使用加载属性文件,java,sql-server-2005,Java,Sql Server 2005,请查看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 sta

请查看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;
    }
}
连接文件

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);
)。将其更改为:

    try {
        dbproperties = new Properties();
        dbproperties.load(dbInputStream);

哈哈,这很简单。谢谢