2013-03-27 42 views
0

我有一个SQL查询,我从MYSQL复制,我删除了引号,但它似乎并没有工作。SQL语法错误,但从MYSQL复制

conn = connect(); 
      selectStatement = "UPDATE student SET Item ? = ?, Type ? = ? WHERE ID = ?"; 
      System.out.println(selectStatement); 
      if(conn != null) 
      { 
       preparedStatement = conn.prepareStatement(selectStatement);  
       preparedStatement.setString(2, id); 
       preparedStatement.setInt(1, location); 
       preparedStatement.setInt(3, location); 
       preparedStatement.setString(4, type); 
       preparedStatement.setString(5, userId); 
       preparedStatement.executeUpdate();   
       return true; 

我不知道为什么它不作为引发的异常工作,这是

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以在'1 ='sad'附近使用正确的语法,类型1 ='asd'WHERE ID ='student1''在第1行

+0

SQL语句应该是:selectStatement =“UPDATE student SET Item =?,Type =?WHERE ID =?”; – 2013-03-27 04:39:54

+0

你可以告诉学生表中的列名是什么? – 2013-03-27 04:41:02

回答

1

最后这个工作。

  conn = connect(); 
      selectStatement = "UPDATE student SET `Item " + location + "` = ? , `Type " + location + "` = ? WHERE ID = ?"; 
      System.out.println(selectStatement); 
      if(conn != null) 
      { 
       preparedStatement = conn.prepareStatement(selectStatement);  
       preparedStatement.setString(1, id); 
       preparedStatement.setString(2, type); 
       preparedStatement.setString(3, userId); 
       preparedStatement.executeUpdate();   
       return true; 
      } 
+0

大声笑没关系。这太奇怪了。一般来说,无论如何在列名中都有空格是一个坏主意。 – 2013-03-27 05:01:29

+0

请告诉我,'location'和'type'永远不会被用户输入。我可以输入'= 1的位置,其中0;辍学生; --' – 2013-03-27 05:02:58