3

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?

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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".

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.