62,628
社区成员
发帖
与我相关
我的任务
分享
public void sure_actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/atmuserinfo", "admin",
"admin");
// 连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
System.out.println("Success connect Mysql server!");
String sql = "select * from users";
Statement stmtFirst = connect.createStatement();
ResultSet rsFirst = stmtFirst.executeQuery(sql);
Statement stmtSecond = connect.createStatement();
ResultSet rsSecond = stmtSecond.executeQuery(sql);
while (rsFirst.next()) {
if ((rsFirst.getString("account").equals(username))) {
Integer after = (Integer.parseInt(rsFirst.getString("money"))
- Integer.parseInt(remitText.getText()));
stmtFirst
.executeUpdate("update users set money = '" + after + "' where " + rsFirst.getString("id"));
}
}
rsFirst.close();
while (rsSecond.next()) {
if (rsSecond.getString("account").equals(accountText.getText())) {
Integer result = (Integer.parseInt(rsSecond.getString("money"))
+ Integer.parseInt(remitText.getText()));
stmtSecond.executeUpdate(
"update users set money = '" + result + "' where " + rsSecond.getString("id"));
//JOptionPane.showConfirmDialog(null, "汇款成功!", "Exit", JOptionPane.YES_NO_OPTION);
break;
}
}
if (!rsSecond.next()) {
JOptionPane.showConfirmDialog(null, "账号不存在!", "Exit", JOptionPane.YES_NO_OPTION);
}


哥哥啊、我那只是给你举得个例子,是指出你的错误所在。代码里当然不能这么写。按你的思路应该是:where id=rsSecond.getString("id").当然你这样写肯定不行。你说你想实现什么功能?[/quote]
谢谢你了,我解决了这个问题了刚才。更改后的代码是这样的
public void sure_actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/atmuserinfo", "admin",
"admin");
// 连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
System.out.println("Success connect Mysql server!");
String sql = "select * from users";
Statement stmtFirst = connect.createStatement();
ResultSet rsFirst = stmtFirst.executeQuery(sql);
while (rsFirst.next()) {
if ((rsFirst.getString("account").equals(username))) {
Integer after = (Integer.parseInt(rsFirst.getString("money"))
- Integer.parseInt(remitText.getText()));
stmtFirst.executeUpdate("update users set money = " + after + " where id = " + rsFirst.getString("id")
);
}
}
rsFirst.close();
stmtFirst.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
try {
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/atmuserinfo", "admin",
"admin");
// 连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
System.out.println("Success connect Mysql server!");
String sql = "select * from users";
Statement stmtSecond = connect.createStatement();
ResultSet rsSecond = stmtSecond.executeQuery(sql);
while (rsSecond.next()) {
if (rsSecond.getString("account").equals(accountText.getText())) {
Integer result = (Integer.parseInt(rsSecond.getString("money"))
+ Integer.parseInt(remitText.getText()));
stmtSecond.executeUpdate("update users set money = " + result + " where id = "
+ rsSecond.getString("id"));
JOptionPane.showConfirmDialog(null, "汇款成功!", "Exit", JOptionPane.YES_NO_OPTION);
break;
}
}
if (!rsSecond.next()) {
JOptionPane.showConfirmDialog(null, "账号不存在!", "Exit", JOptionPane.YES_NO_OPTION);
}
rsSecond.close();
stmtSecond.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
哥哥啊、我那只是给你举得个例子,是指出你的错误所在。代码里当然不能这么写。按你的思路应该是:where id=rsSecond.getString("id").当然你这样写肯定不行。你说你想实现什么功能?[/quote]
第一次操作就是转出(做减)。第二次操作就是转入(做加)。
然后更新数据库,
哥哥啊、我那只是给你举得个例子,是指出你的错误所在。代码里当然不能这么写。按你的思路应该是:where id=rsSecond.getString("id").当然你这样写肯定不行。你说你想实现什么功能?