Link to home
Start Free TrialLog in
Avatar of srikotesh
srikotesh

asked on

better approach to set the values in list if there are no records or vals

qry:            
select txnCount,currency,amount,days from test where country='IN' AND ROWNUM<=5;

IT MAY FETCH MAX 5 RECORDS
SOME TIME 1 or 2 or 3....
if the rocords count is less than 5 i have set val as NA
EACH COLUMN I NEED TO FETCH VALUES AND SET INTO LIST THEN NEED TO CONVERT IT AS STRING

LET SEE if the above qry fect 3 records
i want to see txnCount column as "23,34,56,NA,NA"
OUTPUT
String txnCount-->"23,34,56,NA,NA"

similary for the rest of the columns as well

i have written below code is there any better approach?

ResultSet rs = st.executeQuery(Query());
		List<String> txnCount = new ArrayList<String>();
		List<String> currency = new ArrayList<String>();
		List<String> docAmt = new ArrayList<String>();
		List<String> avgDays = new ArrayList<String>();
		
		
		System.out.println("size " + rs.getFetchSize());
		while (rs.next()){
			txnCount.add(rs.getString(1));
			currency.add(rs.getString(2));
			docAmt.add(rs.getString(3));
			avgDays.add(rs.getString(4));
			year = rs.getString(5);
		}
		System.out.println("txn sisss "+txnCount.size());
		System.out.println(5-txnCount.size());
		int size1 = 5-(txnCount.size());
		for(int i=0;i<size1;i++){
			txnCount.add("NA");
		}
		int size2 = 5-(currency.size());
		for(int i=0;i<size2;i++){
			currency.add("NA");
		}
		int size3 = 5-(docAmt.size());
		for(int i=0;i<size3;i++){
			docAmt.add("NA");
		}
		int size4 = 5-(avgDays.size());
		for(int i=0;i<size4;i++){
			avgDays.add("NA");
		}
		String txnCnt= txnCount.toString().replaceAll("[\\[\\]]", "");
		String curncy= currency.toString().replaceAll("[\\[\\]]", "");
		String amount= docAmt.toString().replaceAll("[\\[\\]]", "");
		String days= avgDays.toString().replaceAll("[\\[\\]]", "");

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

One question right off: why are you storing each column in a separate collection?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.