0

Using the code below:

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

I get the following output:

[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.

The user name, password, etc. are all correct and the server is accessible (I verified that by successfully creating a connection with a database administration tool).

Why then do I get this exception?

  • When you connect with your "database administration tool" does `SELECT local_tcp_port FROM sys.dm_exec_connections WHERE session_id=@@SPID` return 1433? – Gord Thompson Mar 02 '17 at 12:04
  • No, it returns [Null]. –  Mar 02 '17 at 12:57
  • So that connection does not confirm that the SQL Server is accessible *via TCP/IP* (it must be using a shared memory or named pipe connection). The SQL Server instance may not be listening on port 1433; you should verify that TCP/IP is enabled and confirm the port number it is using. – Gord Thompson Mar 02 '17 at 13:24
  • Please check the steps in http://stackoverflow.com/a/18850073/466862 – Mark Rotteveel Mar 02 '17 at 18:51

1 Answers1

0

I installed the "Microsoft ODBC Driver for SQL Server on macOS" (https://learn.microsoft.com/en-us/sql/connect/odbc/mac/installing-the-microsoft-odbc-driver-for-sql-server-on-macos) and now it works. Thank your for your replies.