【问题标题】:Unable to connect to Oracle database using the JAR file无法使用 JAR 文件连接到 Oracle 数据库
【发布时间】: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 中调试或运行时运行良好,但从未显式运行!!

标签: java database oracle


【解决方案1】:

我找不到发生这种情况的确切原因,但我删除了导致错误 FAT Jar Export: could't find the class-path for ...etc 的 JAR 并再次导入它们。它成功了。

【讨论】:

    猜你喜欢
    • 2019-10-18
    • 2020-01-02
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    相关资源
    最近更新 更多