I am new in JDBC and I wanted to find out if there is a way to check if a particular database already exists in MySQL.
Let\'s say I wanted to create a database named
In newer versions of MySQL (5 and above) run this query:
SELECT COUNT(*)
FROM information_schema.tables
WHERE table_schema = '[database name]'
AND table_name = '[table name]';
If the result is 1 it exists.
In Java JDBC that would look something like this:
// Create connection and statement
String query = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema'[database name]' AND table_name = '[table name]'";
ResultSet rs = stmt.executeQuery(query);
rs.next();
boolean exists = rs.getInt("COUNT(*)") > 0;
// Close connection, statement, and result set.
return exists;