简体   繁体   English

Java - MySQL - 创建连接错误

[英]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"'. com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:由于基础异常,无法加载连接类:'java.lang.Number FormatException:对于输入字符串:“null”'。

What did I do wrong here? 我在这做错了什么?

The most likely cause is that one of your parameters is null. 最可能的原因是您的一个参数为null。 Probably port , since an attempt is made to convert it to a number. 可能是port ,因为尝试将其转换为数字。

Unless you are using a non default port (the default being 3306), you can just omit the port : 除非您使用非默认端口(默认为3306),否则您可以省略端口:

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

My guess, without seeing more code, is that port is null . 我的猜测,没有看到更多的代码,是portnull

When you pass the string to the getConnection() method, it tries to parse out the port as a number. 将字符串传递给getConnection()方法时,它会尝试将端口解析为数字。 The when you concatenate a null value to a String , it appends 'null'. 将null值连接到String ,它会追加'null'。 Now your string looks like jdbc:mysql://examplehost:null . 现在你的字符串看起来像jdbc:mysql://examplehost:null Thus the java.lang.Number FormatException: For input string: "null" . 因此java.lang.Number FormatException: For input string: "null"

Its preety much clear from the Exception. 从Exception中可以清楚地看出它的清晰度。 Its because your port is null and throws exception while tried to parse into Number. 这是因为您的port为null并在尝试解析为Number时抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM