2011-01-10 91 views
1

我对MySQL的Java相当陌生,但我已经执行了一些成功的INSERT查询,但似乎无法获得CREATE TABLE查询而无法执行MySQLSyntaxErrorException异常。我的代码如下:MySQLSyntaxErrorException在创建表时

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(); 

和错误如下:

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 

我会很感激,如果任何人都可以在此查询发现错误,因为我已经试过内phpMyAdmin的执行此它的工作原理应该如此。

回答

3

\n将使按回车效果:)使它像

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

还不行,可以在单一的逗号进行转义? – 2011-01-10 13:02:53

相关问题