Description:
When the database connection url start with the word "address",the JDBC Driver can't parse the "host:port" Correctly.
For example:
my db connection url is :addressXXX.xxx.com:3306,JDBC Driver will report:
Caused by: java.net.UnknownHostException: addressXXX.xxx.com:3306
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:883)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1236)
at java.net.InetAddress.getAllByName0(InetAddress.java:1187)
at java.net.InetAddress.getAllByName(InetAddress.java:1117)
at java.net.InetAddress.getAllByName(InetAddress.java:1053)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:190)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
How to repeat:
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class tmpjdbc {
public static void main(String[] args) throws SQLException, ClassNotFoundException, UnknownHostException, SocketException {
Class.forName("com.mysql.jdbc.Driver");
Properties pro = new Properties();
pro.setProperty("user", "XXX");
pro.setProperty("password", "XXX");
Driver driver = new com.mysql.jdbc.Driver();
Connection connection1 = driver.connect("jdbc:mysql://addressxxx.com:3306/?zeroDateTimeBehavior=convertToNull", pro);
Statement statement1 = connection1.createStatement();
ResultSet _result = statement1.executeQuery("select 1");
while (_result.next()) {
System.out.println(_result.getInt(1));
}
connection1.close();
}
}
Suggested fix:
change the method parseHostPortPair of com/mysql/jdbc/NonRegisteringDriver.java from
protected static String[] parseHostPortPair(String hostPortPair) throws SQLException {
String[] splitValues = new String[2];
if (StringUtils.startsWithIgnoreCaseAndWs(hostPortPair, "address")) {
splitValues[HOST_NAME_INDEX] = hostPortPair.trim();
splitValues[PORT_NUMBER_INDEX] = null;
return splitValues;
}
.....
to
protected static String[] parseHostPortPair(String hostPortPair) throws SQLException {
String[] splitValues = new String[2];
if (StringUtils.startsWithIgnoreCaseAndWs(hostPortPair, "address=")) {
splitValues[HOST_NAME_INDEX] = hostPortPair.trim();
splitValues[PORT_NUMBER_INDEX] = null;
return splitValues;
}
.....