java - 使用 JDBC 连接到 Azure SQL 数据仓库时出现 SQLException

标签 java sql-server azure azure-synapse

我正在尝试通过 JDBC 连接到 Azure SQL 数据仓库。我收到以下异常。

*

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host tcsqldatawh.database.windows.net, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:242)
    at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2280)
    at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:493)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1387)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1068)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:904)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:451)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1014)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at testsqldw.SQLDatabaseConnection.main(SQLDatabaseConnection.java:30)

*

我看到过有关连接到 SQLServer DB 的类似问题 here

我将数据库配置为允许使用进程 here 访问我的 IP .

请检查以下代码:

package testsqldw;

import java.sql.*;  

    public class SQLDatabaseConnection {  

        // Connect to your database.  
        // Replace server name, username, and password with your credentials  
        public static void main(String[] args) {  


            String connectionString =  
                    "jdbc:sqlserver://databaseserver.database.windows.net:1433;"
                    +"database=databaseserver;"
                    +"user=username@databaseserver;"
                    + "password=password;"
                    + "encrypt=true;"
                    + "trustServerCertificate=false;"
                    + "hostNameInCertificate=*.database.windows.net;"
                    + "loginTimeout=30;";

            System.out.println("Total connection string is---\n"+connectionString);

            // Declare the JDBC objects.  
            Connection connection = null;  
            Statement statement = null;   
            ResultSet resultSet = null;  

            try {  
                connection = DriverManager.getConnection(connectionString);  

                // Create and execute a SELECT SQL statement.  
                String createSql = "create table employee(employee_id varchar(20));";  
                statement = connection.createStatement();  
                boolean status=statement.execute(createSql);

                System.out.println(status);

                // Print results from select statement  

            }  
            catch (Exception e) {  
                e.printStackTrace();  
            }  
            finally {  
                // Close the connections after the data has been handled.  

                if (statement != null) try { statement.close(); } catch(Exception e) {}  
                if (connection != null) try { connection.close(); } catch(Exception e) {}  
            }  
        }  
    }  

总连接字符串为

jdbc:sqlserver://databaseserver.database.windows.net:1433;database=databaseserver;user=username@databaseserver;password=password;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;

请帮我解决这个问题。

最佳答案

根据我的经验,该问题可能由以下原因引起:

  1. 您创建的 SQL Server 可能无法正常工作。
  2. 您可能使用了不正确的 JDBC 连接字符串。

对于第一个原因,您可以使用一些客户端工具连接到 SQL 服务器。如果能连接到服务器,说明SQL服务器是好的。 如果没有,您可以创建一个新的 SQL Server。之后,您可以根据此URL https://azure.microsoft.com/en-us/documentation/articles/sql-data-warehouse-get-started-provision/创建一个新的SQL数据仓库。 。 该 URL 还包含防火墙配置方法。

我使用 SQL Server InTouch 连接到 SQL Server。以下是一些描述图片。

enter image description here

您可以通过Azure Portal获取参数。端口号为1433。

出现下图说明您的服务器正常。

enter image description here

对于第二个原因,您可以从 Azure 门户复制连接字符串并仅修改密码。

enter image description here

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

关于java - 使用 JDBC 连接到 Azure SQL 数据仓库时出现 SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39654292/

相关文章:

java - 如何使用Java xuggler合并视频中的两种不同音频

Java/Swing(以及一般的 GUI)- 使用条件/过滤器构建动态搜索表单的技术或模式

c# - 如何从 C# 中的存储过程中获取设置值

sql-server - 使用全文在多个表中搜索

azure - 在 ARM 模板 IPRestriction 中包含前门 ID

java - Drools Flow 人工任务示例 : How do I prevent 'Named query not found' exception being thrown?

java - 为什么Java不强制对枚举属性使用final

返回范围的 SQL Select 查询

azure - APIM : Limit concurrent open websocket connections

azure - Azure Blob 索引器的问题