【问题标题】:Problem connecting android studio to Azure sql using java使用 java 将 android studio 连接到 Azure sql 的问题
【发布时间】:2022-01-20 05:42:00
【问题描述】:

我正在尝试将 android studio 连接到我的 Azure sql 云数据库。 运行代码我遇到此错误: “由于客户端 TLS 版本低于服务器允许的最低 TLS 版本,登录失败。”强> 我已经尝试将 azure 的 lts 设置为 1.0 版。

公共类 MainActivity 扩展 AppCompatActivity {

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

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    final Button button = findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {
                String SQL = "select * from dbo.Person";
                Class.forName(name);
                conn = DriverManager.getConnection(url);

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

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

}

【问题讨论】:

    标签: java android sql azure connection


    【解决方案1】:

    出现此错误的原因是防火墙中 Azure 端数据库的 TLS 版本设置不匹配,连接如图所示:

    以下屏幕截图是错误配置的示例:

    Azure 被要求使用高于 TLS 1.2 的 TLS 版本,但是,连接使用的是 TLS1.2

    这会导致不匹配。

    a) 防火墙(所选版本高于 TLS 1.2):

    b) 连接(在连接级别选择为 TLS1.2 与上述设置完全不匹配):

    因此,如果您在连接级别选择 TLS1.2,则 Azure 上的设置应选择为大于 TLS 1.1(即 >TLS 1.1)。

    要解决此问题,请确保选择兼容的 TLS 版本,以确保 Secure Agent 可以成功连接到数据库。

    此外,请确保为连接选择的加密方法是 SSL 或此方案的任何其他基于 SSL 的选项。

    这是因为如果加密方法设置为“无”,则不会在运行时为任务应用 TLS 设置,从而导致任务在运行时失败并显示相同的错误消息。

    【讨论】:

      猜你喜欢
      • 2020-08-06
      • 2015-12-13
      • 1970-01-01
      • 2018-01-14
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 2022-07-13
      • 2023-01-13
      相关资源
      最近更新 更多