【发布时间】:2015-08-19 21:05:04
【问题描述】:
我有一个使用 JSF 的 java-ee webapp,我们在应用程序中有一个数据库组件
积分
在我的代码中,我们将数据库凭据定义为:
String url = "jdbc://mysql://localhost:3306/database?autoReconnect=true";
String user = "root";
String pwd = "P@ssw0rd";
连接
那么对于 db 连接本身是 JDBC 我认为这可能是一个问题
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
Logger.getLogger(MysqlJDBC.class.getName()).log(Level.SEVERE, null, ex);
}
// some other random code
try {
connection = DriverManager.getConnection(url, user, pwd);
System.out.println("[MYSQL]\tSUCCESS - Connected to [" + url + "] as user: " + user);
return;
} catch (SQLException e) {
System.out.println("[MYSQL]\tFAILURE - Could not connect to [" + url + "] as user: " + user);
System.out.println("[MYSQL]\t" + e.getLocalizedMessage());
}
JNDI
我不完全了解 JDNI 的工作原理,但据我了解,您在外部定义资源,在运行时或 Web 应用程序启动时,它将从某处读取数据并将其插入应用程序。我正在使用 Tomcat,我想知道有没有一种方法可以让我通过 JNDI 信息填充我的数据库凭据。
如果这是不可能的,我是否必须在外部定义我的整个数据库连接并重新编写我所有的内部代码才能以某种方式使用它?
非常感谢任何代码示例。
【问题讨论】:
标签: java mysql tomcat jdbc jndi