> Hey All,
>
> I'm having a little problem here. I have a project that I'm working on
> that involves a MySQL server database backend. I'm having no problem
> creating the database on the fly if it doesn't already exist and using it
> once it's created.
OK, I told you that I was quite possibly being an idiot. I found my own
solution...
I've revamped my code to trying to use the database with the SQL 'USE'
statement and catching the SQLException error in the event the database
does not exist. In my 'catch ( SQLException sqlE )' block, I've called
the routine to create the database. I've tested it a few times and, lo
and behold, success! This works just fine.
Now I have no need to worry about trying to create a file in the JAR at
runtime. Sorry to have wasted your time with this stupid question.
Cheers,
Sean
Lew - 04 Mar 2007 20:40 GMT
> I've revamped my code to trying to use the database with the SQL 'USE'
> statement and catching the SQLException error in the event the database
[quoted text clipped - 4 lines]
> Now I have no need to worry about trying to create a file in the JAR at
> runtime. Sorry to have wasted your time with this stupid question.
The solution you found is correct because it checks directly for the condition
of interest, instead of looking for some unreliable side-effect of that condition.
You cannot modify the contents of the running JAR at runtime. When you got the
current "directory" of the program that ran from the JAR, you didn't get a
path to a real directory. I don't even know what meaning one would expect for
"." in the context of a program run from a JAR. A JAR is not a file system.
Instead you wrote your code to determine whether something exists in the
database by asking the database. That just makes sense.
-- Lew
Lew - 04 Mar 2007 20:47 GMT
> I've revamped my code to trying to use the database with the SQL 'USE'
> statement and catching the SQLException error in the event the database
> does not exist. In my 'catch ( SQLException sqlE )' block, I've called
> the routine to create the database. I've tested it a few times and, lo
> and behold, success! This works just fine.
You are using an Exception. Is the absence of the database an exceptional
condition?
If "database absent" is an Exception, the expectation is that the database
will be there. If the 'USE' statement is necessary to your logic, then it
makes sense to have it there. If not, then the SQLException will be thrown by
whatever the first statement is that tries to hit the absent resource. There
is no single point of failure; every statement that tries to use the resource
has to check for exceptions. That is true for all database calls anyway, so it
doesn't represent additional overhead.
It is also possible to model "database absent" as a normal condition, in which
case you should not throw an Exception or depend on catching one, but directly
read the catalog to determine if the database exists. Instead of a
SQLException, you'd receive an empty result set or zero count or something
like that.
Choose the idiom that most closely matches the conceptual model of your
business logic.
-- Lew