求助,我想对表里的两条数据进行操作,但老是得不到预期结果,代码感觉没问题啊

ccc-c 2016-06-27 05:42:45

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);

}

这是操作之前的数据

例如admin要向test001转1元,但是变成了这样


希望知道的人讲讲。多谢了
...全文
177 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccc-c 2016-06-27
  • 打赏
  • 举报
回复
引用 10 楼 csdn_binger 的回复:
嗯。虽然解决了。但你代码很复杂,不规范。需要优化改进。学学用框架开发。
正在朝这方面学。不是正规军,每天都要抽时间来学,很慢。不过还是挺开心的。哈哈哈哈
csdn_binger 2016-06-27
  • 打赏
  • 举报
回复
嗯。虽然解决了。但你代码很复杂,不规范。需要优化改进。学学用框架开发。
ccc-c 2016-06-27
  • 打赏
  • 举报
回复
引用 7 楼 csdn_binger 的回复:
[quote=引用 6 楼 cqt00 的回复:] [quote=引用 4 楼 csdn_binger 的回复:] [quote=引用 3 楼 cqt00 的回复:] [quote=引用 2 楼 csdn_binger 的回复:] 好奇葩的写法。问题在你更新语句where那。后面没有条件啊。你这样执行更新所有记录不都更新成一样的了吗?where后面是id=XX的。另外看你代码第二个判断没执行。
谢谢,你能再说详细点吗,弄了快一天了。[/quote] update users set money = '" + after + "' where " + rsFirst.getString("id")你这里按你写的执行的语句是“”“update user set money=99999 where 1” 这样执行当然都更新了。admin的语句应该是“update user set money=99999 where id=1” test001的应该是“update user set money=201 where id=2”.另外你代码错误也很多[/quote] 我专业不是软件工程的。我是自学的。有些地方代码不规范可能会有很多错误 还有按照你说的办法用“update user set money=201 where id=2”.ID被限制死了啊,多条数据又会出问题[/quote] 哥哥啊、我那只是给你举得个例子,是指出你的错误所在。代码里当然不能这么写。按你的思路应该是: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();
		}
	}
}
ccc-c 2016-06-27
  • 打赏
  • 举报
回复
引用 7 楼 csdn_binger 的回复:
[quote=引用 6 楼 cqt00 的回复:] [quote=引用 4 楼 csdn_binger 的回复:] [quote=引用 3 楼 cqt00 的回复:] [quote=引用 2 楼 csdn_binger 的回复:] 好奇葩的写法。问题在你更新语句where那。后面没有条件啊。你这样执行更新所有记录不都更新成一样的了吗?where后面是id=XX的。另外看你代码第二个判断没执行。
谢谢,你能再说详细点吗,弄了快一天了。[/quote] update users set money = '" + after + "' where " + rsFirst.getString("id")你这里按你写的执行的语句是“”“update user set money=99999 where 1” 这样执行当然都更新了。admin的语句应该是“update user set money=99999 where id=1” test001的应该是“update user set money=201 where id=2”.另外你代码错误也很多[/quote] 我专业不是软件工程的。我是自学的。有些地方代码不规范可能会有很多错误 还有按照你说的办法用“update user set money=201 where id=2”.ID被限制死了啊,多条数据又会出问题[/quote] 哥哥啊、我那只是给你举得个例子,是指出你的错误所在。代码里当然不能这么写。按你的思路应该是:where id=rsSecond.getString("id").当然你这样写肯定不行。你说你想实现什么功能?[/quote] 第一次操作就是转出(做减)。第二次操作就是转入(做加)。 然后更新数据库,
csdn_binger 2016-06-27
  • 打赏
  • 举报
