database not exist, how to tell

L

Liz

I have a java program that will create a MySQL
database, create a table, and then write data into the table.
My problem is how to tell if the databasename that
is given to me already exists or not, because if it
exists then I can just delete the table and create
a new one. Otherwise I have to create it too. Right
now I have this code that works, but is there a way
to tell existence without using the exception? "filename"
is passed to me as a parameter.
---
try {
DriverManager.registerDriver(new Driver());
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/" + filename);
} catch (SQLException e1) {
// probably database doesn't exist (what we want)
if ((e1.getMessage().indexOf("Unknown database") == -1)) {
// some other error occurred
System.out.println("SQLException: " + e1.getMessage());
System.out.println("SQLState: " + e1.getSQLState());
System.out.println("VendorError: " + e1.getErrorCode());
e1.printStackTrace();
return null;
}
System.out.println("Got what we wanted -- file does not exist");
// do something
return filename;
}
// file exists -- we have to get rid of it
// do something
// return filename;
 
L

Liz

Liz said:
I have a java program that will create a MySQL
database, create a table, and then write data into the table.
My problem is how to tell if the databasename that
is given to me already exists or not, because if it
exists then I can just delete the table and create
a new one. Otherwise I have to create it too. Right
now I have this code that works, but is there a way
to tell existence without using the exception? "filename"
is passed to me as a parameter.
---
try {
DriverManager.registerDriver(new Driver());
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/" + filename);
} catch (SQLException e1) {
// probably database doesn't exist (what we want)
if ((e1.getMessage().indexOf("Unknown database") == -1)) {
// some other error occurred
System.out.println("SQLException: " + e1.getMessage());
System.out.println("SQLState: " + e1.getSQLState());
System.out.println("VendorError: " + e1.getErrorCode());
e1.printStackTrace();
return null;
}
System.out.println("Got what we wanted -- file does not exist");
// do something
return filename;
}
// file exists -- we have to get rid of it
// do something
// return filename;
After having posed the above problem, I tried to move on to having
the application create a database, but I could not figure out how.
The tutorial sort of implies that it can't be done, but doesn't explicitly
say so. They start the tutorial assuming the database already exists.
So is it possible to create a database in java?
 
A

Andrew Harker

Liz wrote:

[snip]
After having posed the above problem, I tried to move on to having
the application create a database, but I could not figure out how.
The tutorial sort of implies that it can't be done, but doesn't explicitly
say so. They start the tutorial assuming the database already exists.
So is it possible to create a database in java?

Connect to mysql rather than a specific database. Its not generally
a good idea to allow DBA stuff and user stuff to be done by the same
user so you may want to separate this - if you just want temp
tables just use your current db. Also you will need to use the full
table name with this connection (or a 'use testdb'). Alternatively I
guess you could have various batch files that do this / mysqladmin
commands and either run then before/after your prog, use jni or the
runtime exec methods.

String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
....
String serverName = "localhost";
String mydatabase = "";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
....
connection = DriverManager.getConnection(url, username, password);
....
int result = stmt.executeUpdate("create database testdb");
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top