无法更新ATM界面中存款交易后的余额
我正在使用Java Swing进行GUI在Java的ATM界面进行项目。在数据库中的存款交易后,我无法更新余额。我找不到我出错的地方。
我有一个名为-tryproject的数据库,其中表为 - CustomerDetails
,其中有cardnum
,cardpin
and code> balance> balance /代码>作为列。实际上,我会遇到一个错误,因为参数索引超出了范围1>参数的数量为0。我正在发送我的存款模块守则。
代码:-
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
int depositAmount = Integer.valueOf(jTextField1.getText());
int cardnum = Integer.valueOf(jTextField2.getText());
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/TYProject","root","riddhimore");
PreparedStatement pst = con.prepareStatement("select * from customerDetails where cardnum=?");
pst.setInt(1, cardnum);
ResultSet rs = pst.executeQuery();
if(rs.next()){
rs.getInt("balance");
String sql = "update customerDetails set balance=balance+"+depositAmount+ "where cardnum=?";
PreparedStatement pst1 = con.prepareStatement(sql);
pst.setInt(1,cardnum);
if(pst1.executeUpdate() == 1){
int a = JOptionPane.showConfirmDialog(this, "Are you sure you want to submit ?", "Confirm ?", JOptionPane.YES_NO_CANCEL_OPTION);
if(a == 0){
new atmFrame10().setVisible(true);
this.setVisible(false);
}
}
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
I'm doing a project on ATM interface in Java using Java swing for GUI. I'm not able to update the balance after the deposit transaction in the database. I'm not able to find where I'm going wrong.
I've a database named - TYProject, in it the table is - customerDetails
and in it there's cardnum
, cardpin
and balance
as columns. Actually I'm getting an error as parameter index out of range 1> number of parameters which is 0. I'm sending my code of deposit module.
Code:-
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
int depositAmount = Integer.valueOf(jTextField1.getText());
int cardnum = Integer.valueOf(jTextField2.getText());
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/TYProject","root","riddhimore");
PreparedStatement pst = con.prepareStatement("select * from customerDetails where cardnum=?");
pst.setInt(1, cardnum);
ResultSet rs = pst.executeQuery();
if(rs.next()){
rs.getInt("balance");
String sql = "update customerDetails set balance=balance+"+depositAmount+ "where cardnum=?";
PreparedStatement pst1 = con.prepareStatement(sql);
pst.setInt(1,cardnum);
if(pst1.executeUpdate() == 1){
int a = JOptionPane.showConfirmDialog(this, "Are you sure you want to submit ?", "Confirm ?", JOptionPane.YES_NO_CANCEL_OPTION);
if(a == 0){
new atmFrame10().setVisible(true);
this.setVisible(false);
}
}
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了避免
indexoutofBoundSexceptions
(或任何所谓的),我养成了使用三元运算符的习惯。这是保险的,以确保您不会从范围中调用索引:这将转化为:
编辑以进行澄清
我知道这不是一个完整的解决方案
,但建议避免一些例外
To avoid the
IndexOutOfBoundsExceptions
(or whatever it is called) I have made a habit of using a ternary operators. This acts as insurance to make sure you aren't calling an index out of bounds:This translates to:
EDIT for clarification
I understand that this is not a full solution to
but advice to avoid some exceptions