且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android Studio无法连接到Azure SQL Server中的数据库

更新时间:2023-02-03 10:31:02

我尝试通过java jdbc连接我的sqlserver,但没有重现您的问题.

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.

您需要修改您的连接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/

如何使用Android中的JTDS驱动程序


更新答案:


Update answer:

我已经对您的连接URL进行了一些调整,并且可以正常连接到我的应用程序数据库.

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();
    }

请注意,删除 database = *** 并在主机字符串之后添加"/<您的数据库名称>" .

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.

希望它对您有帮助.