2016-03-10 57 views
-1

嗯,我有这个sql语法的一些问题。java JDBC MYSQL语法错误

public static void newTable(String tableName, String columnName, String columnType) { 

    try { 

     PreparedStatement pst = conn.prepareStatement 
       ("CREATE TABLE `" + tableName + "`" + "(`" +columnName + "` `" + columnType + "`)"); 

     //PreparedStatement pst = conn.prepareStatement("CREATE TABLE teszt2 (asd VARCHAR(255));"); //THIS IS WORKING 

     //pst.setString(1, tableName); 
     //pst.setString(2, columnName); 
     //pst.setString(3, columnType); 
     pst.execute(); 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

} 

这是我得到的错误: 我每次运行此我得到这个错误。我认为我的查询存在一些问题。 虽然,没有字符串的人工作正常。

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 '`VARCHAR(255)`)' at line 1 

回答

1

根据你的代码中的注释,这个工程:

CREATE TABLE teszt2 (asd VARCHAR(255)); 

但这并不:

CREATE TABLE `teszt2` (`asd` `VARCHAR(255)`); 

所以我想卸下后,蜱,并使用“工作“版本:

PreparedStatement pst = conn.prepareStatement 
      ("CREATE TABLE " + tableName + " (" +columnName + " " + columnType + ")"); 

或至少rem从类型说明符中删除它们:

PreparedStatement pst = conn.prepareStatement 
      ("CREATE TABLE `" + tableName + "`" + "(`" +columnName + "` " + columnType + ")"); 
+0

感谢您的快速回答。我不知道我怎么会这么愚蠢。 我从类型说明符中删除了反标记,它像魅力一样工作。 :) 再次感谢你。 – david20002062