0
votes

Using this connection string

jdbc:sqlserver://localhost\\SQLEXPRESS:1433;database=mydb;user=username;password=psw

I am getting the following error that I cannot figure out how to solve it:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.

I looked at the registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\<Instance Name>\MSSQLServer\SuperSocketNetLib\Tcp, the port is set, and the firewall is disabled, the instance is running and the user does have master default access privilege!, So what was wrong?

3
your conncetion string look ok.. but jus try it this way and see jdbc:sqlserver://localhost;instanceName=SQLEXPRESS;database=mydb;user=username;password=psw'Ja9ad335h
Just tried that, getting: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot open database "mydb" requested by the login. The login failed.TiyebM
is database mydb exists? with given username and password permissions?Ja9ad335h
@JAG thank you for letting me thinking about it, I didn't change the db but the user, using sa with its password it works, I don't know what was wrong then?!TiyebM
So how can I give the permission to another login besides sa on this db?, please put an answer for this whole problem now, @JAG, thanks.TiyebM

3 Answers

4
votes

try this connection string

jdbc:sqlserver://localhost;instanceName=SQLEXPRESS;database=mydb;user=username;‌​password=psw'

and make sure you have this

instanceName = SQLEXPRESS;
database = mydb;  -- database mydb should exists
user = userName;‌ -- user should exits and have permission to mydb
​password = userPassword

to create new user

USE [master]
GO
CREATE LOGIN [userName] WITH PASSWORD=N'userPassword', 
             DEFAULT_DATABASE=[YourDB], CHECK_EXPIRATION=OFF,CHECK_POLICY=OFF

to give access to YourDB

GO
USE [YourDB] -- in your case its mydb
GO
CREATE USER [userName] FOR LOGIN [userName] WITH DEFAULT_SCHEMA=[dbo]
GO
1
votes

Go to SQL Server Configuration Manager -->> Protocols for SQLEXPRESS in the right tab change the properties of TCP/IP 1- Enable the protocol 2- At IP Addresses : make sure that all TCP Port are 1433 3- Apply -->> Ok 4- Restart SQL Server

I hope it worked well

0
votes

Try omitting the port number, or use a comma in place of the colon.

jdbc:sqlserver://localhost\\SQLEXPRESS;database=mydb;user=username;password=psw

jdbc:sqlserver://localhost\\SQLEXPRESS,1433;database=mydb;user=username;password=psw