" com.microsoft.sqlserver.jdbc.SQLServerException:SQL Server未返回响应。"

时间:2017-03-02 10:38:39

标签: java sql-server jdbc connection

使用以下代码:

writeMessageLog

我得到以下输出:

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);
        }
    }
}

用户名,密码等都正确且服务器可访问(我通过成功创建与数据库管理工具的连接验证了这一点。)

为什么我会得到这个例外?

1 个答案:

答案 0 :(得分:0)

我在macOS上安装了“用于SQL Server的Microsoft ODBC驱动程序”(https://docs.microsoft.com/en-us/sql/connect/odbc/mac/installing-the-microsoft-odbc-driver-for-sql-server-on-macos),现在它可以工作了。感谢您的回复。