代码:
public static int getBalance()
{
List <Integer> sum = new ArrayList<Integer>();
int balance=0;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/money","root","cpp");
PreparedStatement ps = con.prepareStatement("select * from account");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
sum.add(Integer.parseInt(rs.getString(1)));
for(int i:sum)
{
balance += i;
}
}
}
}
当显示金额时,它已添加两倍的时间,例如,如果2000只是当前金额,则当前余额为4000
1条答案
按热度按时间62lalag41#
对找到的每个结果执行一次,因此如果有两个结果,则金额将相加两次:
试着把那个街区搬走
while
声明。你可以了解更多while
在这里:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html