2

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("/");
}
nbro
  • 15,395
  • 32
  • 113
  • 196

1 Answers1

1

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] \/$
Community
  • 1
  • 1
mateo
  • 179
  • 13