gpt4 book ai didi

java.sql.SQLTransactionRollbackException : At least one parameter to the current statement is uninitialized 异常

转载 作者:行者123 更新时间:2023-11-30 10:04:48 27 4
gpt4 key购买 nike

我正在尝试从数据库(在我名为“vsota”的项目中)获取整数值的总和,并将其显示在 JLabel 中(在我名为“vpd”的项目中)。并且提供的代码不起作用。这是异常(exception):

Error: java.sql.SQLTransactionRollbackException: At least one parameter to the current statement is uninitialized.

try {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT SUM(vsota) FROM Vse WHERE vsota = ?";
rs = stmt.executeQuery(sql);

rs.next();
int stevilo = rs.getInt("vsota");
String vsota = Integer.toString(stevilo);

vpd.setText(vsota);

} catch (SQLException ex) {
Logger.getLogger(ZačetniObrazec.class.getName()).log(Level.SEVERE, null, ex);
}

最佳答案

Error: java.sql.SQLTransactionRollbackException: At least one parameter to the current statement is uninitialized.

您的 String 使用参数 vsota = ? 因此您需要使用 PreparedStatement 而不是 Statement能够为其定义一个值。

PreparedStatement ps = null;
ResultSet rs = null;

try{
ps = con.prepareStatement(sql);
ps.setInt(1, vSotaValue); //parameters are 1-based

rs = ps.execute();
} finally {
if(rs != null) try{rs.close();} catch(SQLException e){}
if(ps != null) try{ps.close();} catch(SQLException e){}
}

关于java.sql.SQLTransactionRollbackException : At least one parameter to the current statement is uninitialized 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55685679/

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