gpt4 book ai didi

java.sql.SQLException : invalid column index

转载 作者:行者123 更新时间:2023-11-30 02:46:27 24 4
gpt4 key购买 nike

我一直在尝试使用preparedStatement选择数据并传递这些参数,但发生了异常。

有人可以帮助我吗?这是我的代码。

Connection connection = DBConnection.getConnection();
String query = "select * from integrantes where nome like '?' or rm like '?'";
ArrayList<Integrante> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, ('%' + term + '%'));
preparedStatement.setString(2, ('%' + term + '%'));
JOptionPane.showMessageDialog(null, ((OraclePreparedStatement) preparedStatement).getOriginalSql());
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
list.add(new Integrante(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3), resultSet.getInt(4)));
}
} catch(SQLException e) {
JOptionPane.showMessageDialog(null, "Erro ao listar o(s) integrante(s)." + e);
} catch(NullPointerException e) {
JOptionPane.showMessageDialog(null, "Ocorreu um erro inesperado.");
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "Ocorreu um erro no código.");
}

return list;

最佳答案

使用 PreparedStatement 时,不得在将要替换为字符串的占位符周围加上引号。您的发言

"select * from integrantes where nome like '?' or rm like '?'"

导致问号被视为文字,因此该语句有零个参数,从而导致异常。将其更改为

"select * from integrantes where nome like ? or rm like ?"

关于java.sql.SQLException : invalid column index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40067365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com