Failing to connect to MySQL Server from Java application - java

I am attempting to establish a connection to my local sqlserver instance on android with this line
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:3306;DatabaseName=test", "admin", "password");
But I am getting this error
The TCP/IP connection to the host localhost, port 3306 has failed.
Error: "Connection refused. 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
I know that using jdbc to create direct database connections on android is not recommended for very good reasons, but that is not my concern right now.
I have verified in sql server configuration manager that tcp/ip is enabled for LOCAL and that IPAll TCP Port is set to 3306. I restarted the server after making these changes.
I have created a firewall rule allowing tcp/ip connections on port 3306 for local/domain connections inbound and outbound.
I checked the port with telnet telnet localhost 3306 and it connected.
Any help is appreciated, it is beginning to get frustrating.

Related

Play framework java connection is not available to sql server database

I'm stucked in a problem during connection from my java project (play framework ) to a sql server database in Azure.
In local enviroment the connection works fine.
Below parameter in my application.conf :
db.default {
url="jdbc:sqlserver://server-name.database.windows.net:1433;database=database-name"
encrypt=true
trustServerCertificate=false
hostNameInCertificate="*.database.windows.net"
loginTimeout=30
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
username="root"
password="RootPwd"
}
This is the error received:
ERROR o.h.e.jdbc.spi.SqlExceptionHelper HikariPool-1 - Connection is
not available, request timed out after 30006ms. ERROR
o.h.e.jdbc.spi.SqlExceptionHelper The TCP/IP connection to the host
par-sql-server.database.windows.net, port 1433 has failed. Error:
"par-sql-server.database.windows.net. 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.". ERROR
p.api.http.DefaultHttpErrorHandler
play.api.PlayException: Execution exception[[CompletionException:
org.hibernate.exception.JDBCConnectionException: Unable to acquire
JDBC Connection]]
at play.api.http.HttpErrorHandlerExceptions$.$anonfun$convertToPlayException$3(HttpErrorHandler.scala:388)
at scala.Option.getOrElse(Option.scala:201)
at play.api.http.HttpErrorHandlerExceptions$.convertToPlayException(HttpErrorHandler.scala:388)
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:373)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:264)
at play.core.server.AkkaHttp
I have already checked that tcp/ip port 1433 is correctly available for database, and my ip is correctly setted to avoid firewall.
Any suggestions?
thanks
Usually this error occurs when the database is not getting the connection within the default connectionTimeout property.
You can try to increase the value of connectionTimeout in Java config as below:
config.setConnectionTimeout(300000);
below is one of the sample config:
HikariConfig config = new HikariConfig();
config.setMaximumPoolSize(20);
config.setConnectionTimeout(300000);
config.setConnectionTimeout(120000);
config.setLeakDetectionThreshold(300000);
If possible let me know the steps to reproduce this issue so that I can fix it accordingly, also based on the information you've provided these blogs (Blog1, Blog2) will help you in establishing the connection

connect amazon mysql with JDBC through remote server

I had a problem here, is it possible that JDBC connecting to the amazon MySQL server remotely? I searching on the Internet for solution but ended up receiving the error
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
I am using jsch.jar to connect the remote server, then call up JDBC connector trying to connect MySQL DB, I had successfully connected to the remote server yet I cannot connect MySQL DB on amazon, here is my code
testPutty t = new testPutty();
t.connect("admin", "", "xx:xx:xx:xx", "openSSHPrivateKey", 22);
t.connect();
System.out.println("connected");
Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection("jdbc:mysql://staging.ppyhcsxnlkji.ap-southwest-1.rds.amazonaws.com:3306/staging","superuser","password");
System.out.println("hello world");
t.close();
First set up the tunnel with SSH. You haven't shown what testPutty is so I'll assume it's a thin Java wrapper around a command line invocation of Putty. The command line option for setting up a tunnel is
-L[localport]:[host]:[remoteport]
For your example this would be
-L3306:staging.ppyhcsxnlkji.ap-southwest-1.rds.amazonaws.com:3306
This sets up a tunnel that forwards connections to local port 3306 to port 3306 on the given host (staging...amazonaws.com) on port 3306, routed through the host that is the target of the SSH command (in your sample xx:xx:xx:xx).
Then, your connection string needs to use localhost instead of the actual remote:
jdbc:mysql://localhost:3306/staging
If 3306 on your local host is not available because you are also running a local copy of MySQL, just choose a different unused port.

Connecting android to remote sqlserver

I am trying to connect to an SQL Server remote database but I can't create new connection in Android although I can connect through a Java application with exact the same code. DriverManager.getConnection() throws an exception. Here's my code
public DatabaseManipulation()
{
URL=String.format(URL,localDatabaseName);
Connect();
}
public void Connect() {
try {
connection = DriverManager.getConnection(URL,
DATABASE_USERNAME, DATABASE_PASSWORD);
statement=connection.createStatement();
} catch(Exception e) {
Log.d("con",e.getMessage);
}
}
Exception:
"The TCP/IP connection to the host 192.168.100.23, port 1433 has
failed. Error: "/192.168.100.23:1433 - Network is unreachable. 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."."
When two or more SQL Servers are connected across network they do all communication using TCP/IP. The default port of SQL Server installation is 1433. This port can be changed through SQL Server Configuration Manager. TCP/IP should be enabled for SQL Server to be connected.
Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools >> SQL Server Configuration Manager >> Select TCP/IP

Error:The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect

I am using
Windows 7
Netbeans IDE 7.1.2
SQL Server Management Studio Express 2005
JDK1.6
I am getting the below error while connecting to the database:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
My connection string is:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;databaseName=dbcm;" +
"user=sa;password=sa");
I've checked below things:
In SQL Server Configuration Manager, Protocols for SQLEXPRESS : TCP/IP Enabled
In fact I made enable remaining 3 too.
SQL Server(SQLExpress) and SQL Browser Service both are running.
I followed the steps below to successfully connect to an SQLExpress instance using JDBC:
Configure TCP/IP communication with SQL Express
Open SQL Server Configuration Manager.
Go to SQL Server Network Configuration -> Protocols for SQLEXPRESS
Set the status of TCP/IP protocol to "Enabled" (if it is already not).
Open Properties window for TCP/IP, go to IP Addresses section.
Go to the bottom of this property page and set the TCP Port under IPAll to 1433.
Connect to the SQLExpress instance using Microsoft's JDBC driver for SQL Server
JDBC URL: jdbc:sqlserver://localhost;instance=SQLEXPRESS;databaseName=<your DB>;user=<your User>;password=<your Passwd>

Connection problem with sockets in java

i tried this code:
http://programming-guides.com/java/tcp-client-server-chat
(full example)
and it's working but when i switch the "host" int the client side to an external IP (insted of localhost) it give me this error:
"Cannot get IO for connection to.."
i treid to switch the port but it didnt work..
Is the server running on that remote machine? Is the port open in the firewall?
Try to netcat to the remote host. If you get an connection the example maybe wrong. If not, you cannot connect to that host: host not exists, not listen on that port, port is filtered, etc, etc.

Categories