javasql-serversqljdbc

Failed to connect to host MyServerName


Hi guys I am trying to connect NetBeans IDE with SqlServer, everything I did is correct, setting ports to 1433 in SQL Server Configurtion Manager and here is my code:

public Db(String login,String password, String dbname) throws SQLException{

        try {
            String url = "jdbc:sqlserver://MJRLGUE\\SWING;databaseName="+dbname+";integratedSecurity = false;";
            Driver monDriver = new SQLServerDriver();
            DriverManager.registerDriver(monDriver);
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(url,login,password);
            this.connecte = true;
            System.out.println("Succes");
      } catch (ClassNotFoundException e) {
            System.out.println("echec driver "+e.toString());
            this.connecte = false;
        }

    }

and the error message:

Try again com.microsoft.sqlserver.jdbc.SQLServerException: Failed to connect to host MJRLGUE, named instance swing. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server name and the instance name, and make sure that no firewalls block UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser service is running Execution on the host.

Exception in thread "main" java.lang.NullPointerException
    at Model.Test.remplirDirecteur(Test.java:79)
    at Main.main(Main.java:17)
C:\Users\Ghassane\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 29 seconds)

my instance name in SqlServer: MJRLGUE\SWING code to connect the database: db= new Db("sa","ensak","SuiviMarche");


Solution

  • Check that you can ping the sql server from the machine running the code.

    If you can you may need to to configure the sql server to allow remote connections, as you have said you already checked the firewall and I assumed the instance is running on the standard port of 1433

    Also, check that you can you use SMSS to connect to the sql server from the machine running the code.

    Edit:

    Looking at your error message you are not using the standard port of 1433 as the message says you are trying to connect to 1434.

    make sure that no firewalls block UDP traffic to port 1434

    try using jdbc:sqlserver://localhost:1433 as your host.