【发布时间】:2020-04-15 13:47:37
【问题描述】:
我在一个 Java 项目中有两个包。
Package A 有一个名为App.java 的类。此类接受参数解析(利用 apache commons-cli lib)。
代码是
CommandLineParser clip = new DefaultParser();
Options options = new Options();
options.addOption("zp", "ZipFilePath", true, "Mention the path where zip file is present");
options.addOption("d", "dbPropFile", true, "Mention the path database property file");
options.addOption("h", "help", false, "This mentions how to use this utility");
CommandLine cli = clip.parse(options, args);
File dbpropFile = new File(cli.getOptionValue("d"));
现在如何在包 B 的另一个类 (SQLServerConn.java) 中导入 dbPropFile 变量?
package com.abc.integra.db.B;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import com.abc.integra.db.A.LoadDBProps;
public class SQLServerConn {
public static Connection dbConn;
public static Properties props;
public static Connection getConn() {
try {
final String drivername = (String)SQLServerConn.props.get("drivername");
final String url = (String)SQLServerConn.props.get("url");
final String dbname = (String)SQLServerConn.props.get("dbname");
final String username = (String)SQLServerConn.props.get("username");
final String password = (String)SQLServerConn.props.get("password");
//jasypt final String password1 = (String)SQLServerConn.props.getProperty(key);
Class.forName(drivername); //registering the driver before connection with the DB
SQLServerConn.dbConn = DriverManager.getConnection(url + ";databaseName=" + dbname + ";user=" + username + ";password=" + password);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
return SQLServerConn.dbConn;
}
static {
SQLServerConn.dbConn = null;
try {
final LoadDBProps lp = new LoadDBProps();
SQLServerConn.props = lp.loadDBProperties(filename);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我想在加载属性部分用dbpropFile 变量值代替filename。
【问题讨论】:
标签: java file-io java-io apache-commons-cli