Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java &引用;com.microsoft.sqlserver.jdbc.SQLServerException:SQL Server未返回响应。”;_Java_Sql Server_Jdbc_Connection - Fatal编程技术网

Java &引用;com.microsoft.sqlserver.jdbc.SQLServerException:SQL Server未返回响应。”;

Java &引用;com.microsoft.sqlserver.jdbc.SQLServerException:SQL Server未返回响应。”;,java,sql-server,jdbc,connection,Java,Sql Server,Jdbc,Connection,使用以下代码: package com.anonymised.anonymised.anonymised; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class EntryP

使用以下代码:

package com.anonymised.anonymised.anonymised;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class EntryPoint
{
    private static Logger logger = LogManager.getLogger(EntryPoint.class);
    private static final String JDBC_DRIVER_NAME = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    public static void main(String[] args)
    {
        logger.info("Service started!");

        try
        {
            Class.forName(JDBC_DRIVER_NAME);
        }
        catch ( ClassNotFoundException e )
        {
            logger.error("The necessary JDBC driver is not available (missing dependency?)",e);
            System.exit(CrashCodes.JDBC_DRIVER_UNAVAILABLE);
        }

        // Credentials for connection to the DB
        String dbURL = "anonymised:1433";
        String dbDatabaseName = "anonymised";
        String dbUsername = "anonymised";
        String dbPassword = "anonymised";

        String connectionUrl = "jdbc:sqlserver://"+dbURL+";" + "databaseName="+dbDatabaseName+";user="+dbUsername+";password="+dbPassword+";";

        try
        {
            Connection con = DriverManager.getConnection(connectionUrl);
        }
        catch (SQLException e)
        {
            logger.error("Cannot connect to the database",e);
            System.exit(CrashCodes.DATABASE_INITIAL_CONNECTION_FAILED);
        }
    }
}
我得到以下输出:

[main] ERROR de.anonymised.anonymised.anonymised.EntryPoint - Cannot connect to the database
com.microsoft.sqlserver.jdbc.SQLServerException: SQL Server did not return a response. The connection has been closed.
用户名、密码等都是正确的,服务器是可访问的(我通过使用数据库管理工具成功创建连接验证了这一点)


为什么我会得到这个例外

我安装了“macOS上用于SQL Server的Microsoft ODBC驱动程序”(Microsoft ODBC Driver for macOS上的SQL Server),现在它可以工作了。感谢您的回复。

当您使用“数据库管理工具”连接时,是否
从sys.dm_exec_connections中选择本地_tcp_端口,其中session_id=@@SPID
返回1433?否,返回[Null]。因此该连接不会确认SQL Server是否可以通过tcp/IP访问(必须使用共享内存或命名管道连接)。SQL Server实例可能未侦听端口1433;您应该验证TCP/IP是否已启用,并确认它正在使用的端口号。请检查中的步骤