Could not connect to the database Network error IOException: Connection refused: connect

Tags: ,



I am running a SQL Server Express 10.50.4000.0 on my machine. I have enabled on the TCP/IP and made sure the port is set to 1433. I have added an exception in my firewall for 1433. The server service is running. I have tried localhost and the computer name and both give the same error. When i try localhostSQLEXPRESS or [USER-PC]SQLEXPRESS I received an error saying it could not find the database.

String driverName = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driverName);

String serverName = "localhost";
String instanceName = "ALLEN-PC\SQLEXPRESS";
String serverPort = "1433";
String database  = serverName +":" + serverPort+"/"+instanceName;
String url = "jdbc:jtds:sqlserver://" + database;
String username = "sa";
String password = "password";

connection = DriverManager.getConnection(url, username, password);

I receive the error:

Could not connect to the database Network error IOException: Connection refused: connect

with no additional context to investigate.

Answer

Your connection string needs to be in this format. Found here: http://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx

jdbc:jtds:sqlserver://<yourDBServerIPAddress>SQLEXPRESS:1433;databaseName=AdventureWorks;user=sa;password=*****;


Source: stackoverflow