Search code examples
sql-serverdatabasejdbcjtds

No Suitable Driver for JDBC in Android App


I have downloaded the jar JDBC file and successfully added into my app/src/lib file. Furthermore I also tried with the JTDS-JDBC library but I was getting the same exact exceptions which were:

  1. Network exception can't connect to 127.0.0.1/1433 ECOREFUSED

  2. java.sql.SQLException: No suitable driver

For Exception #1 I searched out and found some posts that the main reason for this exception to be thrown is the lack of memory, hence I increased SQLServer's memory to 4GB which didn't solve my problem.

For both libraries I tried to establish a connection in order to achieve an INSERTION into my SQLServer database with the only difference that for JTDS-JDBC I used the following:

Class.forName("net.sourceforge.jtds.jdbc.Driver"))

I'm running the following code:

try {
     /*Declare the JDBC objects.*/
     Connection con = null;
     Statement stmt = null;
     ResultSet rs = null;


     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

     String constring = "jdbc:sqlserver://localhost:1433;databaseName=bumpit;integratedSecurity=true;";

     con = DriverManager.getConnection(constring);

     /*Create and execute an SQL statement.*/
     stmt = con.createStatement();
     String SQL = "INSERT INTO BumpItUsers (name,address,mobile,password) VALUES ('" + name + "','" + address + "','" + address + "','" + password + "')";
     rs = stmt.executeQuery(SQL);


     con.close();
     return true;

     } catch (Exception e) {
     e.printStackTrace();
     return false;
     }
 }

Any solution will be accepted!


Solution

  • First, localhost, from the standpoint of the Android device, is the Android device. This is how localhost works everywhere: it refers to the machine on which the code is running. SQL Server does not run on Android. You would need to change your code to refer to the actual IP address or domain name of the machine running SQL Server.

    Second, few developers use JDBC on Android, for reliability and security reasons.