Stanley Mungai
Stanley Mungai

Reputation: 4150

Pass A resultset outside the While Loop in Java

Hello friends i have this class that is getting values from the database:

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

}
}

When I try to print out the variable "atmsend" outside the While loop, it only gives the last value in the database but inside the for loop, all the records from the database are there. How can I have all the records on the database outside the While loop?

Upvotes: 0

Views: 1518

Answers (4)

Shamis Shukoor
Shamis Shukoor

Reputation: 2515

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

Upvotes: 1

declare atmsend as String array

 String[] atmsend = null;

now when you store the value inside the while loop use

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

to retireve the string results

for(int i=0;i<atmsend.length();i++)
{
SYstem.out.println(atmsend[i]);
}

Upvotes: 0

Jayant Varshney
Jayant Varshney

Reputation: 1825

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

Upvotes: 1

andreih
andreih

Reputation: 433

If you want to put in the variable atmsend all the values from the database, change this code:

atmsend = AtmName + "-" + AtmBal;

to this, although it is not recommended:

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

You could use an ArrayList<String> and add all the values inside it while iterating them, instead of using a String

Upvotes: 2

Related Questions