每次刷新都会更改数据库中的值

时间:2013-05-31 11:37:49

标签: java mysql jsp struts2

我正在使用struts2和jsp进行查看,这里我面临一个问题。每当我刷新数据库中的值都会被更改,所以当我尝试将值恢复到jsp时,它会给我那些更改的值,这不是我想要的那个我实际想要的时候我只点击那个时候它应该被更改而不是当我刷新。知道如何解决它吗?任何帮助将不胜感激。

package com.rajesh.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.opensymphony.xwork2.ActionSupport;

public class DepositAction extends ActionSupport {

private int accountno;
private String username;
private String password;
private String amount;
private int dataamount = 0;

public int getDataamount() {

return dataamount;
}

public void setDataamount(int dataamount) {
this.dataamount = dataamount;
}

public int getAccountno() {
return accountno;
}

public void setAccountno(int accountno) {
this.accountno = accountno;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getAmount() {
return amount;
}

public void setAmount(String amount) {
this.amount = amount;
}

public String execute() throws Exception {

Connection con = GetCon.getCon();

PreparedStatement ps = con.prepareStatement("Select * from account where accountno=? and username = ? and password =?");
ps.setInt(1, accountno);
ps.setString(2, username);
ps.setString(3, password);

ResultSet rs = ps.executeQuery();

boolean status = rs.next();

System.out.println(status);

if (status == true) {

PreparedStatement ps1 = con.prepareStatement("Select * from account where accountno=?");

ps1.setInt(1, accountno);

ResultSet rs1 = ps1.executeQuery();

if (rs1.next()) {
dataamount = Integer.parseInt(amount) + rs.getInt(5);
System.out.println(rs1.getInt(5));
System.out.println(dataamount);

}

PreparedStatement ps2 = con.prepareStatement("update account set amount=? where accountno='"
                        + accountno + "'");
ps2.setInt(1, dataamount);
int state = ps2.executeUpdate();

if (state > 0)
System.out.println("AMOUNT DEPOSITED");

return SUCCESS;

} else {
return "error";
}
}
}

和我的deposit.jsp

<div id="header12">
<table>
<tr><td><h4>amount deposited is:<s:property value="amount"/> Rs</h4></td></tr>
<tr>
<td align="center" valign="middle"><h4>your current Balance is:<s:property value="dataamount" />   Rs</h4></td>
</tr>
</table>

和我的deposit1.jsp

<form name=F1 onSubmit="return dil(this)" action="depositAction" method="post">
<table cellspacing="5" cellpadding="3">
<tr><td>ACCOUNT NO:</td><td> <input type="text" name="accountno"/></td></tr>
<tr><td>USER NAME:</td><td> <input type="text" name="username"/></td></tr>
<tr><td>PASSWORD:</td><td> <input type="password" name="password"/></td></tr>
<tr><td>AMOUNT:</td><td> <input type="text" name="amount"/></td></tr>

1 个答案:

答案 0 :(得分:1)

您遇到双重提交问题

传统方式是使用发布/重定向/获取,Struts2上更难的方式是使用令牌拦截器,但如果你不是旧目标浏览器,就这样做:

<body>
    ....

    <script>
        window.history.pushState("","", location.href);
    </script>
</body>

这样可以防止在刷新时重新提交。

this answer上的链接。