MySQL PreparedStatement:参数索引超出范围

时间:2015-04-20 03:41:57

标签: java mysql sql

我在一小时内通过stackoverflow搜索并找不到我的问题的答案(嗯,有人有同样的错误,但不完全像我的)。

所以这是代码:

    public synchronized static void setAllData(String player, String xp, String lvl, String coins, String won, String lost, String kills, String deaths, String redput, String blueput, String time)
{
    try
    {
        PreparedStatement sql =
        connection.prepareStatement("UPDATE `" + Main.table + "` SET `xp` = ?, lvl` = ?,  `towercoins` = ?, `won` = ?, `lost` = ?, `kills` = ?, `deaths` = ?, `redput` = ?, `blueput` = ?, `time` = ? WHERE `player` = ?;");
        sql.setString(1, xp);
        sql.setString(2, lvl);
        sql.setString(3, coins);
        sql.setString(4, won);
        sql.setString(5, lost);
        sql.setString(6, kills);
        sql.setString(7, deaths);
        sql.setString(8, redput);
        sql.setString(9, blueput);
        sql.setString(10, time);
        sql.setString(11, player);
        sql.executeUpdate();
        sql.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

我每次都有这个错误:

  

Java.sql.SQLException:参数索引超出范围(2>数   参数,即1)。

我没有得到它,因为所有参数都在这里等...为什么呢?

非常感谢:)

2 个答案:

答案 0 :(得分:1)

好像你错过了这句话的开头报价。

SET `xp` = ?, lvl` = ?,

答案 1 :(得分:0)

就像这样 connection.prepareStatement(" UPDATE " + Main.table + " SET xp =?,lvl =?,towercoins =?,won =?,{{1 }} =?,lost =?,kills =?,deaths =?,redput =?,blueput =?WHERE time =?;");