0

so I wanted to do a simple SQL query to my MariaDB server to remove the database (I know it's probably not the best way to do it). This is the piece of code to do that:

public static void cleanDatabase() {
    try {
      Connection con = DriverManager.getConnection("myurl", "username", "password"); // replaced by real values
      Statement stmt = con.createStatement();
      stmt.executeUpdate("DROP DATABASE statuer_test; CREATE SCHEMA IF NOT EXISTS statuer_test DEFAULT CHARACTER SET utf8;");
      con.close();
    } catch (Exception e)  {
      e.printStackTrace();
      System.err.println("cleanDatabse(): error");
    }
  }

This is what I get when I call the cleanDatabase() method :

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CREATE SCHEMA IF NOT EXISTS statuer_test DEFAULT CHARACTER SET utf8' at line 1
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
(more lines...)

However this is totally working when I use a mysql cli client:

MariaDB [(none)]> CREATE SCHEMA IF NOT EXISTS statuer_test DEFAULT CHARACTER SET utf8;
Query OK, 0 rows affected, 1 warning (0.000 sec)

So what I'm missing here ?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
lefuturiste
  • 87
  • 1
  • 7

1 Answers1

1

Seem to work with that code, so resolved:

stmt.executeUpdate("DROP DATABASE statuer_test");
stmt.executeUpdate("CREATE SCHEMA IF NOT EXISTS statuer_test DEFAULT CHARACTER SET utf8");
lefuturiste
  • 87
  • 1
  • 7