[fix] Java JDBC ConnectException: Connection refused

Post Banner
Caused by: java.net.ConnectException: Connection refused (Connection refused)
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
 at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
 at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
 at java.net.Socket.connect(Socket.java:607)
 at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153)
 at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
 ... 9 more

If your Application is not able to connect to the Database you will get ConnectException: Connection refused while working with Java JDBC, below are a few reasons that can cause this Exception,

Some Solutions/Fix

  1. The Database is down? Check connection to the database via DB Client or Console. Try doing telnet.
  2. Make sure you have defined the right database Hostname/IP Address in your JDBC DriverManager Connection.
  3. Make sure the Port is correct.
  4. Make sure your firewall settings are not restricting access.
  5. Make sure the Database server if having an IP Whitelisting policy, your IP is listed.
  6. If the Database Server is on Cloud (AWS/Azure/GCS), the right policies are set.
  7. Check that you have Internet connectivity.

Have Questions? Post them here!
Try Out Code2care Dev Tools:


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.