我正在尝试从数据库(在我名为“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/