简体   繁体   English

无法加载 jdbc 驱动程序 (ClassNotFoundException)

[英]cannot load jdbc driver (ClassNotFoundException)

I'm trying to load the JDBC driver to make some SQL calls to my AS400.我正在尝试加载 JDBC 驱动程序以对我的 AS400 进行一些 SQL 调用。 I've tried running the connection on a computer which has JDBC installed on it and the URL and SQL calls work fine.我尝试在安装了 JDBC 的计算机上运行连接,并且 URL 和 SQL 调用工作正常。

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.我需要开发一个没有安装驱动程序的应用程序(目前适用于 Android,尽管我们正在寻求扩展到桌面应用程序)。 I am testing the code on an actual android device, not the emulator, so it has full internet permissions.我正在实际的 android 设备上测试代码,而不是模拟器,因此它具有完整的互联网权限。

jt400 has drivers located in com.ibm.as400.access.AS400JDBCDriver as noted by IBM .IBM 所述, jt400 的驱动程序位于 com.ibm.as400.access.AS400JDBCDriver 中。

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.当我运行它时,我得到一个“找不到类:com.ibm.as400.access.AS400JDBCDriver”。

I've done some research and it suggests that Class.forName isn't a good way to go.我做了一些研究,它表明 Class.forName 不是一个好方法。 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.驱动程序需要作为 jar 包含在您的项目中。 If you already have it then add it to your project in a folder /libs.如果您已经拥有它,请将其添加到文件夹 /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.但是,在运行时它需要找到它,并且因为您没有包含它不能包含的 jar。

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.我建议您使用 IBM 的 JTOpen Lite/JTLite 库,而不是您尝试使用的库,这些库专为在 Android 上使用而设计。

For more information, please see my answer to a related question .有关更多信息,请参阅我对相关问题的回答

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

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