Android studio can't connect to database in Azure sql server

孤街浪徒 提交于 2019-12-22 17:09:05

问题


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 error of Object not found in my database.

I found out that it might be connecting to my master database instead of the database I want it to connect to. Is there any solution to solve the problem?

package com.example.lenovo.testing1;

import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;

import java.sql.*;

public class ConnectionClass {
    String hostName = "haozailai.database.windows.net";
    String dbName = "haozailai";
    String user = "username";
    String password = "password";

    @SuppressLint("NewApi")
    public Connection CONN() {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        String ConnURL;
        Connection conn = null;

        try {

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

        }catch (SQLException se)
        {
            Log.e("error here 1 : ", se.getMessage());
        }
        catch (ClassNotFoundException e)
        {
           Log.e("error here 2 : ", e.getMessage());
        }
        catch (Exception e)
        {
            Log.e("error here 3 : ", e.getMessage());
        }


        return conn;


    }
}

Picture of my database structure


回答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.*;

public class Test {

    public static final String url = "jdbc:sqlserver://***.database.windows.net:1433;database=***;user=***password=***;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
    public static final String name = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    public static Connection conn = null;
    public static PreparedStatement pst = null;
    public static Statement stmt = null;
    public static ResultSet rs = null;

    public static void main(String[] args) {

        try {

            String SQL = "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();
        }

    }

    public static void close() {
        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.




回答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=....



来源:https://stackoverflow.com/questions/47202540/android-studio-cant-connect-to-database-in-azure-sql-server

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