I am trying to connect to a SQL database on a machine on my network. From what I can tell the TCP/IP settings and Firewall settings are all correct to be able to connect, but I am having no luck with my connection string.
Code:
static String dbUser = "user";
static String dbPass = "pass";
static String dbURL = "jdbc:sqlserver://SERVER1/BKUPEXEC;Instance=SQL2005;databaseName=images;";
static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
try {
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(dbURL, dbUser, dbPass);
System.out.println("Connected with host:port/database.");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
Error:
SEVERE: null
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host TMTSERVER1, port 1433 has failed. Error: "connect timed out. 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.".
try
"jdbc:sqlserver://SERVER1/BKUPEXEC;instanceName=SQL2005;databaseName=images;"
^^^^^^^^^^^^
I ended up changing the connection string to use the server's IP address instead of it's domain name. Also turns out that the SQL server was not using the default port number.
This is the new working connection string:
static String dbURL = "jdbc:sqlserver://192.168.1.240:1053;databaseName=SI_images;";
Related
I have class for connecting to DB from my JavaFx application
public class DBConnection {
public static Connection mcConnection() {
Connection connection = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost; databaseName=McQueen_db;";
connection = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
When i tried to connect i have an exception
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host port 1433 has failed. Error: "null. 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 check my firewall and its ok. I think that port too. Have any thoughts whats the problem?
try to change "localhost" to "127.0.0.1"
As sometimes, your system cannot mapping localhost correctly.
refer to official doc
I am trying to connect to my database by JDBC on localhost. Connecting via windows authentication is no problem, but I want to connect via SQL authentication. Therefore, I created a login and a user corresponding to this login in my database. I can normally log in SSMS:
My connection string for JDBC:
jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych;user=doszke;password=doszke123
Thrown exception:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'doszke'. ClientConnectionId:b7005fe3-904d-40c5-a89e-af0cb61250d6
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:254)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:258)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:104)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:4772)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3581)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:81)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3541)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7240)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2869)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2395)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2042)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1889)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1120)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:700)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
at main.Main.main(Main.java:38)
The username and password are the same, as those used for loging to SSMS.
Here my class code:
package main;
import java.sql.*;
public class Main {
private static ResultSet selectStan(Connection connection) throws SQLException {
String sql_stmt = "SELECT * FROM STAN;";
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql_stmt);
System.out.println("Select executed");
return result;
}
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String userName = "doszke";
String password = "doszke123";
String url = "jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych;user=doszke;password=doszke123";
try (Connection con = DriverManager.getConnection(url)) {
if(con != null){
System.out.println("connected");
} else {
System.out.println("unable to connect");
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
As Mark Rotteveel pointed out, I was trying to connect to a LocalDB instance with JDBC, which seemed undoable. (ref: here)
However, I installed jTDS and added to my classpath, changed my connection string to
jdbc:jtds:sqlserver://./TestBazyDanych;instance=LOCALDB#EB7165FD;namedPipe=true
create a connection by the use of this connection string, username and password and it worked. The instance pipe number was taken from cmd line via
sqllocaldb i MSSQLLocalDB
There are few things need to check:
Did you create doszke user under the database and SSMS?
Are you able to login with doszke/doszke123 credentials in SSMS?
Please check 1433 port are open or not in your inbound and outbound firewall.
Trying to telnet on localhost 1433. If it's getting failed change below setting:
Go to Configuration tools -> SQL Server Configuration Manager Select SQL Server Network Configuration -> Select protocol in the right side window enable tcp/ip and restart the services in services.
I want to connect to a database using Java. I use this code:
public static Connection getConnection() throws Exception {
try {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:sqlserver://localhost;integratedSecurity=true;";
String username = "root";
String password = "mysql";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("Connected");
return conn;
} catch(Exception e) {
System.out.println(e);
}
return null;
}
But I don't know the correct default url and driver property values. I use MySQLWorkBench.
the Driver you are using is correct but you will need to download include mysql connector jar in your build path.
For the url the mysql is by default running on port 3306 so your url will be like this, replace [yourdatabasename] with the name of your database, useSSL is set since the connection is not https
jdbc:mysql://localhost:3306/[yourDatabasename]?useSSL=false
Your driver is Class.forName("com.mysql.jdbc.Driver"); and your url will be Connection con=DriverManager.getConnection("jdbc:mysql://{hostname}:3306/{schemaname}","{username}","password");
and you can download the mysql jdbc driver from here
Hoping you would find this useful. Also remember 3306 is the default port and your MySQL instance should be running on this port. All the best :-)
My application runs on Linux Mandriva and can't get a connection to the MySQL database.
Part of my.cnf:
bind-address="127.0.0.1"
# skip- networking
I setted wait_timeout as follows:
SET GLOBAL wait_timeout = 28800;
I trying to get a connection to the database:
public class TestJdbc {
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String driver = "com.mysql.jdbc.Driver";
String dbName = "gibrid", userName = "java",
password = "java";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName, userName, password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
I packed this class in jar and send to the server. When I try to execute it, I get the following:
com.mysql.jdbc.CommunicationsException:
Communications link failure due to underlying exception:
** BEGIN MESSAGE **
java.net.ConnectException
MESSAGE: Connection timed out
STACKTRACE: java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:333)
...
But when I run this code from my localhost, everything is OK:
Connected to the database
Disconnected from database
What could be the problem?
Can you telnet to port 3306 on that Linux server ? That will tell you if something is listening on that port or not.
Note that if you run your code from your Windows server, the following line:
jdbc:mysql://localhost:3306/";
means you're connecting to a service on your Windows machine, not your Linux machine.
I am writing a java code to connect with MS SQL Server 2005. MS SQL Server is on Remote server windows server 2003. I am trying the following code but i am unable to establish a connection:
import java.*;
public class Connect {
private java.sql.Connection con = null;
private final String url = "jdbc:sqlserver://";
private final String serverName="xxx.xxx.xxx.xxx";
private final String portNumber = "1433";
private final String databaseName="myDb";
private final String userName ="user1";
private final String password = "xxxx";
private final String selectMethod = "cursor";
// Constructor
public Connect() {}
private String getConnectionUrl() {
return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
}
private java.sql.Connection getConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
if(con!=null) System.out.println("Connection Successful!");
} catch(Exception e) {
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
/*
Display the driver properties, database details
*/
public void displayDbProperties() {
System.out.println("Perform Operations ");
}
private void closeConnection() {
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Connect myDbTest = new Connect();
// myDbTest.displayDbProperties();
}
}
But I am getting following exceptions:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
Error Trace in getConnection() : The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
Error: No active Connection
I am not getting where is the problem in the above code or do i need to do some setting
to connect to remote server.
Please give me your valuable suggestion which can help me to overcome with this problem.
Make sure that your SQL Server is configured to use TCP/IP. Enable it from SQL Server's Network Utility app. Also check there that the SQL Server is using port 1433 (IP Addresses - IPAll - TCP Port).
Try to use "telnet <server_host> 1433". If it doesn't connect you will not be able to establish a connection.
IMHO "Connection refused" means your database server is not visible from your application server.
Check IP address and port.
Check database connectivity directly from your database server (to avoid firewalls).
Check database connectivity from your application server.
Hope this will help you
ALLOW THE CONNECTION FIRST IN WHICH PC YOUR SQL SERVER IS RUNNING...
GO TO CONTROL PANEL-->ADMIN. TOOLS--->Windows Firewall with Advanced Security-->Inbounded rules-->new rule-->select port radio button -->next-->enter port 3306-->click next -->finally give the rule name like conn any...click finish