我们如何创建一个 JDBC 连接创建逻辑,而不需要每次数据库凭据发生更改时都更改 java 代码?
注意::数据库的用户名和密码将由用户(具有特殊权限)给出...完全权限授予他..
我的意思是, DB 用户名、密码和其他详细信息通常硬编码在 java 程序中。我们可以通过哪些其他方式保留它,以便在更改凭据时无需更改 java 代码? 我们还必须牢记凭证的隐私和安全因素...
最佳答案
- 将您的凭据存储在文件 (credentials.properties) 中
- 如果您的程序启动则读取该文件
像这样:
Properties properties = new Properties();
properties .load(new FileInputStream(new File("c:\\credentials.properties")));
String user = properties.getProperty("username");
String pass = properties.getProperty("password");
//...
credentials.properties 的内容如下所示:
#DB properties:
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost/mydatabase?useEncoding=true&characterEncoding=UTF-8
username = dit
password = 123456
关于java - 提供数据库密码而不将其硬编码在程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29227180/