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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With