sql - Java: Trouble connecting to MySQL -


I am writing a desktop Java application on which I want to connect to a MySQL database on a server. The code to do this is:

  import java.io.IOException; Import java.sql.Connection; Import java.sql.DriverManager; Import java.sql.SQLException; Personal stable connection throws getDBConnection () SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {String Username = "myUserName"; String password = "myPassWord"; String url = "jdbc: mysql: //www.domainName.com: 3306 / databasename"; Class.forName ("com.mysql.jdbc.Driver"); System.out.println ("Connecting to the database ..."); // hangs here Conne conn = DriverManager.getConnection (url, username, password); Return canon; }  

When I run it, it hangs on DriverManager.getConnection () call. Why does this happen Is my URL bad?

(I can not find an error message, but the program does not react like an unlimited loop. I have not been waiting more than 90 seconds to see that the connection will never be established .)

In addition, class .for name () what is the purpose of the call? how does it work?

I'm absolutely certain that the username and password are correct I (I just used the usernames and passWord as a placeholder .)

UPDATE : I have set the port number, and now I get this error:

Can not connect to the database: Java.sql.SQLException: Access denied to user 'usernameName'@'r236059121.resnet.mySchool.edu' (using password: yes)

Does this mean that I need to configure the settings on the database? Or does it mean that I have wrong credentials? (They work for PHP scripts deployed on the server, which includes the database.)

Solution : Host added on cPanel above host host list.

I think the line must be

  class. ForName ("com.mysql.jdbc .com");  

( .newInstance () turn off the bit)

By doing so the driver registers himself with the driver manager and driver manager Allows to choose a driver for the database url.

I think the hang is caused by a DNS problem, or your DB can not be reached for any other reason. By default, MySQL does not end time for the JDBC driver connection. Look and look for connectTimeout

In your code, you have

  string url = "jdbc: mysql: //www.domainName. Com: portNumber / databaseName ";  

I think you used an actual port there? By default, this should be 3306. You can test from the test database which is present in almost all mysql examples:

  string URL = "jdbc: mysql: //www.domainName.com: 3306 / test";  

You also wrote:

  string user name = "myUserName"; String password = "myPassWord";  

Obviously you should also use the actual certificate here. Ask your DBA what they are. If you are DBA ... well you should probably read on MySQl Administration :) Seriously use those people you were prompted for the password for root user (obviously when you install MySQL (clear)

You should not hang in the real code when DB is not there so I recommend adding a connecttime option:

  string URL = " Jdbc: mysql: //www.domainName.com: 3306 / test? ConnectTimeout = 3000 ";  

( connecttimeout is in milliseconds, so it will be after 3 seconds)


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -