简体   繁体   中英

DriverManager.getConnection() doesn't work with OpenJDK

I have an executable jar utility program that makes database queries. It runs fine with Oracle Java. However, if I run it with OpenJDK, it can't get the database connection. The weird thing is there is no error or exception. It just stops running the method.

I'm connecting to SQL Server. I get "getDbConnection 2" output to the console, but nothing happens after that. No errors, nothing. I can't figure out why this works with standard Java but fails with 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. After updating to mssql-jdbc-9.4.0.jre8.jar it works with OpenJDK 11.0.2 as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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