android app连接到外部数据库

时间:2014-05-22 11:55:06

标签: java android sql-server jtds

我已经尝试用jtds-1.3.1将我的Android应用程序连接到MSSQL服务器。 这是我的连接管理器代码。它在java控制台应用程序上运行良好..

package com.pjx.gradebook.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager {


    public static void  query()     // it is triggered by a button click
    {
        Connection conn = null;
        try  
        {  
            Class.forName("net.sourceforge.jtds.jdbc.Driver");  
            conn = DriverManager.getConnection("jdbc:jtds:sqlserver://ipaddress:1433/database;tds=8.0;lastupdatecount=true", username, password);  
        } catch (ClassNotFoundException e)  
        {  
            e.printStackTrace();  
        } catch (SQLException e)  
        {  
            e.printStackTrace();  
        } 
    }

}

我总是得到错误

05-22 06:47:42.230: W/System.err(21996): java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
05-22 06:47:42.230: W/System.err(21996):    at java.lang.Class.classForName(Native Method)
05-22 06:47:42.230: W/System.err(21996):    at java.lang.Class.forName(Class.java:251)
05-22 06:47:42.230: W/System.err(21996):    at java.lang.Class.forName(Class.java:216)
05-22 06:47:42.230: W/System.err(21996):    at com.pjx.gradebook.dao.ConnectionManager.query2(ConnectionManager.java:19)
05-22 06:47:42.230: W/System.err(21996):    at com.pjx.gradebook.MainActivity.connect(MainActivity.java:44)
05-22 06:47:42.230: W/System.err(21996):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 06:47:42.230: W/System.err(21996):    at java.lang.reflect.Method.invoke(Method.java:515)
05-22 06:47:42.230: W/System.err(21996):    at android.view.View$1.onClick(View.java:3818)
05-22 06:47:42.230: W/System.err(21996):    at android.view.View.performClick(View.java:4438)
05-22 06:47:42.230: W/System.err(21996):    at android.view.View$PerformClick.run(View.java:18422)
05-22 06:47:42.230: W/System.err(21996):    at android.os.Handler.handleCallback(Handler.java:733)
05-22 06:47:42.230: W/System.err(21996):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-22 06:47:42.230: W/System.err(21996):    at android.os.Looper.loop(Looper.java:136)
05-22 06:47:42.230: W/System.err(21996):    at android.app.ActivityThread.main(ActivityThread.java:5017)
05-22 06:47:42.230: W/System.err(21996):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 06:47:42.240: W/System.err(21996):    at java.lang.reflect.Method.invoke(Method.java:515)
05-22 06:47:42.240: W/System.err(21996):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-22 06:47:42.240: W/System.err(21996):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-22 06:47:42.240: W/System.err(21996):    at dalvik.system.NativeStart.main(Native Method)
05-22 06:47:42.240: W/System.err(21996): Caused by: java.lang.NoClassDefFoundError: net/sourceforge/jtds/jdbc/Driver
05-22 06:47:42.240: W/System.err(21996):    ... 19 more
05-22 06:47:42.240: W/System.err(21996): Caused by: java.lang.ClassNotFoundException: Didn't find class "net.sourceforge.jtds.jdbc.Driver" on path: DexPathList[[zip file "/data/app/com.pjx.gradebook-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.pjx.gradebook-2, /vendor/lib, /system/lib]]
05-22 06:47:42.240: W/System.err(21996):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-22 06:47:42.240: W/System.err(21996):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
05-22 06:47:42.240: W/System.err(21996):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
05-22 06:47:42.240: W/System.err(21996):    ... 19 more

请帮帮我吗? ADT:Build:v22.6.2-1085508

谢谢!

1 个答案:

答案 0 :(得分:0)

检查此答案,如果错误仍然存​​在,我会尝试找到更多解决方案

https://stackoverflow.com/a/18660514/2721515