3

I'm building a basic app that lets me scan an RFID tag and push the data to an SQL server. I finally got the NFC part hacked out (first Java/Android application), but am having trouble with the database. I've done allot of searching but can't find an answer. I know it is a security risk and poor practice to directly connect to the server, but this app will only be installed on one phone, and only used for demonstrative/test purposes. Any actual deployment would get a web interface, but that simply isn't an option right now.

I'm using the jtds 1.3.0 driver, and the following code snippet:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    import net.sourceforge.jtds.jdbc.*;

    public void query2()
    {
    Log.i("Android"," MySQL Connect Example.");
    Connection conn = null;
    try {
    String driver = "net.sourceforge.jtds.jdbc.Driver";
    Class.forName(driver).newInstance();
    //test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
    String connString = "jdbc:jtds:sqlserver://server_ip_address               
    :1433/DBNAME;encrypt=fasle;user=xxxxxxxxx;password=xxxxxxxx;instance=SQLEXPRESS;";
    String username = "xxxxxx";
    String password = "xxxxxxxxxx";
    conn = DriverManager.getConnection(connString,username,password);
    Log.w("Connection","open");
    Statement stmt = conn.createStatement();
    ResultSet reset = stmt.executeQuery("select * from TableName");

    //Print the data to the console
    while(reset.next()){
    Log.w("Data:",reset.getString(3));
    //              Log.w("Data",reset.getString(2));
    }
    conn.close();

    } catch (Exception e)
    {
    Log.w("Error connection","" + e.getMessage());
    }
    }

At the moment, my issue is that whenever I get to the

    Class.forname(driver).newInstance();

it hops straight to the catch block. I'm not sure why. I've gone line by line, but can't find an error message when the exception occurs, and have no idea why it does. Thanks guys.

Edit: I removed the try/catch block then executed it and got allot from the logcat. Not sure if it helps, but here's what it gave me:

12-12 05:29:37.369: W/dalvikvm(1242): threadid=1: thread exiting with uncaught exception (group=0x4150a930)
12-12 05:29:37.389: E/AndroidRuntime(1242): FATAL EXCEPTION: main
12-12 05:29:37.389: E/AndroidRuntime(1242): java.lang.IllegalStateException: Could not execute method of the activity
12-12 05:29:37.389: E/AndroidRuntime(1242):     at android.view.View$1.onClick(View.java:3597)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at android.view.View.performClick(View.java:4202)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at android.view.View$PerformClick.run(View.java:17340)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at android.os.Handler.handleCallback(Handler.java:725)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at android.os.Looper.loop(Looper.java:137)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at java.lang.reflect.Method.invoke(Method.java:511)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at dalvik.system.NativeStart.main(Native Method)
12-12 05:29:37.389: E/AndroidRuntime(1242): Caused by: java.lang.reflect.InvocationTargetException
12-12 05:29:37.389: E/AndroidRuntime(1242):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at java.lang.reflect.Method.invoke(Method.java:511)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at android.view.View$1.onClick(View.java:3592)
12-12 05:29:37.389: E/AndroidRuntime(1242):     ... 11 more
12-12 05:29:37.389: E/AndroidRuntime(1242): Caused by: java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
12-12 05:29:37.389: E/AndroidRuntime(1242):     at java.lang.Class.classForName(Native Method)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at java.lang.Class.forName(Class.java:217)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at java.lang.Class.forName(Class.java:172)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at com.nateapp.finalproject.MainActivity.sendData(MainActivity.java:261)
12-12 05:29:37.389: E/AndroidRuntime(1242):     ... 14 more
12-12 05:29:37.389: E/AndroidRuntime(1242): Caused by: java.lang.NoClassDefFoundError: net/sourceforge/jtds/jdbc/Driver
12-12 05:29:37.389: E/AndroidRuntime(1242):     ... 18 more
12-12 05:29:37.389: E/AndroidRuntime(1242): Caused by: java.lang.ClassNotFoundException: Didn't find class "net.sourceforge.jtds.jdbc.Driver" on path: /data/app/com.nateapp.finalproject-1.apk
12-12 05:29:37.389: E/AndroidRuntime(1242):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-12 05:29:37.389: E/AndroidRuntime(1242):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-12 05:29:37.389: E/AndroidRuntime(1242):     ... 18 more
4
  • First, check the Logcat for the error message (e.getMessage) Commented Dec 12, 2012 at 10:06
  • The only thing that comes up in logcat when it happens is "Error connection." Is there a way to get a more detailed error message? Commented Dec 12, 2012 at 10:17
  • I removed the try/catch block and got allot more data from the logcat. Hopefully it's relevant and meaningful. Commented Dec 12, 2012 at 10:36
  • May the "encrypt=fasle" in connString be a problem? Commented Dec 12, 2012 at 13:37

1 Answer 1

1

After a good bit more searching, someone suggested that it is a bug in the 1.3.0 version. I simply deleted the 1.3.0 version from my project (libs folder) and dropped the 1.2.7 version in and it works fine now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.