Android Studio无法连接到Azure SQL Server中的数据库 [英] Android studio can't connect to database in Azure sql server

查看:87
本文介绍了Android Studio无法连接到Azure SQL Server中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用android studio开发应用程序,并使用Azure Sql Server托管我的数据库.问题是我能够连接到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 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;


    }
}

我的数据库结构图片

推荐答案

我尝试通过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.

希望它对您有帮助.

这篇关于Android Studio无法连接到Azure SQL Server中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