开发者

MySQLSyntaxErrorException when creating table

开发者 https://www.devze.com 2023-02-03 16:12 出处:网络
I am 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 excep

I am 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:

Statement stmt;
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开发者_Python百科 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.


\n will make press enter effect :) make it like

String tblSQL = "CREATE TABLE IF NOT EXISTS `dev`.`testTable`"
            + "("
            + "id INTEGER(11) NOT NULL AUTO_INCREMENT primary key,"
            + "date smallint(6) NOT NULL"
            + ")"
            + "ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
0

精彩评论

暂无评论...
验证码 换一张
取 消