Java MySQL Query Problem MySQLSyntaxErrorException When Creating a Table

Posted by Aqib Mushtaq on Stack Overflow See other posts from Stack Overflow or by Aqib Mushtaq
Published on 2011-01-10T12:50:39Z Indexed on 2011/01/10 12:53 UTC
Read the original article Hit count: 279

Filed under:
|
|
|
|

I fairly new to MySQL with Java, but I have executed a few successful INSERT queries however cannot seem to get the CREATE TABLE query to execute without getting the 'MySQLSyntaxErrorException' exception. My code is as follows:

import java.sql.*;

Statement stmt;

Class.forName("com.mysql.jdbc.Driver");

String url = "jdbc:mysql://localhost:3306/mysql";

Connection con = DriverManager.getConnection(url, "root", "password");

stmt = con.createStatement();

String tblSQL = "CREATE TABLE IF NOT EXISTS \'dev\'.\'testTable\' (\n"
                + " \'id\' int(11) NOT NULL AUTO_INCREMENT,\n"
                + " \'date\' smallint(6) NOT NULL\n"
                + ") ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";

stmt.executeUpdate(tblSQL);

stmt.close();
con.close();

And the error is as follows:

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 ''dev'.'testTable' (
 'id' int(11) NOT NULL AUTO_INCREMENT,
 'date' smallint(6) N' at line 1

I would appreciate it if anyone could spot the mistake in this query, as I've tried executing this within phpMyAdmin and it works as it should.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about java

Related posts about mysql