thijmen321
thijmen321

Reputation: 475

Java - MySQL - Create connection error

I don't know why but this line gives me an error. I'm first making a few private strings, host, port, database, user and password. Then I'm setting those strings in the constructor.

Connection conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + user + "&password=" + password);

This is the error message that I get:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.Number FormatException: For input string: "null"'.

What did I do wrong here?

Upvotes: 3

Views: 1053

Answers (3)

Azar
Azar

Reputation: 1106

My guess, without seeing more code, is that port is null.

When you pass the string to the getConnection() method, it tries to parse out the port as a number. The when you concatenate a null value to a String, it appends 'null'. Now your string looks like jdbc:mysql://examplehost:null. Thus the java.lang.Number FormatException: For input string: "null".

Upvotes: 1

Vimal Bera
Vimal Bera

Reputation: 10497

Its preety much clear from the Exception. Its because your port is null and throws exception while tried to parse into Number.

Upvotes: 0

Eran
Eran

Reputation: 393831

The most likely cause is that one of your parameters is null. Probably port, since an attempt is made to convert it to a number.

Unless you are using a non default port (the default being 3306), you can just omit the port :

Connection conn = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database + "?user=" + user + "&password=" + password);

Upvotes: 3

Related Questions