java org.postgresql.util.PSQLException:列索引超出范围:5、列数:4

eh57zj3b  于 2023-06-28  发布在  Java
关注(0)|答案(2)|浏览(306)

我正在做一个简单的客户银行应用程序,我被困在这里。我试图更新数据库中的一行,我使用Eclipse,Dbeaver和PostqreSQL。

public Client updateClient (int clientId, AddUpdateDTO client) throws SQLException 
        
        try (Connection con = JDBCUtility.getConnection()){
            String sql = "UPDATE clients"
                    + "SET client_first_name = ?, "
                    + "client_last_name = ?, "
                    + "client_phone_number = ?, "
                    + "client_email"
                    + "WHERE"
                    + "client_id = ?; ";            
            PreparedStatement pst = con.prepareStatement(sql);
            
            pst.setString(1, client.getFirstName());
            pst.setString(2, client.getLastName());
            pst.setString(3, client.getPhoneNumber());
            pst.setString(4, client.getEmail());
            pst.setInt(5, clientId);
            
            int updatedAmount = pst.executeUpdate();
            
            if (updatedAmount != 1) {
                throw new SQLException("Unable to find client record with Id of " +                          clientId);
            }
        }
        
        return new Client(clientId, client.getFirstName(), client.getLastName(), client.getPhoneNumber(), client.getEmail());       
    }

下面是我的数据库表:

CREATE TABLE clients (client_id SERIAL PRIMARY KEY,
    client_first_name VARCHAR(255) NOT NULL,
    client_last_name VARCHAR(255) NOT NULL,
    client_phone_number VARCHAR(255) NOT NULL,
    client_email VARCHAR(255) NOT NULL  
);

我所有的其他查询工作,如添加,删除,getAll等,我只是不能弄清楚这一个。

cetgtptt

cetgtptt1#

我想你错过了client_email字段的赋值。您的查询应该如下所示:

String sql = "UPDATE clients"
                    + "SET client_first_name = ?, "
                    + "client_last_name = ?, "
                    + "client_phone_number = ?, "
                    + "client_email = ?"
                    + "WHERE"
                    + "client_id = ?; ";
1tuwyuhd

1tuwyuhd2#

谢谢,伙计们,那个小错误要了我的命,现在效果很好。

相关问题