0

what ever I did I get the same error NO SUITABLE DRIVER I have sql server 2014 and I am using Eclipse mars I tried every jtds i found no solution I am new on android and java, i really need help.

ListView lv = null;
Connection connx = null;
SimpleAdapter sa = null;
String sqlreq = "select * from news";
@SuppressLint("NewApi")
private Connection cnx() //connection to sqlserver
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy);
    Connection conn = null;
    String ConnUrl = null;



    try 
    {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnUrl = "jdbc:jdts:sqlserver://xxxxxx:1433;DatabaseName=xxxx;ssl=require";
        conn=DriverManager.getConnection(ConnUrl);

    } catch (SQLException e) 
    {
        Log.e("Error", e.getMessage());
    }
    catch (ClassNotFoundException e) 
    {
        Log.e("Error", e.getMessage());
    }
    catch (Exception e) 
    {
        Log.e("Error", e.getMessage());
    }
    return conn;
}



public void SqlQuery(String SqlCmnd) 
{
    connx = cnx();
    ResultSet rs;
    try 
    {
        Statement stm = connx.createStatement();
        rs = stm.executeQuery(SqlCmnd);
        List<Map<String, String>> data = null;
        data = new ArrayList<Map<String, String>>();

        while (rs.next()) {

            Map<String, String> datanum = new HashMap<String, String>();
            datanum.put("A", rs.getString("title"));
            datanum.put("B", rs.getString("idnews"));
            data.add(datanum);
        }
        String[] from = {"A","B"};
        int [] views ={R.id.Titleid,R.id.Articleid};
        sa = new SimpleAdapter(this, data, R.layout.model, from, views);
        lv.setAdapter(sa);
    } 
    catch (Exception e) {
        Log.e("Error", e.getMessage());
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    lv = (ListView)findViewById(R.id.listView1);
    setContentView(R.layout.activity_main);
    SqlQuery(sqlreq);
}
mubeen
  • 813
  • 2
  • 18
  • 39
  • You donot have to connect android with database server. To access database data you had to create webservices and call that services from your android – mubeen Apr 12 '16 at 07:20
  • You can learn it. This is tutorial for restful webservices https://www.youtube.com/watch?v=xkKcdK1u95s . This may help you. There are also lot of tutorial over internet. – mubeen Apr 12 '16 at 07:22
  • thanks but i want to connect it to my Sql Server Db to learn how to. Actually i did solve some errors and now it show another error **_04-12 05:32:46.523: E/Error(2564): Network error IOException: failed to connect to /xxxxxx (port 1433): connect failed: ETIMEDOUT (Connection timed out)_** i just don't know what localhost should i use – Fouad el hssayni Apr 12 '16 at 09:34
  • If you are using localhost and running on emulator then try use 10.0.2.2 http://developer.android.com/tools/help/emulator.html And If you are using physical device make sure you are connected to same network and give the development machine ip adress – mubeen Apr 12 '16 at 09:54
  • i m using an emulator i tried what u told me and its not wroking, the same error .here is my connection url **_jdbc:jtds:sqlserver://10.0.2.2/PFFDB;ssl=required";_** (PFFDB is my databasename) – Fouad el hssayni Apr 12 '16 at 11:36
  • I donot know why it donot work. Try if this work, add port after ip address. Like jdbc:jtds:sqlserver://10.0.2.2:1433/PFFDB;ssl=required – mubeen Apr 12 '16 at 12:19
  • thanks a lot but its not working it take too long i don't know why and it gives me the same error **_connect failed : ETIMEDOUT ..._** – Fouad el hssayni Apr 12 '16 at 13:07
  • It is not recommended to connect android with database server. You should use web services or implement an HTTP handler – mubeen Apr 12 '16 at 13:35
  • yeah sure but what if i want to connect it to sql server DB there is no solution i could use for my error – Fouad el hssayni Apr 12 '16 at 14:56

0 Answers0