4

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Sammy
  • 99
  • 1
  • 2
  • 10
  • 2
    JDBC isn't something that is installed. A jdbc Driver is a class that interacts with your database. You need to have that class (usually within a .jar file) on your classpath when you run your application. – Sotirios Delimanolis Mar 14 '13 at 20:27
  • I see. Is there a way to include it or load it from a .jar that's included within the project instead of on the classpath? – Sammy Mar 14 '13 at 20:49
  • 1
    You include the `jar` in your project under the libs folder, then add that libs folder to the build path. Follow the steps here: http://stackoverflow.com/questions/1334802/how-can-i-use-external-jars-in-an-android-project – Sotirios Delimanolis Mar 14 '13 at 20:53
  • I get this over and over again: – Sammy Mar 14 '13 at 21:03
  • Dx warning: Ignoring InnerClasses attribute for an anonymous inner class (com.ibm.as400.access.PrintObjectList$1) that doesn't come with an associated EnclosingMethod attribute. This class was probably produced by a compiler that did not target the modern .class file format. The recommended solution is to recompile the class from source, using an up-to-date compiler and without specifying any "-target" type options. The consequence of ignoring this warning is that reflective operations on this class will incorrectly indicate that it is *not* an inner class. – Sammy Mar 14 '13 at 21:04
  • And then it crashed Eclipse. I have the most recent version of the lib and am using the lasted java I could find. – Sammy Mar 14 '13 at 21:07
  • I think you might be hitting an adroid problem. I've found that it does not like to work with jars that are compiled with older Java versions. – kiwiron Sep 05 '14 at 01:31
  • 1
    *Remove* the `Class.forName()` while you're about it. It hasn't been needed since 2007. – user207421 Jan 31 '19 at 03:21

2 Answers2

0

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.

ggenglish
  • 1,668
  • 18
  • 19
  • The jar is included in my project libraries. I've tried it both by including it just in the library reference as well as in the /libs folder. It still can't find it. – Sammy Mar 14 '13 at 20:50
0

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
burtmacklin16
  • 715
  • 2
  • 13
  • 31
  • Thanks very much for the documentation! I've switched drivers to "com.ibm.jtopenlite.database.jdbc.JDBCDriver" which does load. However I'm getting a "java.net.SockettimeOutException: Connection timed out" exception. I know this URL worked because I've used it in my IBS Integrator connection (and when testing it out in J2SE). It is a local IP, as this server doesn't speak to anything outside the network unless you remote into the VPN. I am using the internal wireless, not 3G connection. Could that mess up my connection somehow? – Sammy Mar 21 '13 at 16:13
  • This exception is a little bit tough to debug without seeing your entire program. I would suggest developing a test program on a PC that you know you can connect from/to your DB. That will help iron out the details as to whether it is a URL problem or otherwise. – burtmacklin16 Mar 21 '13 at 20:19
  • The URL is working just fine using a desktop java program. Perhaps I can send you my code somehow. There's not much to it quite yet. – Sammy Mar 22 '13 at 22:32
  • Though it occurs to me that I use "jdbc:as400://IP" for the desktop (which won't work on mobile, I get a "wrong driver" error) and for the android, I use "jdbc:jtopenlite://IP" – Sammy Mar 22 '13 at 22:40