回复
引用 6 楼 cqt00 的回复:
[quote=引用 4 楼 csdn_binger 的回复:] [quote=引用 3 楼 cqt00 的回复:] [quote=引用 2 楼 csdn_binger 的回复:] 好奇葩的写法。问题在你更新语句where那。后面没有条件啊。你这样执行更新所有记录不都更新成一样的了吗?where后面是id=XX的。另外看你代码第二个判断没执行。
谢谢,你能再说详细点吗,弄了快一天了。[/quote] update users set money = '" + after + "' where " + rsFirst.getString("id")你这里按你写的执行的语句是“”“update user set money=99999 where 1” 这样执行当然都更新了。admin的语句应该是“update user set money=99999 where id=1” test001的应该是“update user set money=201 where id=2”.另外你代码错误也很多[/quote] 我专业不是软件工程的。我是自学的。有些地方代码不规范可能会有很多错误 还有按照你说的办法用“update user set money=201 where id=2”.ID被限制死了啊,多条数据又会出问题[/quote] 哥哥啊、我那只是给你举得个例子,是指出你的错误所在。代码里当然不能这么写。按你的思路应该是:where id=rsSecond.getString("id").当然你这样写肯定不行。你说你想实现什么功能?
ccc-c 2016-06-27
  • 打赏
  • 举报
回复
引用 4 楼 csdn_binger 的回复:
[quote=引用 3 楼 cqt00 的回复:] [quote=引用 2 楼 csdn_binger 的回复:] 好奇葩的写法。问题在你更新语句where那。后面没有条件啊。你这样执行更新所有记录不都更新成一样的了吗?where后面是id=XX的。另外看你代码第二个判断没执行。
谢谢,你能再说详细点吗,弄了快一天了。[/quote] update users set money = '" + after + "' where " + rsFirst.getString("id")你这里按你写的执行的语句是“”“update user set money=99999 where 1” 这样执行当然都更新了。admin的语句应该是“update user set money=99999 where id=1” test001的应该是“update user set money=201 where id=2”.另外你代码错误也很多[/quote] 我专业不是软件工程的。我是自学的。有些地方代码不规范可能会有很多错误 还有按照你说的办法用“update user set money=201 where id=2”.ID被限制死了啊,多条数据又会出问题
ccc-c 2016-06-27
  • 打赏
  • 举报
回复
引用 1 楼 u012943783 的回复:
保持数据一致性 操作完成 再查询
你说的好深奥,我这两条update语句是百度来的。我数据库目前就两条数据 我的本意是对第一个数据操作之后再对第二个数据进行操作。但是第一个数据操作后的结果 更新后覆盖了第二个数据,导致两个数据都一样了。我真的被困在这里了,怎么弄都得不到预期结果,临时去补数据库又不太现实,希望你能讲讲解决的办法。谢谢了
csdn_binger 2016-06-27
  • 打赏
  • 举报
回复
引用 3 楼 cqt00 的回复:
[quote=引用 2 楼 csdn_binger 的回复:] 好奇葩的写法。问题在你更新语句where那。后面没有条件啊。你这样执行更新所有记录不都更新成一样的了吗?where后面是id=XX的。另外看你代码第二个判断没执行。
谢谢,你能再说详细点吗,弄了快一天了。[/quote] update users set money = '" + after + "' where " + rsFirst.getString("id")你这里按你写的执行的语句是“”“update user set money=99999 where 1” 这样执行当然都更新了。admin的语句应该是“update user set money=99999 where id=1” test001的应该是“update user set money=201 where id=2”.另外你代码错误也很多
ccc-c 2016-06-27
  • 打赏
  • 举报
回复
引用 2 楼 csdn_binger 的回复:
好奇葩的写法。问题在你更新语句where那。后面没有条件啊。你这样执行更新所有记录不都更新成一样的了吗?where后面是id=XX的。另外看你代码第二个判断没执行。
谢谢,你能再说详细点吗,弄了快一天了。
csdn_binger 2016-06-27
  • 打赏
  • 举报
回复
好奇葩的写法。问题在你更新语句where那。后面没有条件啊。你这样执行更新所有记录不都更新成一样的了吗?where后面是id=XX的。另外看你代码第二个判断没执行。
拘谨的小人 2016-06-27
  • 打赏
  • 举报
回复
保持数据一致性 操作完成 再查询

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