如何判断转账金额的合法性——正则表达式 1)转账金额必须为纯数字,包括整数和小数,小数最多只能两位 Boolean match = transferNum.matches(“^/-?([1-9]/d*|0)(/./d{0,2})?$”); 2)转账金额不能为负数,包括负整数和负小数 Pattern pattern1 = Pattern.compile(“^-[0]/.[1-9]|^-[1-9]/d/./d*”); Matcher isNegativeDecimal = pattern1.matcher(transferNum); Pattern pattern2 = Pattern.compile(“^-[1-9]/d*”); Matcher isNegativeInteger = pattern2.matcher(transferNum); Boolean result = isNegativeDecimal.matches() || isNegativeInteger.matches();
事务回滚 Connection conn = null; try { BigDecimal srcaccount = new BigDecimal(getUser(srcUser).getAccount()); BigDecimal targetaccount = new BigDecimal(getUser(targetUser).getAccount()); BigDecimal transferaccount = new BigDecimal(account); conn = DBUtils.getConnection(); conn.setAutoCommit(false);// 取消自动提交 userdao.updateAccount(conn, srcUser, String.valueOf(srcaccount.subtract(transferaccount))); userdao.updateAccount(conn, targetUser, String.valueOf(targetaccount.add(transferaccount))); conn.commit();// 提交 } catch (Exception e) { try { conn.rollback();// 回滚 throw new TransferFailureException(ConstCodeUtils.TRANSFER_FAILURE_4); } catch (SQLException e1) { e1.PRintStackTrace(); } e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } }
字符串为一系列数字时,直接相加减 相加:String.valueOf(targetaccount.add(transferaccount) 相减:String.valueOf(srcaccount.subtract(transferaccount)
比较两个字符串的大小 int result = curuserAccount.compareTo(transferNum); result大于0则curuserAccount较大,等于0则两个数相等,小于0则curuserAccount较小。
新闻热点
疑难解答