KonradK
KonradK

Reputation: 53

Connecting to SQL Server 2014 from Android Studio

I have a problem connecting to SQL-server database through from my android project. I have added sqljdbc41.jar file to my /app/libs directory and I have added it to dependencies in my android studio project.

I use following code:

package com.konrad.rezerwacje1;

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



public class Database_Console {

public static void openConnection(){
    try {
        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"‌​);
        String url = "jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db";
        Connection con = DriverManager.getConnection(url);
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args){
    openConnection();
}
}

yet i still get this error

java.sql.SQLException: No suitable driver found for jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)

Upvotes: 0

Views: 3579

Answers (2)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59978

Instead of this :

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"‌​);
String url = "jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db";

You have to use this :

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=my_db";

Connection con = DriverManager.getConnection(url, "username", "password");

Note the different classname, and the fact that prefix jbdc in the URL has been changed to jdbc.

Upvotes: 2

N00b Pr0grammer
N00b Pr0grammer

Reputation: 4647

If it is not a requirement to go with sqljdbc41.jar, then you might consider using the jtds driver for your requirement to connect to SQL Server 2014 with Android Studio. There are tons of articles that can help you start with this set of technologies.

For a primer, here are the details:

  • Download the JTDS driver from here
  • Then import this jar into your Android Studio, eg: jtds-1.2.5.jar
  • Use the following details in your code:

    Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
    DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433/DATABASE;user=sa;password=p@ssw0rd");
    

Upvotes: 0

Related Questions