0

I have created a simple database application with Java and MySQL. I would like for the use to specify what they want to call the database and then have it created. I get an SQL syntax error when I run the following code:

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { 
    try{
        Class.forName("com.mysql.jdbc.Driver");
        connect.con = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=pass"); 
        connect.st = connect.con.createStatement();

        String dbName = jTextField19.getText().trim();
        System.out.println(dbName);

        String sql = "CREATE DATABASE'"+dbName+"'";
        int rs = connect.st.executeUpdate(sql);
        System.out.println("Database Created");

    }catch(Exception e){

    }
}

What am I doing wrong? An explanation is much appreciated.

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
BEE
  • 107
  • 1
  • 2
  • 13

1 Answers1

0
"CREATE DATABASE'"+dbName+"'";

should be (note the additional space):

"CREATE DATABASE "+dbName;
StuPointerException
  • 7,117
  • 5
  • 29
  • 54
  • When I run it with a space where you have indicated the output is as follows: test Error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''test'' at line 1 Do I need to get rid of the commas surrounding test? – BEE Oct 30 '14 at 12:53