Does this error occur randomly, or every time you run the code?
In the first case, it might be a networking glitch. In the second case, it might be a bug in the
JDBC driver (yes, there are occasionally some); I'd suggest to try using a different version of the driver (see our
Oracle FAQ).
I'd also advice against using this
Firstly, you're catching an
Exception, which is too broad. Your code can only throw
SQLException, and
you should only catch that. If you modify your code in the future in a way it could throw a different checked exceptions, your catch clause might start handling exceptions you haven't intended when you wrote the code.
Secondly, an exception should end the processing. You simply return
false when an exception occurs, but you cannot know the nature of the exception, and in most cases, when an
SQLException occurs, you can't tell anything about the state of the database connection which was used at that time. You should simply cancel the rest of your processing, probably by throwing out some unchecked exception (a
RuntimeException, for example). That exception should make it to some topmost level of your application, where it would be logged.
And finally, your method uses
throws SQLException declaration, although, as far as I can tell, it cannot produce one (you're handling it within the method). This forces you to handle the
SQLException in the caller. Simply drop the
throws SQLException from the declaration.