Search code examples
javasqlintellij-ideasql-server-2014-expressconnector-j

connecting to sql server 2014 database from basic java application?


I am following along with the book "learn java for web development" by vishal layka at present. But I have run into an issue which I will describe as best I can here. Essentially I cant connect to my database called "book" with connectorj 5.0.8. the database is running locally on microsft sql server 2014. this is the method iam using to get the connection.

private Connection getConnection() throws SQLException
{
     return DriverManager.getConnection("jdbc:mysql://localhost:3306/books","eoin", "");
}

iam getting this output when iam trying to get the connection.

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

 ** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException
MESSAGE: Connection refused: connect

I edited my sql server configuration in the "sql server configuration manager" tool so that it operates on port 3306 and it is enabled on localhost. the service but still no joy. below shows an image of this config.

enter image description here

iam thinking maybe my lack of knowledge of sql server is holding me back here. another thing i think that may be the issue is in sql management studio. when I try to change the login properties password. this is circled in the image below.

enter image description here

when i change this password.then click "ok" then reenter the "login properties for this db user then password I previously changed seems to have returned to what it was originally? I may be barking up the wrong tree altogether but iam not too sure? I hope the question was clear enough and any help is appreciated!

just a quick edit. Ive also tried turning off my firewall completely and I still couldnt connect.


Solution

  • First of all your connection string is wrong.

    jdbc:mysql://localhost:3306/books
    

    Change it to

    jdbc:sqlserver://localhost:3306/books
    

    See connection string info here.

    Second, you may need to open TCP/IP in protocol since by default SQL server express has only Named Pipes open. See how to open TCP/IP Protocol here.