Java: problem connecting to MySQL

I am writing a java desktop application on the fact that I want to connect to the MySQL database on the server. Here is the code for this:

import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; private static Connection getDBConnection() throws 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 database..."); //hangs here Connection conn = DriverManager.getConnection(url, username, password); return conn; } 

When I run this, it freezes when calling DriverManager.getConnection() . Why is this happening? Is my URL incorrect?

(I do not receive any error messages, but the program does not respond as if in an infinite loop. I did not wait more than 90 seconds to find out if the connection will be established.)

Also, what is the purpose of calling Class.forName() ? How it works?

I am pretty much sure that the username and password are correct. (I used userName and passWord as placeholders above.)

UPDATE : I fixed the port number and now I get this error:

Unable to connect to database: java.sql.SQLException: access denied for user userName'@'r236059121.resnet.mySchool.edu '(using password: YES)

Does this mean that I need to configure the parameters in the database? Or does this mean that I have credentials incorrectly? (They work for PHP scripts deployed on a server that contains a database.)

SOLUTION Added the node above to the list of access hosts in cPanel.

+1
java sql mysql jdbc
source share
3 answers

I think the line should be

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

(close the .newInstance() bit)

This forces the driver to register with the driver manager and allows the driver manager to select the driver for the database URL.

I think the hang is caused by a DNS problem or some other reason why your db could not be reached. By default, the MySQL JDBC driver is not disabled to connect. See http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html and find connectTimeout .

In your code you have

  String url = "jdbc:mysql://www.domainName.com:portNumber/databaseName"; 

I suppose you used the real port there? By default, it should be 3306. You can test the test database, which is present in almost all mysql instances:

  String url = "jdbc:mysql://www.domainName.com:3306/test"; 

You also wrote:

  String username = "myUserName"; String password = "myPassWord"; 

Obviously, here you must use real credentials. Ask your dba who they are. If you are a DBA, then ... well, you should probably read in MySQl administration :) Seriously, when you installed MySQL, you probably managed to get the password for the root user. Use them (obviously)

In real code, you probably shouldn't hang when db doesn't exist. Therefore, I recommend adding the connectTimeout parameter as follows:

  String url = "jdbc:mysql://www.domainName.com:3306/test?connectTimeout=3000"; 

( connectTimeout is in milliseconds, so this will happen in 3 seconds)

+2
source share

It seems to me that your database is unavailable, and you will probably receive an error message when the call goes into timeout. Are you sure the host name and port are correct and accessible from your computer?

You do not need newInstance() at the end of Class.forName() . Class.forName() starts the class loader to load this class, which, in turn, runs the internal registration code in the driver, which makes the driver available.

+4
source share

Rosarch - you cannot connect to your database from the moment it is unavailable. After a while, it will burn out. Try telnetting -

telnet <IP-OF-domainName.com> <PortNumber>

Basically you will see that it shows the wait time.

Solutions - 1.) If you are behind a firewall, you need to punch a hole to allow access 2.) If you are behind a proxy server, you need to configure it for access

0
source share

All Articles