gpt4 book ai didi

java - 如何使用 jTDS 连接到 Azure SQL 数据库

转载 作者:太空宇宙 更新时间:2023-11-03 14:47:32 24 4
gpt4 key购买 nike

我尝试通过以下方式连接到 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)

有趣的是,我可以使用相同的 JDBC 驱动程序从同一台计算机连接到数据库 SQuirreL SQL (虽然没有 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?

最佳答案

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

但是,使用jTDS 代替Microsoft JDBC 驱动程序用于SQL Server 有一点不同,您可以引用the step 3 of the tutorial 中的注释。知道它。作为引用,我把笔记的内容贴在这里。

Note:

If you are using the JTDS JDBC driver, then you will need to add "ssl=require" to the URL of the connection string and you need to set the following option for the JVM "-Djsse.enableCBCProtection=false". This JVM option disables a fix for a security vulnerability, so make sure you understand what risk is involved before setting this option.

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

关于java - 如何使用 jTDS 连接到 Azure SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37743441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com