繁体   English   中英

我在 WEB-INF/config 文件夹中有一个 system.properties 文件。 如何编写用于从该文件中检索数据的代码?

[英]I have a system.properties file present in WEB-INF/config folder. How to write a code for retrieving data from this file?

下面是我的 system.properties 文件:

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:xe
duser=kj
dpass=class234

下面是要放置它的java代码:

抽象数据访问对象.java

package com.dts.core.dao;

import com.dts.core.util.LoggerManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


public class AbstractDataAccessObject
{
  Connection con;
  static Properties props;

  public AbstractDataAccessObject() {}

  public Properties getProperties()
  {
    return props;
  }



  public void setProperties(Properties props)
  {
    AbstractDataAccessObject.props = props;
  }

  public Connection getConnection()
  {
    try {

        Properties p = getProperties();
        Class.forName(p.getProperty("driver"));
        con = DriverManager.getConnection(p.getProperty("url"), p.getProperty("duser"), p.getProperty("dpass"));

    }
    catch (ClassNotFoundException cnf)
    {





      LoggerManager.writeLogWarning(cnf);
    }
    catch (SQLException se)
    {
      LoggerManager.writeLogWarning(se);
    }
    return con;
  }


  public int getSequenceID(String tableName, String pkid)
  {
    int id = 0;
    try
    {
      con = getConnection();
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("select max(" + pkid + ") from " + tableName);
      if (rs.next())
        id = rs.getInt(1);
      id++;
    }
    catch (SQLException se)
    {
      LoggerManager.writeLogWarning(se);







      try
      {
        con.close();
      }
      catch (SQLException se1)
      {
        LoggerManager.writeLogWarning(se1);
      }
      catch (Exception e)
      {
        LoggerManager.writeLogWarning(e);
      }
    }
    catch (Exception e)
    {
      LoggerManager.writeLogWarning(e);



      try
      {
        con.close();
      }
      catch (SQLException se)
      {
        LoggerManager.writeLogWarning(se);
      }
      catch (Exception e1)
      {
        LoggerManager.writeLogWarning(e1);
      }
    }
    finally
    {
      try
      {
        con.close();
      }
      catch (SQLException se)
      {
        LoggerManager.writeLogWarning(se);
      }
      catch (Exception e)
      {
        LoggerManager.writeLogWarning(e);
      }
    }
    return id;
  }
}

请帮助我通过 system.properties 文件连接我的数据库,以及我应该在哪里保存属性文件?

在使用实际定义到属性文件中的属性之前,首先需要加载它,

java.util.Properties properties  = new java.util.Properties();
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(filePath);

//load to property file to system... 
properties.load(inputStream);

// now, it's ready to use,
properties.getProperty("driver"); // get driver from properties file if is there otherwise gives null...

编辑:-

public static void main(String... s){
    Properties prop = new Properties();
    InputStream input = null;

    try {

            String filename = "app.properties";
            input = AbstractDataAccessObject.class.getClassLoader().getResourceAsStream(filename);
            if(input==null){
                    System.out.println("Sorry, unable to find " + filename);
                return;
            }
            //load a properties file from class path, inside static method
            prop.load(input);
            //get the property value and print it out
            System.out.println(prop.getProperty("driver")); // gives, oracle.jdbc.driver.OracleDriver
            System.out.println(prop.getProperty("duser")); // gives, kj
            System.out.println(prop.getProperty("dpass")); // gives, class234

    } catch (IOException ex) {
        ex.printStackTrace();
    } 
}

查看放入资源目录中的属性,以便 .properties 文件位于类路径中。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM