读取配置文件内容

时间:2015-04-22 18:39:46

标签: java config

我正在尝试读取“DB_Config_File.properties”文件但由于某种原因发生了以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)

我正在处理配置文件读取的类如下所示:

公共类数据库{

public static Connection conn;
public static void dbConnection() throws FileNotFoundException, IOException, ClassNotFoundException, SQLException {

        Properties props = new Properties();
        driver = props.getProperty("driver");
        String url = props.getProperty("url");
        String username = props.getProperty("username"); 
        String password = props.getProperty("password");
        String configFile = "F:/Project/Java Project/src/mainScreen/DB_Config_File.properties";

        InputStream ins = new FileInputStream(configFile); 
        props.load(ins);
        Class.forName(driver);


        conn = DriverManager.getConnection(url, username, password);

}

根据错误判断我猜测“Class.forName(driver)”部分有错误。

你能帮帮我吗?

由于

1 个答案:

答案 0 :(得分:0)

在从中读取值后,您将从输入流加载属性文件。变量'驱动程序'由于属性为空并且导致NPE,因此为null。

相关问题