Skip to content Skip to sidebar Skip to footer

Android Studio Can't Connect To Database In Azure Sql Server

I am using android studio to develop an application and using Azure Sql Server to host my database. The problem is I was able to connect to my database on SQL server but it has an

Solution 1:

I tried to connect my sqlserver via java jdbc and did not reproduce your issue.

I can connect to my application db successfully.

My test code:

import java.sql.*;

publicclassTest {

    publicstaticfinalStringurl="jdbc:sqlserver://***.database.windows.net:1433;database=***;user=***password=***;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
    publicstaticfinalStringname="com.microsoft.sqlserver.jdbc.SQLServerDriver";

    publicstaticConnectionconn=null;
    publicstaticPreparedStatementpst=null;
    publicstaticStatementstmt=null;
    publicstaticResultSetrs=null;

    publicstaticvoidmain(String[] args) {

        try {

            StringSQL="select * from dbo.Student";
            Class.forName(name);
            conn = DriverManager.getConnection(url);

            stmt = conn.createStatement();
            rs = stmt.executeQuery(SQL);

            while (rs.next()) {
                System.out.println(rs.getString("name"));
            }
            close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    publicstaticvoidclose() {
        try {
            conn.close();
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

After some research, I found out it is because of your connect url.

You need to modify your connect url :

jdbc:jtds:sqlserver://haozailai.database.windows.net:1433/<your application db name> ...

You could refer to the pages below for more details.

  1. https://sourceforge.net/p/jtds/discussion/104389/thread/a672d758/

  2. how to connect sql server using JTDS driver in Android


Update answer:

I have made a slight adjustment to your connect URL and can connect to my application database normally.

try {

        String SQL = "select * from dbo.Student";

        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        String url = String.format("jdbc:jtds:sqlserver://***.database.windows.net:1433/<your database name>;user=***;password=***;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;");
        conn = DriverManager.getConnection(url);

        stmt = conn.createStatement();
        rs = stmt.executeQuery(SQL);

        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }
        close();
    } catch (Exception e) {
        e.printStackTrace();
    }

Notice that remove the database=*** and add "/<your database name>" after your host string.

Please refer to the above code and try again.Any concern, please let me know.

Hope it helps you.

Solution 2:

I know this answer is waay too late, but I found this video that totally works: https://www.youtube.com/watch?v=WJBs0zKGqH0

The thing is, you have to download a jtds jar, the guy in the video says where you can get it from and also, you need to add "jtds" before "sqlserver" in connection url and edit the way the 'databe' is written in the connection url, like this: jdbc:jtds:sqlserver://serverName.database.windows.net:portNr:DatabaseName=dbName;user=....

Post a Comment for "Android Studio Can't Connect To Database In Azure Sql Server"