简体   繁体   中英

cannot load jdbc driver (ClassNotFoundException)

I'm trying to load the JDBC driver to make some SQL calls to my AS400. I've tried running the connection on a computer which has JDBC installed on it and the URL and SQL calls work fine.

I need to develop an app (currently for Android, though we're looking to expand to desktop application) which doesn't have the drivers installed. I am testing the code on an actual android device, not the emulator, so it has full internet permissions.

jt400 has drivers located in com.ibm.as400.access.AS400JDBCDriver as noted by IBM .

This is my code:

    try {

        Class.forName("com.ibm.as400.access.AS400JDBCDriver").newInstance();
        Connection conn = DriverManager.getConnection(
                url + schema + ";naming=sql;errors=full",
                uname,
                psswrd);

        // do SQL query stuff

        rs.close();
        stmt.close();
        conn.close();

    } 
    catch (ClassNotFoundException e)
    {
        this.basicOutput.setText("Class not found: " + e.getMessage());
        System.out.println(e.getStackTrace());
    }
    //catch (SQLException e)
    catch (Exception e)  //need generic to catch all errors thrown
    {

        this.basicOutput.setText(e.getMessage());
        System.out.println(e.getStackTrace());

    }

I get a "Class not found: com.ibm.as400.access.AS400JDBCDriver" when I run this.

I've done some research and it suggests that Class.forName isn't a good way to go. So I tried this as well:

        DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());

But this also yields the same error.

The class is there. The compiled code doesn't throw any syntax errors, but for some reason, runtime can't find it.

What am I missing?

The driver needs to be included as a jar in your project. If you already have it then add it to your project in a folder /libs.

You specify the driver class as a string literal so during compile time it passes and you think everything is fine. However, during run time it needs to find it and because you haven't included the jar it cannot.

Instead of the libraries that you are trying to use, I recommend using IBM's JTOpen Lite/JTLite libraries, which are specifically designed for use on Android.

For more information, please see my answer to a related question .

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