Re: Aletrnative to ParseURL in Connector/J version
Posted by: Filipe Silva
Date: December 08, 2023 02:40PM

Hi Mustajib,

Well, `ConnectionUrlParser.isConnectionStringSupported(String)` just checks the scheme section of the URL. As long as it starts with one of the schemes supported by Connector/J - "jdbc:mysql", "jdbc:mysql:loadbalance:", "mysqlx:" ... - it returns true. This is compliant to the JDBC specification Driver.acceptsURL(String), which makes no assumption with regard to the validity or the rest of the URL.

If this ends up being what you want, instead of calling `isConnectionStringSupported()` directly you could go through the standard API:

new com.mysql.cj.jdbc.Driver().acceptsURL("jdbc:mysql:/invalid-url::3006");

This is equivalent and, I'd say, safer to use that direct calls to the `ConnectionUrlParser` class.


Regarding "jdbc:mysql:/invalid-url::3006" being valid or not, well, this is debatable. You see, Connector/J is very tolerate with regard to what can be considered a valid or invalid URL. In this particular case, if you pass it to the
`ConnectionUrl.getConnectionUrlInstance(String, Properties)` you'll see that it will be able to parse it. It might not be what you expect, but still syntactically correct. This is so because our parser allows omitting most of the sections of the url replacing them by defaults. In this case the host information section is omitted, so the single slash character there is in reality the one preceding the database name, so "invalid-url::3006" becomes the database name, while the host becomes the default "localhost:3306". See how works:

System.out.println(ConnectionUrl.getConnectionUrlInstance("jdbc:mysql:/invalid-url::3006", null));

-- output:
com.mysql.cj.conf.url.SingleConnectionUrl@7cef4e59 :: {type: "SINGLE_CONNECTION", hosts: [com.mysql.cj.conf.HostInfo@3419866c :: {host: "localhost", port: 3306, hostProperties: {dbname=invalid-url::3006}}], database: "invalid-url::3006", properties: {}, propertiesTransformer: null}

So, yeah, it is expected and you will only know a URL/Connection String really works if you try to create a Connection instance from it.

Options: ReplyQuote




Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.