2

我正在构建一个基本应用程序,它可以让我扫描 RFID 标签并将数据推送到 SQL 服务器。我终于把 NFC 部分破解了(第一个 Java/Android 应用程序),但是数据库有问题。我已经完成了搜索,但找不到答案。我知道直接连接到服务器是一种安全风险和不好的做法,但是这个应用程序只会安装在一部手机上,并且只用于演示/测试目的。任何实际部署都会获得一个 Web 界面,但现在这根本不是一个选项。

我正在使用 jtds 1.3.0 驱动程序和以下代码片段:

    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());
    }
    }

目前,我的问题是,每当我到达

    Class.forname(driver).newInstance();

它直接跳到 catch 块。我不确定为什么。我一行一行地走了,但是当异常发生时找不到错误消息,也不知道为什么会这样。多谢你们。

编辑:我删除了 try/catch 块然后执行它并从 logcat 中获得分配。不确定它是否有帮助,但这是它给我的:

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

1 回答 1

1

经过一番搜索,有人建议这是 1.3.0 版本中的错误。我只是从我的项目(libs 文件夹)中删除了 1.3.0 版本,然后将 1.2.7 版本放入其中,它现在可以正常工作了。

于 2012-12-12T11:06:18.563 回答