aspose file tools*
The moose likes JDBC and the fly likes checking valid database connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "checking valid database connection" Watch "checking valid database connection" New topic
Author

checking valid database connection

Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
How would I test out if a database location, name, username, password are valid for a database?
like making sure this is valid during a DB connect:
"jdbc:mysql://"+DBLocation+"/"+DBName+"?user="+username+"&password="+password";
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
You might use DriverManager.getConnection(String url, String user, String password) and catch the possible SQLException if a database access error (like invalid password) occurs. I'm not sure if there is a cleaner way to check for invalid account information than to simply try to use the account information (and handle any problems).


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
I tested it out, and getConnection doesn't seem to return any errors with bad information. I just set up a boolean method to try to "show tables" and return false on any exceptions. Dirty, but it should work.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I just double checked that I do indeed get a connection failure (in the form of an SQLException) when I try to connect to a MySQL database with invalid user information. The error message reads:
General error: Access denied for user: 'somebody@somewhere' to database 'someDatabase'
I'm not sure why your experience would differ.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: checking valid database connection
 
Similar Threads
How to passed a login page with java code
How to passed a login page with java code
Weblogic 7.0 configure problem
SQL Server 2008r2 Connection Failed
ask for login