簡體   English   中英

錯誤:com.microsoft.sqlserver.jdbc.SQLServerException:索引 2 超出范圍

[英]Error: com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range

I have a function to update the user's information as follows:

public void updateAccount(String username, String name, String address, String aboutMe, String 
id) {
    String sql = "update Account set username = '?', \n"
            + "                [Full_Name] = '?',\n"
            + "                [Address] = '?',\n"
            + "                [about_me] = '?'\n"
            + "                where id = ?";
    try {
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, username);
        ps.setString(2, name);
        ps.setString(3, address);
        ps.setString(4, aboutMe);
        ps.setString(5, id);
        ps.executeUpdate();

    } catch (Exception ex) {
        Logger.getLogger(AccountDao.class.getName()).log(Level.SEVERE, null, ex);
    }
}

and this code is giving me an error like this: 

Severe: com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.

在 com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191) 在 com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:933) 在 com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue (SQLServerPreparedStatement.java:948) 在 com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1578) 在 dao.AccountDao.updateAccount(AccountDao.java:117) 在 controller.UserProfileController.doPost(UserProfileController.java: 91)

我不明白為什么它會給我錯誤“索引 2 超出范圍”,有什么辦法可以解決嗎?

不封? 引號中的參數標記。 這些由適當的 setter 鍵入:

String sql = "update Account set username = ?, \n"
        + "                [Full_Name] = ?,\n"
        + "                [Address] = ?,\n"
        + "                [about_me] = ?\n"
        + "                where id = ?";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM