I want to connect to the database mysql .
I am using Type IV driver Connector/J.
How to connect to the database.

When i load the driver class its working after that i get this error


SQL Exception :Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.ConnectException
MESSAGE: Connection refused: connect

STACKTRACE:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:364)
at java.net.Socket.connect(Socket.java:507)
at java.net.Socket.connect(Socket.java:457)
at java.net.Socket.<init>(Socket.java:365)
at java.net.Socket.<init>(Socket.java:207)
at com.mysql.jdbc.StandardSocketFactory.connect(
va:173)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:268)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2745)
at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:285)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at myjdbc1.main(myjdbc1.java:20)


** END NESTED EXCEPTION **

Last packet sent to the server was 15 ms ago. 08S01 0

Dani AI

Generated

As reported a ConnectException ("Connection refused") and pointed toward basic checks, here is a compact, actionable checklist with exact commands and a minimal Java example to eliminate the usual causes and get a working JDBC connection.

First, verify the server is listening and reachable from the client. On Linux run:

sudo ss -tlnp | grep 3306
# or
netstat -tlnp | grep 3306

On Windows:

netstat -ano | findstr 3306

From the client try a raw TCP check:

telnet DB_HOST 3306
# or (if available)
nc -vz DB_HOST 3306

If these fail, check mysqld configuration (my.cnf / my.ini) for bind-address and skip-networking, and ensure the server was restarted after changes.

Use the mysql client to confirm MySQL itself accepts connections:

mysql -h DB_HOST -P 3306 -u myuser -p
# or
mysqladmin ping -h DB_HOST -P 3306 -u myuser -p

A minimal Java example (Connector/J 8+) to try locally:

Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(
  "jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&serverTimezone=UTC",
  "myuser", "mypassword");

If networking is fine but authentication fails, create/allow the DB user from the correct host:

GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword';
FLUSH PRIVILEGES;

Replace '%' with the specific client IP for better security. On cloud hosts also open the port in security groups/firewalls and confirm no NAT/port-forwarding issues.

If problems persist, collect server error logs and a tcpdump while reproducing the error. Connector/J reference: .

Recommended Answers

All 3 Replies

Is the database running? Is it running on port 3306? Is there a firewall active blocking the port? Are you attempting to connect to the right host? Are you attempting to connect to 3306? Are you using the correct tablename in the connection url? Is the username and password correct? Is MySQL configured to allow that user, with that password, to connect from where the user is attempting to connect from?

I think that pretty much covers the possibilites.

Thanks!Ur Qns help me to find solution

Very good. It has been awhile since a new poster here has been willing to work through his own problem. Good Job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.