简体   繁体   中英

Create a mysql database using JDBC

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.

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

应该是(请注意额外的空格):

"CREATE DATABASE "+dbName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM