我正在尝试将我的Android设备连接到MySQL服务器上运行的MySQL数据库。但我不断收到下面所述的错误。
有谁能请我指出正确的方向?
logcat的
07-15 09:17:21.708: E/AndroidRuntime(19677): FATAL EXCEPTION: AsyncTask #1
07-15 09:17:21.708: E/AndroidRuntime(19677): java.lang.RuntimeException: An error occured while executing doInBackground()
07-15 09:17:21.708: E/AndroidRuntime(19677): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-15 09:17:21.708: E/AndroidRuntime(19677): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-15 09:17:21.708: E/AndroidRuntime(19677): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-15 09:17:21.708: E/AndroidRuntime(19677): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-15 09:17:21.708: E/AndroidRuntime(19677): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-15 09:17:21.708: E/AndroidRuntime(19677): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-15 09:17:21.708: E/AndroidRuntime(19677): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-15 09:17:21.708: E/AndroidRuntime(19677): at java.lang.Thread.run(Thread.java:841)
07-15 09:17:21.708: E/AndroidRuntime(19677): Caused by: java.lang.VerifyError: net/sourceforge/jtds/jdbc/TdsCore
07-15 09:17:21.708: E/AndroidRuntime(19677): at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:359)
07-15 09:17:21.708: E/AndroidRuntime(19677): at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
07-15 09:17:21.708: E/AndroidRuntime(19677): at java.sql.DriverManager.getConnection(DriverManager.java:179)
07-15 09:17:21.708: E/AndroidRuntime(19677): at java.sql.DriverManager.getConnection(DriverManager.java:213)
07-15 09:17:21.708: E/AndroidRuntime(19677): at com.example.rf.MainActivity.runSQL(MainActivity.java:110)
07-15 09:17:21.708: E/AndroidRuntime(19677): at com.example.rf.MainActivity$DoInBackground.doInBackground(MainActivity.java:58)
07-15 09:17:21.708: E/AndroidRuntime(19677): at com.example.rf.MainActivity$DoInBackground.doInBackground(MainActivity.java:1)
07-15 09:17:21.708: E/AndroidRuntime(19677): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-15 09:17:21.708: E/AndroidRuntime(19677): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-15 09:17:21.708: E/AndroidRuntime(19677): ... 4 more
CODE
我如何称呼它:
new DoInBackground().execute();
方法:
private class DoInBackground extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void...unused) {
String response = "";
try {
runSQL();
}
catch(Exception e) {
Log.i("Error", e.toString());
}
return null;
}
}
public void runSQL()
{
String connectionString = "jdbc:jtds:sqlserver://server:1433/DBNAME;instance=SQLEXPRESS";
String userName = "username";
String password = "password";
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver"); //specify the jtds driver
Connection conn = DriverManager.getConnection(connectionString, userName, password); //establish connection
//System.out.println("Connection properly established");
Log.i("Success", "Connected to DB");
conn.close();
} catch (Exception e)
{
String err = (e.getMessage()==null)?"SD Card failed":e.getMessage();
Log.e("sdcard-err2:",err);
}
}
更新
我发现了这个问题。问题是,我使用ktds 1.3.1作为连接到数据库的驱动程序,但这个版本被窃听。因此切换回1.3.0版本解决了我的问题。
感谢您的帮助!
答案 0 :(得分:1)
我假设您正在尝试从Android客户端连接服务器的SQL。 您无法从Android客户端直接连接到SQL服务器。 您需要在服务器端为它创建Web服务。
在Web服务中,您可以使用适当的JDBC连接..