Reputation: 15837
In my application I'm using MySQL as the RDMS and I'm trying to connect to the database with the usual classes under java.sql
.
I've a constructor in this class that is responsible for connecting or creating the database which accepts a parameter to specify the database path.
I would like to do some checks on the string passed to this constructor to check if it's a valid URL path. Currently, I'm using the following code, but I'm not sure if there's something that I'm forgetting or if it can be improved, maybe my simply checking using a regular expression.
private boolean isValidDBName(String dbName) {
return dbName != null && !dbName.isEmpty() && dbName.startsWith("jdbc:mysql://") && dbName.endsWith("/");
}
Upvotes: 3
Views: 2702
Reputation: 319
In my opinion using regular expression is better way. There is a good topic about URLs and regular expressions. What is the best regular expression to check if a string is a valid URL?
-- Edit
You can use something like this:
^jdbc:mysql:\/\/ [your pattern] \/$
Upvotes: 2