我有数据访问对象工厂类。有两种方法:一种是检索数据,另一种是更新表。检索数据的方法可以正常工作。但更新方法不起作用。我无法弄清楚这个问题。请帮忙。
这是工厂类:
public class ChequeDAOImpl implements ChequeDAO {
public DBConnect dbConnection;
@Override
public List<Cheque> getCheques() throws SQLException{
List<Cheque> cheques = new ArrayList<Cheque>();
Connection connection = dbConnection.getConnection();
try{
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery("select * from TblProj");
while(result.next()) {
Cheque cheque = new Cheque();
cheque.setName(result.getString("Name"));
cheque.setAmount(result.getDouble("Amount"));
cheque.setDate(result.getDate("Date"));
cheque.setChqNum(result.getString("CNumber"));
cheque.setValue(result.getDouble("Value"));
cheques.add(cheque);
}
}catch(Exception exception){
exception.printStackTrace();
}finally{
connection.close();
}
return cheques;
}
@Override
public void updateFlag(String chNum) throws SQLException{
DBConnect Connection = null;
Connection conn = Connection.getConnection();
try{
PreparedStatement stmt = (PreparedStatement) conn.prepareStatement("UPDATE TblProj SET flag = ? WHERE CNumber = ? ");
int i= stmt.executeUpdate();
stmt.setString(1, "1");
stmt.setString(2, chNum);
if(i>0)
{
JOptionPane.showMessageDialog(null, "Updated successfully");
}
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.toString());
}
finally{
conn.close();
}
}
public DBConnect getConnection() {
return dbConnection;
}
public void setConnection(DBConnect connection) {
this.dbConnection = connection;
}
}
这就是我使用updateFlag方法的方法:
dao.updateFlag(gui.chqNo);
答案 0 :(得分:0)
请将您的预准备语句重写为如下所示,首先设置prepare语句的参数,然后使用executeUpdate()方法执行。
PreparedStatement stmt =(PreparedStatement)conn.prepareStatement(“UPDATE TblProj SET flag =?WHERE CNumber =?”);
stmt.setInt(1, 1);
stmt.setString(2, chNum);
int i= stmt.executeUpdate();