Connecting android app to sql server using jtds

荒凉一梦 提交于 2019-12-11 08:40:12

问题


I'm tired of searching with no result i don't know where the problem come from , i'm trying to connect my android app with the local SQl server 2008, now i get unable to get information from sql server. please help this is my code :

String url = "jdbc:jtds:sqlserver://10.0.2.2:1433/lear_db;instance=SQLEXPRESS;";
TextView coucou = new TextView(this);

    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        java.sql.Connection connexion =               
        DriverManager.getConnection(url,"kamal","kamal1234"); 

        coucou.setText("Connection successful");

    } catch ( SQLException e) {
        // TODO Auto-generated catch block
        coucou.setText("error1" + e.getMessage());
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        coucou.setText("error2" + e.getMessage());
        e.printStackTrace();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        coucou.setText("error0" + e.getMessage());
        e.printStackTrace();
    }   
    setContentView(coucou);

回答1:


This is the connexion string that worked for me :

"jdbc:jtds:sqlserver://"+ adresseip +":1433/lear_db;user=youruser;password=yourpass"

than i got an other error wich is : android.os.NetworkOnMainThreadException

the solution is to use AsyncTask this is a tutorial : http://droidapp.co.uk/2011/05/12/android-dev-pre-loading-with-asynctask/

now i'm having a new error ( This errors never ends ) :

Unknow server host name 'unable to resolve host  

Than it worked , on wifi with ip adresse of the host .




回答2:


Usually, I use jdbc:sqlserver in this fasion

String url = "jdbc:sqlserver://10.0.2.2:1433/lear_db;instance=SQLEXPRESS;";

or alternatively if using jtds, try:

String url = "jdbc:jtds://10.0.2.2:1433/lear_db;instance=SQLEXPRESS;";


来源:https://stackoverflow.com/questions/15276971/connecting-android-app-to-sql-server-using-jtds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!