【问题标题】:How to connect to Azure SQL database with jTDS如何使用 jTDS 连接到 Azure SQL 数据库
【发布时间】:2016-06-10 08:26:46
【问题描述】:

我尝试通过以下方式连接到 Azure SQL:

import java.sql.*;

public class ExampleJTDS {

    public static void main(String[] args) {

        // Setting.
        String connectionUrl = "jdbc:jtds:sqlserver://SERVER.database.windows.net:1433/DATABASE;ssl=off";
        String user = "USER@SERVER";
        String pass = "PASSWORD";

        // Declare the JDBC object.
        Connection conn = null;

        try {
            // Establish the connection.
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            conn = DriverManager.getConnection(connectionUrl, user, pass);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

但我得到了:

java.sql.SQLException: I/O Error: DB server closed connection.
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2481)
    at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:632)
    at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:371)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at run.ExampleJTDS.main(ExampleJTDS.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

如果我通过将 ssl=off 替换为 ssl=require 来强制加密,我会得到:

java.sql.SQLException: Network error IOException: Connection reset
    at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at run.ExampleJTDS.main(ExampleJTDS.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

有趣的是,我可以通过SQuirreL SQL 从同一台计算机和相同的 JDBC 驱动程序连接到数据库(尽管没有 SSL - SQuirreL SQL 设法将凭据放入第一个 TDS 数据包,Azure 接受)。因此,问题不应该出在防火墙的设置上。

元数据:

  • 服务器:Azure V12
  • 驱动:jtds-1.3.1
  • JRE:1.8.0_72-b15(来自 Oracle)
  • _JAVA_OPTIONS:-Djsse.enableCBCProtection=false
  • security.provider.1:sun.security.provider.Sun
  • 操作系统:OS X 10.11.5
  • SQuirreL SQL:3.7.1

如何从 Java 连接到 Azure SQL?

【问题讨论】:

标签: java azure ssl jdbc azure-sql-database


【解决方案1】:

根据我的经验,我认为问题是由连接字符串引起的,它是您代码的变量 connectionUrl。我已经回答了您关于 SO 线程的类似问题,请参阅How to connect to Azure SQL with JDBC

但是,使用jTDS代替Microsoft JDBC driver for SQL Server有一点区别,可以参考the step 3 of the tutorial中的注释了解。作为参考,我把笔记的内容贴在这里。

注意:

如果您使用的是 JTDS JDBC 驱动程序,则需要在连接字符串的 URL 中添加“ssl=require”,并且需要为 JVM 设置以下选项“-Djsse.enableCBCProtection=false”。此 JVM 选项禁用对安全漏洞的修复,因此请确保在设置此选项之前了解所涉及的风险。

希望对您有所帮助。有任何问题,请随时告诉我。

【讨论】:

    猜你喜欢
    • 2020-04-12
    • 2020-07-20
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    相关资源
    最近更新 更多