我正在尝试从数据库(在我的项目中称为“vsota”)中获取整数值的SUM并将其显示在JLabel
(在我的项目中称为“vpd”)中。并且提供的代码不起作用。这是一个例外:
错误:java.sql.SQLTransactionRollbackException:当前语句的至少一个参数未初始化。
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);
}
错误:java.sql.SQLTransactionRollbackException:当前语句的至少一个参数未初始化。
你的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){}
}