2011-01-10 19 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

相關問題