Java MySQL Query Problem MySQLSyntaxErrorException When Creating a Table
- by Aqib Mushtaq
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.