【发布时间】:2021-01-29 02:49:21
【问题描述】:
我正在使用 eclipse 2020 版本,并且我添加了连接到 Oracle 服务器所需的所有库,例如 ojdbc7.jar,我的代码如下:
public Connection SetDatabaseConnection() {
writeInLog("Connecting to IRB", 0);
if(openConnection()){
try {
productionPool.setDriverType("thin");
productionPool.setUser(username);
productionPool.setPassword(password);
productionPool.setPortNumber(Integer.parseInt(port));
productionPool.setServerName(IP);
productionPool.setServiceName(serviceName);
productionPool.setURL("jdbc:oracle:thin:@"+ _connStr.substring(_connStr.indexOf(":")+1));
productionPooledConnection = productionPool.getPooledConnection();
if (productionPooledConnection != null) {
//return true;
currentConnection = productionPooledConnection.getConnection();
logger.info("Connected to IRB server");
return currentConnection;
}
} catch (SQLException ex) {
logger.info("Unable to connect to IRB server, SQLException: "+ex.getMessage());
System.out.println(" (IRB-Exception) DB Exception: \n"+ ex);
}
}
}
我的问题是:我可以在 Eclipse 中调试或运行应用程序时连接到服务器,但是当我导出 JAR 文件时,应用程序在此步骤中停止。
另外: 我打开连接的代码:
private boolean openConnection(){
try {
productionPool = new OracleConnectionPoolDataSource();
productionPooledConnection = new OraclePooledConnection();
logger.info("openConnection(): Connected to IRB server \n");
return true;
} catch (SQLException e) {
e.printStackTrace();
logger.info("Unable to connect to IRB server , SQLException: "+e.getMessage());
}
logger.info("openConnection(): Unable to connect to IRB server \n");
return false;
}
应用程序从不抛出任何异常,它只在日志文件中写入此语句:writeInLog("Connecting to IRB", 0);
【问题讨论】:
-
请问您也可以添加堆栈跟踪吗?导出 JAR 文件后,可能是 ojdbc7.jar 不在类路径中?
-
你搞砸了日志记录。您不会将“ex.getMessage()”连接到事物,而是逐字传递异常,并让日志框架对其进行排序。然后这会让你得到堆栈跟踪、因果链等。你扔掉了 90% 的有用信息。应该是
logger.info("blabla", ex); -
当 iIextract JAR 文件时,我选择“将所需库提取到生成的 JAR”选项。此外,ojdbc7.jar 在 Java 构建路径中!
-
@rzwitserloot 问题是它没有给我任何异常 JAR 文件只是退出。正如您所看到的,我在日志文件中写入了任何会引发任何异常但从未发生过的情况。正如我在问题中所说,它在语句 writeInLog("Connecting to IRB", 0); 中突然停止。所以这是日志文件的最后一行!!
-
应用程序在 Eclipse 中调试或运行时运行良好,但从未显式运行!!