Create connection to SQL database using JDBC Driver- SQLException












0















I am trying to connect SQL database to java. I added the driver jar from maven: com.microsoft.sqlserver:mssql-jdbc:7.0.0.jre102. I was using this sites to create a proper connection:



Implementing connection in java: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-the-jdbc-driver?view=sql-server-2017



Creating a login to the database: https://docs.microsoft.com/en-us/sql/relational-databases/security/authentication-access/create-a-login?view=sql-server-2017



Enabling named pipes and TCP/IP connections: https://www.blackbaud.com/files/support/infinityinstaller/content/installermaster/tkenablenamedpipesandtcpipconnections.htm



But still- there is thrown an SQLException:



com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4, port 1433 has failed. Error: "LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".



Code of my tester class:



package main;
import java.sql.*;

public class Main {

public static void main(String args) {

try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

String connectionUrl = "jdbc:sqlserver://LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4:1433;" +
"databaseName=TestBazyDanych;user=doszke;password=doszke123;";

Connection con = null;

try {
con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
e.printStackTrace();
}

if(con != null){
System.out.println("connected");
} else {
System.out.println("unable to connect");
}

}


}


I got one problem during TCP/IP connections setting. I cannot restart my server from SSMS and I did it via SQL Server Configuration Manager:
SSMS- server cannot be restarted



Server restarted from SSCM










share|improve this question


















  • 1





    The Microsoft SQL Server JDBC driver cannot connect to a LocalDB, only to a real server.

    – Mark Rotteveel
    23 hours ago











  • Hmm, what could you recommend to use to connect to my local server?

    – Embid123
    23 hours ago






  • 1





    You need to use the real server. I'd start with trying jdbc:sqlserver://localhost;databaseName=TestBazyDanych, then try with the port configured in the SQL Server setting of that host (jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych) or start the SQL Server Browser service and use its instance name (jdbc:sqlserver://localhost<instanceName>;databaseName=TestBazyDanych (note the use of backslash instead of forward slash). But first of all, check your TCP/IP configuration. Default installations use a random port (if TCP/IP is enabled at all), not port 1433.

    – Mark Rotteveel
    23 hours ago













  • The second one worked for me fine, only an exception is thrown: login to database failed. But I think I can manage it. Thank you.

    – Embid123
    23 hours ago
















0















I am trying to connect SQL database to java. I added the driver jar from maven: com.microsoft.sqlserver:mssql-jdbc:7.0.0.jre102. I was using this sites to create a proper connection:



Implementing connection in java: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-the-jdbc-driver?view=sql-server-2017



Creating a login to the database: https://docs.microsoft.com/en-us/sql/relational-databases/security/authentication-access/create-a-login?view=sql-server-2017



Enabling named pipes and TCP/IP connections: https://www.blackbaud.com/files/support/infinityinstaller/content/installermaster/tkenablenamedpipesandtcpipconnections.htm



But still- there is thrown an SQLException:



com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4, port 1433 has failed. Error: "LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".



Code of my tester class:



package main;
import java.sql.*;

public class Main {

public static void main(String args) {

try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

String connectionUrl = "jdbc:sqlserver://LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4:1433;" +
"databaseName=TestBazyDanych;user=doszke;password=doszke123;";

Connection con = null;

try {
con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
e.printStackTrace();
}

if(con != null){
System.out.println("connected");
} else {
System.out.println("unable to connect");
}

}


}


I got one problem during TCP/IP connections setting. I cannot restart my server from SSMS and I did it via SQL Server Configuration Manager:
SSMS- server cannot be restarted



Server restarted from SSCM










share|improve this question


















  • 1





    The Microsoft SQL Server JDBC driver cannot connect to a LocalDB, only to a real server.

    – Mark Rotteveel
    23 hours ago











  • Hmm, what could you recommend to use to connect to my local server?

    – Embid123
    23 hours ago






  • 1





    You need to use the real server. I'd start with trying jdbc:sqlserver://localhost;databaseName=TestBazyDanych, then try with the port configured in the SQL Server setting of that host (jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych) or start the SQL Server Browser service and use its instance name (jdbc:sqlserver://localhost<instanceName>;databaseName=TestBazyDanych (note the use of backslash instead of forward slash). But first of all, check your TCP/IP configuration. Default installations use a random port (if TCP/IP is enabled at all), not port 1433.

    – Mark Rotteveel
    23 hours ago













  • The second one worked for me fine, only an exception is thrown: login to database failed. But I think I can manage it. Thank you.

    – Embid123
    23 hours ago














0












0








0








I am trying to connect SQL database to java. I added the driver jar from maven: com.microsoft.sqlserver:mssql-jdbc:7.0.0.jre102. I was using this sites to create a proper connection:



Implementing connection in java: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-the-jdbc-driver?view=sql-server-2017



Creating a login to the database: https://docs.microsoft.com/en-us/sql/relational-databases/security/authentication-access/create-a-login?view=sql-server-2017



Enabling named pipes and TCP/IP connections: https://www.blackbaud.com/files/support/infinityinstaller/content/installermaster/tkenablenamedpipesandtcpipconnections.htm



But still- there is thrown an SQLException:



com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4, port 1433 has failed. Error: "LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".



Code of my tester class:



package main;
import java.sql.*;

public class Main {

public static void main(String args) {

try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

String connectionUrl = "jdbc:sqlserver://LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4:1433;" +
"databaseName=TestBazyDanych;user=doszke;password=doszke123;";

Connection con = null;

try {
con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
e.printStackTrace();
}

if(con != null){
System.out.println("connected");
} else {
System.out.println("unable to connect");
}

}


}


