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;
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;