【问题标题】:db.properties - null pointer exceptiondb.properties - 空指针异常
【发布时间】:2014-08-29 11:47:51
【问题描述】:

我为相关问题尝试了许多可用的答案,但没有一个是成功的。

这是我的问题,

我有一个动态 Web 应用程序,它使用 oracle 数据库来检索数据。为了让它变得更好,我使用了一个 db.properties 配置文件并创建了一个数据源,

这是 dataFactory.class 的代码:

public DataSource getDatasource() {
    Properties props = new Properties();
    FileInputStream fis = null;
    InputStream in = null;
    OracleDataSource oracleDS = null;
    try {
        fis = new FileInputStream("db.properties");
        props.load(fis);
        oracleDS = new OracleDataSource();
        oracleDS.setURL(props.getProperty("url"));
        oracleDS.setUser(props.getProperty("user"));
        oracleDS.setPassword(props.getProperty("password"));
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return oracleDS;
}

为了建立连接,我在 EmployeeDao.class 中使用它,

 DataSource ds;
 EmpDatasourceFactory empDs = new EmpDatasourceFactory();
 Connection conn;

public boolean insertRecord(Employee emp) {

    boolean status = false;
    ds = empDs.getDatasource();


    try {
        conn = ds.getConnection(); //Getting the null point exception
    }
}

注意:请注意,我会根据需要提供所有其他代码。

问题是,当我创建一个 java 文件并创建一些方法并使用它来建立连接时,它工作正常,我的意思是当它作为 java 应用程序运行时。

但是当使用一个employeeDao.java 并使用apache tomcat 服务器运行它时,它给了我一个错误。请帮忙。!!!

【问题讨论】:

    标签: java web-applications jdbc datasource tomcat6


    【解决方案1】:

    在@joop Eggen 的帮助下,我能够修复这个错误,最后我的方法看起来像

    public static  DataSource geDataSource() throws ResourceResolverException{
        ResourceBundle props = ResourceBundle.getBundle("db");
        OracleDataSource oracleDS = null;
        try {
            oracleDS = new OracleDataSource();
            oracleDS.setURL(props.getString("url"));
            oracleDS.setUser(props.getString("user"));
            oracleDS.setPassword(props.getString("password"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return oracleDS;
    }
    

    我必须处理所有的“ResourceResolverException”。之后效果很好。

    快乐编码..!!!并使其成为 Java :)

    【讨论】:

      【解决方案2】:

      .war(类似 .jar 的 zip 文件)是部署 Web 应用程序的常规方式。在 zip 中不能使用 File 来检索 zip 条目。

      要走的路是从类路径中读取“资源”。 ResourceBundle 在这里很合适。

      ResourceBundle props = ResourceBundle.getBundle("db");
      OracleDataSource oracleDS = null;
      try {
          oracleDS = new OracleDataSource();
          oracleDS.setURL(props.getString("url"));
          oracleDS.setUser(props.getString("user"));
          oracleDS.setPassword(props.getString("password"));
      } catch (ResourceNotFoundException e) {
          e.printStackTrace();
      } catch (SQLException e) {
          e.printStackTrace();
      }
      return oracleDS;
      

      其他做法是在 .war 之外的 Web 容器中定义此类应用程序资源。这在每个 Java EE 应用服务器上略有不同,但例如允许为不同的服务器使用不同的数据源。

      【讨论】:

      • 经过一些修改后效果很好。感谢那。我已将我的方法包含在答案框中。谢谢。
      • 我将ResourceBundle.get 更正为ResourceBundle.getBundle,供任何不知情的读者阅读。
      • 感谢@Joop Eggen
      猜你喜欢
      • 2015-07-12
      相关资源
      最近更新 更多