Communications link failure due to underlying exception: Connection Time Out - java

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.

Related

Create connection to SQL database using JDBC Driver- SQLException [duplicate]

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.

JDBC MySQL - Java unable to connect to database server

I have a local MySQL running on the default port, but for some reason my code can't connect to it. restdb is the database name I'm trying to connect to. I can do queries from Workbench, so I'm pretty positive the database itself is up.
I have a maven project, and the mysql-connector (mysql-connector-java-8.0.11.jar) is in my Maven Dependencies. Is this the wrong place?
Here is my code. It fails at getConnection. In my error log it tells me:
Could not create connection to database server. Attempted reconnect 3 times. Giving up.
private Connection getConnection()
{
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/restdb?autoReconnect=true&useSSL=true";
String username = "root";
String password = "password";
try
{
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e)
{
System.err.println(e.getMessage());
}
return conn;
}
It looks like you don't have SSL setup. Unless this is intentional, remove the 'useSSL=true' part of the URL.

Java application is not connecting to Database after hosting on vps

I have created a Dynamic web application in java,jsp and connected to MySQL server using phpMyAdmin as localhost database server.
This is the code i have written in connection class:
static final String USER = "root";
static final String PASS = "";
Statement stmt = null;
static Connection conn = null;
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost/dbtest";
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
And, While hosting this application on VPS (through Plesk parallel panel for windows) i have changed the DB_URL to my server ip address:port and uploaded on server using .WAR file.
When i am trying to print conn object to check if the connection is establishing with database i got the object value and connection was successful and all database operation are working well.
The issue i faced after hosting .war file is, conn object is returning null and my application gives 500 internal server error.
Can anyone suggest where iam going wrong and how to connect to database while hosting it.
Thank you.

TCP/IP connection issue using jdbc.sqlserver

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;";

Java DataBase Connectivity Problem with MS SQL Server 2005 from a Remote Server

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

Categories