Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my SQL connection get stuck when I use DriverManager.getConnection?

Tags:

java

mysql

jdbc

I have Java code that looks as follows and a MySQL database running on my machine.

System.out.println("Loading JDBC");     
Class.forName("com.mysql.jdbc.Driver").newInstance();           
System.out.println("JDBC loaded");      
System.out.println("Establishing connection");
myConnection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:1186/dbname","username","password");
System.out.println("Connection established");

However, the output I get is the following:

Loading JDBC
JDBC loaded
Establishing connection

and then nothing? it doesn't report an error connecting, so it must be connected. What's going on? why isn't it moving to the next line of code?

I'd appreciate any help

like image 629
user1251858 Avatar asked Mar 13 '26 23:03

user1251858


1 Answers

Few points:

  1. What is the name of database? If you are trying to run the code shown above, than database name must be "dbname", username must be "username" and password must be "password".

  2. Is your MySQL listening to port 1186? I am just wondering as generally at the time of installation default port is set to 3306.

If you want to follow steps involved in connecting MySQL with JDBC, you can take a look to this tutorial for JDBC Steps.

Jwalant

like image 137
Jwalant Avatar answered Mar 16 '26 12:03

Jwalant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!