title: CREATE DATABASE query using java jdbc and prepared statement returns syntax error
body: <p>I am trying to get Java to create a database using JDBC but I get a syntax error, despite the query being correct. If I write the name of a database into the code explicitly, for example, it works fine. Here's my code:</p>
  
  <pre><code>package mysql_manipulator;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  
  public class Create {
      private static final String 
              driver = "com.mysql.jdbc.Driver",
              url = "jdbc:mysql://localhost",
              username = "dylan",
              password = "******";
      private String databaseName;
  
      public Create (){
          this.databaseName = null;
      }
  
      public String getDatabaseName() {
          return databaseName;
      }
  
      public void setDatabaseName(String databaseName) {
          this.databaseName = databaseName;
      }
  
      public void createDatabase(String databaseName){
          try {
              this.setDatabaseName(databaseName);        
              Class.forName(driver);
              Connection con = DriverManager.getConnection(url,username, 
                  password);
              String query = "CREATE DATABASE ?";
              PreparedStatement preparedStmt = con.prepareStatement(query);
              preparedStmt.setString (1,this.getDatabaseName());
              preparedStmt.execute();
              preparedStmt.close();
              con.close();        
          }
          catch (Exception anException){
              System.out.println("Error: " + anException);
          }
      }
  }  
  </code></pre>
  
  <p>My Main Class  </p>
  
  <pre><code>package mysql_manipulator;
  
  public class Mysql_manipulator {
      public static void main(String[] args) {
          Create myObj = new Create();
          myObj.createDatabase("whatevs");        
      }    
  }
  </code></pre>
  
  <p>The error </p>
  
  <pre><code>Error: 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 ''whatevs'' at line 1
  </code></pre>
  
  <p>Huge thanks to anyone who knows why this isn't working - I've searched all over the web to no avail.</p>