gpt4 book ai didi

java - 在 Android 中直接访问 SQL 数据库

转载 作者:行者123 更新时间:2023-11-30 11:37:56 24 4
gpt4 key购买 nike

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

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

    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 block 。我不确定为什么。我已经一行一行地看了,但是在异常发生时找不到错误信息,也不知道为什么会这样。谢谢大家。

编辑:我删除了 try/catch block 然后执行它并从 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

最佳答案

经过多方搜索,有人提出这是 1.3.0 版本中的一个错误。我只是从我的项目(libs 文件夹)中删除了 1.3.0 版本并将 1.2.7 版本放入其中,现在它工作正常。

关于java - 在 Android 中直接访问 SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13837231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com