I got one problem during TCP/IP connections setting. I cannot restart my server from SSMS and I did it via SQL Server Configuration Manager:
SSMS- server cannot be restarted



Server restarted from SSCM










share|improve this question














I am trying to connect SQL database to java. I added the driver jar from maven: com.microsoft.sqlserver:mssql-jdbc:7.0.0.jre102. I was using this sites to create a proper connection:



Implementing connection in java: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-the-jdbc-driver?view=sql-server-2017



Creating a login to the database: https://docs.microsoft.com/en-us/sql/relational-databases/security/authentication-access/create-a-login?view=sql-server-2017



Enabling named pipes and TCP/IP connections: https://www.blackbaud.com/files/support/infinityinstaller/content/installermaster/tkenablenamedpipesandtcpipconnections.htm



But still- there is thrown an SQLException:



com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4, port 1433 has failed. Error: "LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".



Code of my tester class:



package main;
import java.sql.*;

public class Main {

public static void main(String args) {

try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

String connectionUrl = "jdbc:sqlserver://LAPTOP-EG6DUP3O//LOCALDB#67B4CDC4:1433;" +
"databaseName=TestBazyDanych;user=doszke;password=doszke123;";

Connection con = null;

try {
con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
e.printStackTrace();
}

if(con != null){
System.out.println("connected");
} else {
System.out.println("unable to connect");
}

}


}


I got one problem during TCP/IP connections setting. I cannot restart my server from SSMS and I did it via SQL Server Configuration Manager:
SSMS- server cannot be restarted



Server restarted from SSCM







java sql-server jdbc tcp connection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 23 hours ago









Embid123Embid123

2077




2077








  • 1





    The Microsoft SQL Server JDBC driver cannot connect to a LocalDB, only to a real server.

    – Mark Rotteveel
    23 hours ago











  • Hmm, what could you recommend to use to connect to my local server?

    – Embid123
    23 hours ago






  • 1





    You need to use the real server. I'd start with trying jdbc:sqlserver://localhost;databaseName=TestBazyDanych, then try with the port configured in the SQL Server setting of that host (jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych) or start the SQL Server Browser service and use its instance name (jdbc:sqlserver://localhost<instanceName>;databaseName=TestBazyDanych (note the use of backslash instead of forward slash). But first of all, check your TCP/IP configuration. Default installations use a random port (if TCP/IP is enabled at all), not port 1433.

    – Mark Rotteveel
    23 hours ago













  • The second one worked for me fine, only an exception is thrown: login to database failed. But I think I can manage it. Thank you.

    – Embid123
    23 hours ago














  • 1





    The Microsoft SQL Server JDBC driver cannot connect to a LocalDB, only to a real server.

    – Mark Rotteveel
    23 hours ago











  • Hmm, what could you recommend to use to connect to my local server?

    – Embid123
    23 hours ago






  • 1





    You need to use the real server. I'd start with trying jdbc:sqlserver://localhost;databaseName=TestBazyDanych, then try with the port configured in the SQL Server setting of that host (jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych) or start the SQL Server Browser service and use its instance name (jdbc:sqlserver://localhost<instanceName>;databaseName=TestBazyDanych (note the use of backslash instead of forward slash). But first of all, check your TCP/IP configuration. Default installations use a random port (if TCP/IP is enabled at all), not port 1433.

    – Mark Rotteveel
    23 hours ago













  • The second one worked for me fine, only an exception is thrown: login to database failed. But I think I can manage it. Thank you.

    – Embid123
    23 hours ago








1




1





The Microsoft SQL Server JDBC driver cannot connect to a LocalDB, only to a real server.

– Mark Rotteveel
23 hours ago





The Microsoft SQL Server JDBC driver cannot connect to a LocalDB, only to a real server.

– Mark Rotteveel
23 hours ago













Hmm, what could you recommend to use to connect to my local server?

– Embid123
23 hours ago





Hmm, what could you recommend to use to connect to my local server?

– Embid123
23 hours ago




1




1





You need to use the real server. I'd start with trying jdbc:sqlserver://localhost;databaseName=TestBazyDanych, then try with the port configured in the SQL Server setting of that host (jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych) or start the SQL Server Browser service and use its instance name (jdbc:sqlserver://localhost<instanceName>;databaseName=TestBazyDanych (note the use of backslash instead of forward slash). But first of all, check your TCP/IP configuration. Default installations use a random port (if TCP/IP is enabled at all), not port 1433.

– Mark Rotteveel
23 hours ago







You need to use the real server. I'd start with trying jdbc:sqlserver://localhost;databaseName=TestBazyDanych, then try with the port configured in the SQL Server setting of that host (jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych) or start the SQL Server Browser service and use its instance name (jdbc:sqlserver://localhost<instanceName>;databaseName=TestBazyDanych (note the use of backslash instead of forward slash). But first of all, check your TCP/IP configuration. Default installations use a random port (if TCP/IP is enabled at all), not port 1433.

– Mark Rotteveel
23 hours ago















The second one worked for me fine, only an exception is thrown: login to database failed. But I think I can manage it. Thank you.

– Embid123
23 hours ago





The second one worked for me fine, only an exception is thrown: login to database failed. But I think I can manage it. Thank you.

– Embid123
23 hours ago












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250906%2fcreate-connection-to-sql-database-using-jdbc-driver-sqlexception%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250906%2fcreate-connection-to-sql-database-using-jdbc-driver-sqlexception%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre