Good day.

The question is as follows:

There is sql server 2014. You need to connect to it from java-code (I work in the netbins).

I try this way:

String connectionString = "Data Source=servername;Initial Catalog=dbName;User id=user;Password=password;" Connection conn = DriverManager.getConnection(connectionString); 

The exception is poured:

java.sql.SQLException: No suitable driver found ...

The driver is in the lib folder of the project + I connect the driver directly in the code before the connection is established:

 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

If I use the query type string:

 "jdbc:sqlserver://localhost:1433;user=name;password=password;database=dbName" 

That exception falls:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP / IP connection has failed. Error: "Connection refused: connect ...

In server services checked - all ports are included with values ​​of 1433 ...

Already run out of ideas where to dig?

Help, pliz.

  • and server and code on the same machine? - ermak0ff
  • Yes, everything works on the same machine. - mihoxt
  • The DriverManager.getConnection method always accepts a jdbc url of the form jdbc:subprotocol:subname . Therefore, ODBC-shnaya connection string just does not fit. So you need to deal with the port. - Nofate
  • Execute netstat -a -n in the console and show lines with port 1433. - Roman
  • for some reason, there is not a single entry with such a port ... but after all, I set port 1433 everywhere in the configuration manager. Does it still work on another port? - mihoxt

1 answer 1

  1. java.sql.SQLException: No suitable driver found ...

Your program does not see the file with the driver. Obviously, putting it in the lib folder is not enough; you need to configure your IDE so that it perceives this folder properly. How to do this in Netbeans will not say. If you run your program from the command line, add the driver file to the CLASSPATH.

  1. com.microsoft.sqlserver.jdbc.SQLServerException: The TCP / IP connection has failed. Error: "Connection refused: connect ...

Do you understand that localhost is your local computer? Is SQL Server installed and running directly on your local computer?

  • 1. when I connect the driver with the string Class.forName ("com.microsoft.sqlserver.jdbc.SQLServerDriver"); - after that I can access it from the code (for example, find out the version) and everything works out the rules ... how can it be that one part of the program sees the driver and the other does not? - mihoxt
  • 2. yes, the server on the local computer - mihoxt
  • There may be a typo somewhere in the name about the lack of a driver. And about the connection here, look. I think your connection string is not correct. - Igor Kudryashov