2013-06-24 49 views
1

我的java/jdbc代码有问题。查询JDBC Java

参数索引超出范围(2号中的哪一个参数1)

代码:

   Connection c = null; 
       MySQL MySQL = new MySQL(Host, Port, Database, Username, Password); 
       c = MySQL.open(); 
       Player player = (Player) sender; 
       String zapytanie = "UPDATE `?` SET `tag`=? WHERE `name`='?';"; 
       PreparedStatement ps = c.prepareStatement(zapytanie); 
       ps.setString(1, Tabel); 
       ps.setString(2, red); 
       ps.setString(3, player()); 
       ps.executeUpdate(); //Executes the query 
       ps.close(); //Closes the query 
       c.close(); 
+2

难道这有什么做的'引号? – austin

回答

4
final String zapytanie = "UPDATE " + Table + " SET tag = ? WHERE name = ?"; 
  • 没有围绕
  • JDBC不需要所需?引号;任意 SQL语句
  • 占位?只能用于列的值
+0

+1,这就是它。我也会建议你的查询字符串'最终字符串zapytanie' – TecHunter

+0

你有你的sql语法错误;检查与您的mysql服务器版本相对应的手册,以在''tabelx'附近使用正确的语法'SET tag ='red'WHERE name ='Awek' – Awesomeness

+0

@Awesomeness表名必须连接在一起。 '?'仅适用于列值。 –