Create connection to SQL database using JDBC Driver- SQLException
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:
java sql-server jdbc tcp connection
add a comment |
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:
java sql-server jdbc tcp connection
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 tryingjdbc: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
add a comment |
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:
java sql-server jdbc tcp connection
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:
java sql-server jdbc tcp connection
java sql-server jdbc tcp connection
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 tryingjdbc: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
add a comment |
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 tryingjdbc: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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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