0

I try, without any success, to connect my java project to my sql server database (local on my laptop).

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

public class JvaConnect2SQL {

    public static void main(String[] args) {
        
        String url = "jdbc:sqlserver://LAPTOP-0CSKUFIE\\MSSQLSERVER;databaseName=datatreck";
        String username = "sa";
        String password = "hello";
        
        try {
            Connection connection = DriverManager.getConnection(url, username, password);
            System.out.println("Connecté à SQL server");
        } catch (SQLException e) {
            System.out.println("Erreur - Problème lors de la connexion");
            e.printStackTrace();
        }

    }

}

The error is :

com.microsoft.sqlserver.jdbc.SQLServerException: La connexion à l'hôte LAPTOP-0CSKUFIE, instance nommée mssqlserver, a échoué. Erreur : « java.net.SocketTimeoutException: Receive timed out ». Vérifiez le nom du serveur et celui de l'instance et veillez à ce qu'aucun pare-feu ne bloque le trafic UDP vers le port 1434. Pour SQL Server 2005 ou ultérieur, vérifiez que le service SQL Server Browser est en cours d'exécution sur l'hôte.

English Translation:

com.microsoft.sqlserver.jdbc.SQLServerException: Connection to host LAPTOP-0CSKUFIE, instance named mssqlserver, failed. Error: "java.net.SocketTimeoutException: Receive timed out". Check the server name and instance name and ensure that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser service is running. running on the host.

It seems the name of the host and/or instance are not right ><

but I was sure it was the good ones, so I'm confuse ... How can I be sure of my host/instance names ? Is the code all right ?

I add exception to the fire wall and add the jdbc driver. I don't understand why the connexion fail.

Tim Jarosz
  • 1,133
  • 1
  • 8
  • 15
  • 1
    @BjorgP That advice is wrong, the [connection URL syntax](https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver16) uses a single backslash, so in a string literal it needs to be doubled, so 2 backslashes is correct here, and 4 is incorrect. – Mark Rotteveel Jan 11 '23 at 16:32

1 Answers1

1
  1. Just a simple tip: don't code or install applications in your native language. There are many reasons for doing that, but I am not going to list them here.

  2. If SQL server is running on your local computer/server requests should be made to localhost:port (for example localhost:1433)

  3. Check an example connection string and steps to connect to the database here: https://stackoverflow.com/a/71481219/19879452

Darius
  • 29
  • 4
  • 1
    Specifically, this: `LAPTOP-0CSKUFIE\\MSSQLSERVER` is a WINS or whatever kind of string and java doesn't know what those are; replace that with `127.0.0.1` and make sure your MySQL server accepts TCP/IP based connections. – rzwitserloot Jan 11 '23 at 14:32
  • @rzwitserloot That is not WINS, that is the SQL Server JDBC [connection URL syntax](https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver16), with hostname `LAPTOP-0CSKUFIE` and instance-name `MSSQLSERVER`. However, the driver expects either the SQL Server to listen on port 1434, and/or the SQL Server browser service to be running on that host to resolve the right instance. – Mark Rotteveel Jan 11 '23 at 16:35
  • thank you all for your help !!! It works now !! thanks to the tutorial ! – VincHawthorne Jan 12 '23 at 17:14