Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (120 points)

While am trying to connect to the mySQL database,getting the exception and am able to load the driver.This is the piece of code i wrote:

Connection con = null;

Class.forName("com.mysql.jdbc.Driver");

System.out.println("Driver Loaded");

try{ con =DriverManager.getConnection("jdbc:mysql://localhost:3306/DBName","username","password");

System.out.println("Connected to mySQL DB");

}catch(SQLException e){

e.printStackTrace();

And i am getting the following exception-------------

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException

MESSAGE: java.net.ConnectException: Connection refused: connect

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused: connect

at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:151)

at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:280)

at com.mysql.jdbc.Connection.createNewIO(Connection.java:1765)

at com.mysql.jdbc.Connection.<init>(Connection.java:430)

at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:268)

at java.sql.DriverManager.getConnection(DriverManager.java:664)

at java.sql.DriverManager.getConnection(DriverManager.java:247)

at com.databaseconnection.ConnectToDBMySQL.main(ConnectToDBMySQL.java:16)

** END NESTED EXCEPTION **

at com.mysql.jdbc.Connection.createNewIO(Connection.java:1830)

at com.mysql.jdbc.Connection.<init>(Connection.java:430)

at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:268)

at java.sql.DriverManager.getConnection(DriverManager.java:664)

at java.sql.DriverManager.getConnection(DriverManager.java:247)

at com.databaseconnection.ConnectToDBMySQL.main(ConnectToDBMySQL.java:16)

Please log in to answer this question.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Dec 25, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
asked Dec 18, 2020 in SQL by Appu (6.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...