0

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

MJRLGUE
  • 55
  • 9

2 Answers2

1

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.

Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
  • thanks for answering, I am working on the same machine, my friend gave me the same settings settings, it worked for him and for me not !! still don't know why – MJRLGUE Jun 25 '17 at 18:31
  • Did you try to use SMSS to connect the sql server? The instance name could be incorrect. Also, since it is local try using localhost instead of MJRLGUE for the host name – Alexander Higgins Jun 25 '17 at 18:50
  • Also, can you verify the instance is running on the standard port of 1433? – Alexander Higgins Jun 25 '17 at 18:53
  • I did this: `String url = "jdbc:sqlserver://localhost;instanceName=SWING;databaseName="+dbname+";integratedSecurity = false;";` found it on [this link](https://technet.microsoft.com/en-us/library/ms378428(v=sql.105).aspx), and get the same errors – MJRLGUE Jun 25 '17 at 19:05
  • Unless you modified your instance to use port 1434 it should be running on 1433. try using String url = "jdbc:sqlserver://llocalhost:1433;instanceName=SWING;databaseName=‌​"+dbname+";integrate‌​dSecurity = false;"; – Alexander Higgins Jun 25 '17 at 19:10
  • Finally it works and I don't know how ^^, what I did is going to SMSS and found that `Named Pipes` was enabled, I disabled it and checked `TCP/IP properties`, I set Listen ALL in `Protocol properties ` to `YES` (before was set to `NO` because I was following a tutorial) and in `URL connection` I deleted port number and it works :D; final code: `String url = "jdbc:sqlserver://MJRLGUE;instanceName=SWING;databas‌​eName=‌​"+dbname+";i‌​ntegrate‌​dSecurity = false;";` – MJRLGUE Jun 26 '17 at 01:07
0

check your url and make sure server is running properly and port number and service free from firewall.

Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17