简体   繁体   English

DriverManager.getConnection() 不适用于 OpenJDK

[英]DriverManager.getConnection() doesn't work with OpenJDK

I have an executable jar utility program that makes database queries.我有一个可执行的 jar 实用程序,可以进行数据库查询。 It runs fine with Oracle Java.它在 Oracle Java 上运行良好。 However, if I run it with OpenJDK, it can't get the database connection.但是,如果我用 OpenJDK 运行它,它就无法获得数据库连接。 The weird thing is there is no error or exception.奇怪的是没有错误或异常。 It just stops running the method.它只是停止运行该方法。

I'm connecting to SQL Server.我正在连接到 SQL Server。 I get "getDbConnection 2" output to the console, but nothing happens after that.我将“getDbConnection 2”输出到控制台,但之后没有任何反应。 No errors, nothing.没有错误,什么都没有。 I can't figure out why this works with standard Java but fails with OpenJDK.我不明白为什么这适用于标准 Java,但不适用于 OpenJDK。

Here is the code:这是代码:

protected Connection getDbConnection()
{
    Connection conn = null;
    consoleWrite("getDbConnection 1");
    
    try {
        final String server = from_server.getText();
        final String port = from_port.getText();
        final String dbName = from_dbname.getText();
        String url = String.format("jdbc:sqlserver://%s:%s;databaseName=%s", server, port, dbName);
        String user = from_user.getText();
        final String pwd = from_pass.getText();
        consoleWrite("getDbConnection 2");
                    
        conn = DriverManager.getConnection(url, user, pwd);
        if (conn == null) {
            consoleWrite("conn not found");
        }
        consoleWrite("getDbConnection 3");
    }
    catch (SQLException se) {
        handleError(se);
    }
    catch (ClassNotFoundException ce) {
        handleError(ce);
    }
    catch(Exception e) {
        handleError(e);
    }
    
    return conn;
}

Solution:解决方案:

I needed a newer JDBC driver.我需要一个更新的 JDBC 驱动程序。 After updating to mssql-jdbc-9.4.0.jre8.jar it works with OpenJDK 11.0.2 as well.更新到 mssql-jdbc-9.4.0.jre8.jar 后,它也适用于 OpenJDK 11.0.2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM