0

你好朋友我有这个从数据库中获取值的类:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailAlerts {


    public static void main(String[] args) {
SMTPAuthenticator sm = new SMTPAuthenticator();//new class
        Connection conn = null;
        String AtmName = "";
        String AtmBal = "";
        String atmsend = "";
        String[] bals = null;
 try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.80:1521:dev9i", "BI", "bi");
            Statement st1 = conn.createStatement();
            String atms = "select CLR_BAL_AMT, FORACID, ACCT_NAME from gam "
                    + "where BACID = 'ATM' and CLR_BAL_AMT < 100000";
            ResultSet rs1 = st1.executeQuery(atms);
            while (rs1.next()) {
                AtmName = rs1.getString("ACCT_NAME");
                AtmBal = rs1.getString("CLR_BAL_AMT");
                bals = AtmBal.split("\n");
                for (int j = 0; j < bals.length; j++) {
                    atmsend = AtmName + "-" + AtmBal;
                    //System.out.println(atmsend);
                }

            }
            System.out.println(atmsend);
}catch(Exception ad){
System.out.println(ad);
}

}
}

当我尝试在 While 循环外打印出变量“atmsend”时,它只给出数据库中的最后一个值,但在 for 循环内,数据库中的所有记录都在那里。如何在 While 循环之外拥有数据库中的所有记录?

4

4 回答 4

2

如果要将atmsend数据库中的所有值放入变量中,请更改以下代码:

atmsend = AtmName + "-" + AtmBal;

对此,虽然不推荐:

atmsend += AtmName + "-" + AtmBal + "\n";

您可以ArrayList<String>在迭代它们时使用并添加其中的所有值,而不是使用String

于 2012-12-03T11:00:00.430 回答
1
atmsend = atmsend + AtmName + "-" + AtmBal +"\n";
于 2012-12-03T10:55:33.607 回答
1
ArrayList <String> atmsend= new ArrayList<>();

Use Arraylist

 while (rs1.next()) {
                AtmName = rs1.getString("ACCT_NAME");
                AtmBal = rs1.getString("CLR_BAL_AMT");
                bals = AtmBal.split("\n");
                for (int j = 0; j < bals.length; j++) {
                    atmsend.add(AtmName + "-" + AtmBal+"\n");
                    //System.out.println(atmsend);
                }

System.out.println(atmsend.toString());
于 2012-12-03T11:00:26.883 回答
0

将 atmsend 声明为字符串数组

 String[] atmsend = null;

现在,当您将值存储在 while 循环中时,请使用

atmsend[i] = AtmName + "-" + AtmBal;

检索字符串结果

for(int i=0;i<atmsend.length();i++)
{
SYstem.out.println(atmsend[i]);
}
于 2012-12-03T11:03:34.763 回答