Android通过jtds连接sql,加载驱动程序出错,求大神指教

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import android.app.Activity;
import android.database.SQLException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import net.sourceforge.jtds.jdbc.Driver;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String UserName = "sa";//用户名  
    String Password = "851202790";//密码  
    Connection con = null;  
    try { // 加载驱动程序  
         Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
        con = DriverManager.getConnection(  
                 "jdbc:jtds:sqlserver://localhost:1433/Drink", UserName,  
                Password);  
    } catch (ClassNotFoundException e) {  
        System.out.println("加载驱动程序出错");  
    } catch (SQLException e) {  
        System.out.println(e.getMessage());  
    } catch (Exception e) {  
        System.out.println(e.getMessage());  

    }  
    try {  
        testConnection(con);//测试数据库连接  
    } catch (java.sql.SQLException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }  
}  

public void testConnection(Connection con) throws java.sql.SQLException {  

    try {  

        String sql = "SELECT * FROM TRight";//查询表名为“table_test”的所有内容  
        Statement stmt = con.createStatement();//创建Statement  
        ResultSet rs = stmt.executeQuery(sql);//ResultSet类似Cursor  

        while (rs.next()) {//<code>ResultSet</code>最初指向第一行  
            System.out.println(rs.getString("authority_url"));//输出第n行,列名为“test_id”的值  
            System.out.println(rs.getString("authority_name"));  

        }  

        rs.close();  
        stmt.close();  
    } catch (SQLException e) {  
        System.out.println(e.getMessage().toString());  
    } finally {  
        if (con != null)  
            try {  
                con.close();  
            } catch (SQLException e) {  
            }  
    }  
}

}图片说明

4个回答

检查下sql server是否启用tcp/ip和远程访问,防火墙。

用1.2.7的jtds驱动

都先输出找不到类的错误了,你先检查一下是不是驱动jar包,没有正常导入到apk文件里吧。

连接数据库操作是不能在主线程中做的,需要建一个Threal来通过handle来操作

在onCreate中

        Thread thread = new Thread(cont);
        thread.start();

在onCreate外

    Runnable cont = new Runnable() {
        public void run() {
            String sql="select * from UserList where id=1";
            String ip = "192.168.128.5:1433/";
            try { 
                String jdbc = "jdbc:jtds:sqlserver://";
                String datebase = "database";
                String connectios = jdbc + ip + datebase;
                String UserName = "sa";
                String PassWord = "sa";
                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                conn = DriverManager.getConnection(connectios,UserName,PassWord);
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(sql);

                while (rs.next()) {
                    username = rs.getString("UserName");
                    phone = rs.getString("Phone");
                }
                    HashMap<String, String> obj = new HashMap<String, String>();
                    obj.put("username", username);
                    obj.put("phone", phone);
                    msg.obj = obj;
                    msg.what = 0;
                    handler.sendMessage(msg);
                rs.close();
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                handler.sendEmptyMessage(1);
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                handler.sendEmptyMessage(2);
                e.printStackTrace();
            }
        }
    };


    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                @SuppressWarnings("unchecked")
                HashMap<String, String> ma = (HashMap<String, String>) msg.obj;
                changelist.putExtra("username", ma.get("username"));
                changelist.putExtra("phone", ma.get("phone"));

                startActivity(changelist);
            }else if(msg.what == 1){
            }else if(msg.what == 2){
            }
        }
    };

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问