Android:连接到数据库

时间:2016-02-23 09:35:35

标签: java android android-asynctask jtds mssql-jdbc

我正在尝试使用AsyncTask中的jtds连接到mssql server数据库。在线,

con = DriverManager.getConnection(connString);

当它出现在这一行时,在Execute函数上执行3或4秒的AsyncTask.java文件后,该应用程序崩溃。

这是我到目前为止所要做的事情

protected Void doInBackground(Void... params)
    {
        Connection con = null;
        try {
            String driver = "net.sourceforge.jtds.jdbc.Driver";
            Class.forName(driver).newInstance();
            String connString = "jdbc:jtds:sqlserver://dbandroid.mssql.somee.com:1433/dbandroid;";
            con = DriverManager.getConnection(connString,"username","password");
            Log.i("SQLConn", "IsClosed:" + con.isClosed());
        }
        catch(Exception ex)
        {
            Log.e("SQLServerConn",ex.getMessage());
        }
        return null;
    }

在3到4秒之后的geting连接线上,它来​​到AsyncTask.java

public synchronized void execute(final Runnable r) {
        mTasks.offer(new Runnable() {
            public void run() {
                try {....

并且sql连接没有问题因为我已经使用相同的用户名和密码将其连接到SSMS

错误记录

02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime: Process: com.example.hamza.sunshine, PID: 15690
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:300)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:  Caused by: java.lang.VerifyError: net/sourceforge/jtds/jdbc/TdsCore
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:359)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.sql.DriverManager.getConnection(DriverManager.java:179)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.sql.DriverManager.getConnection(DriverManager.java:213)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at com.example.hamza.sunshine.MainActivity$SQLServerConn.doInBackground(MainActivity.java:270)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at com.example.hamza.sunshine.MainActivity$SQLServerConn.doInBackground(MainActivity.java:258)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
02-23 05:18:03.704 15690-15737/com.example.hamza.sunshine E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841) 

1 个答案:

答案 0 :(得分:0)

这是答案。当我收到f * ckin错误时,我正在使用jtds-1.3.1。刚刚将jtds降级到1.3.0和BINGO。它运行正常并成功连接到数据库。我想这是最新版本中的一个错误。