Why does my SQL connection get stuck when I use Dr

2019-07-05 05:45发布

问题:

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

回答1:

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



回答2:

I usually do not put in a port number after the IP address. I use SQLYog which is an user interface for mysql. SQLYog is much easier to use than the MySql command line.



标签: java mysql jdbc