Merging Two ResultSets
Hi,
i have two Resultsets one isrs1 and 2nd one isrs2 which are having data like...
The First resultsets meansrs1 have data:
String bcode = rs1.getString("bankcode");
String outwardcount = rs1.getString("outwardcount ");
String outwardAmt = rs1.getString("outwardAmt ");
This result is displaying like this...
bcodeoutwardcountoutwardAmt
10 1 2
20 3 4
30 5 6
The First resultsets meansrs2 have data:
String bcode1 = rs12.getString("bankcode");
String inwardcount = rs2.getString("inwardcount ");
String inwardAmt = rs2.getString("inwardAmt ");
This result is displaying like this...
bcode inwardcount inwardAmt
1078
509 11
Then I need to merge the above resultsets and my desired output should be like...
bcodeoutwardcountoutwardAmtinwardcountinwardAmt
101 2 78
203 4 00
30 5 6 00
50 0 0 911
here the bank code is same for both resultsets.
Can u please solve this problem and tell me
how to compare one resultset to another by using hashtable or anything...
if the bank code is equal then the corresponding inwardacount and inwardAmt should be merge with the resultset one
[1307 byte] By [
kagitalaa] at [2007-11-27 7:28:58]

you can create your data object first:
// BankRecord.java
public class BankRecord {
public String bankCode,
outwardCount,
outwardAmt,
inwardCount,
inwardAmt;
public BankRecord() {
// if you like to assume null = 0
bankCode = "";
outwardCount = "0";
outwardAmt = "0";
inwardCount = "0";
inwardAmt = "0";
}
public String toString() {
return bankCode + " " + outwardCount + " " + outwardAmt + " " + inwardCount + " " + inwardAmt;
}
}
and used the data object here instead of using String array:
// put to hash table
Hashtable<String, BankRecord> table = new Hashtable<String, BankRecord>();
// handle rs 1
while (rs1.next()) {
BankRecord rec = new BankRecord();
rec.bankCode = rs1.getString("bankcode");
rec.outwardCount = rs1.getString("outwardcount ");
rec.outwardAmt = rs1.getString("outwardAmt ");
}
// handle rs 2
while (rs2.next()) {
BankRecord rec = table.get(rs2.getString("bankcode");
if (rec == null) rec = new BankRecord();
rec.bankCode = rs2.getString("bankcode");
rec.inwardCount = rs2.getString("inwardcount ");
rec.inwardAmt = rs2.getString("inwardAmt ");
}
// print result
for (Enumeration<BankRecord> recs = table.elements(); recs.hasMoreElements(); ) {
System.out.println(recs.nextElement());
}
Thank u very much but one more thing how can i display individual results like bankcode, outwardCount, outwardAmt, inwardCount, inwardAmt valuesI need to print the above valuesCan u please tell me I need urgent
previous example, indeed print the result by calling BankRecord.toString() automatically.
// print result
for (Enumeration<BankRecord> recs = table.elements(); recs.hasMoreElements(); ) {
System.out.println(recs.nextElement());
}
here is an example, to print it individually:
for (Enumeration<BankRecord> recs = table.elements(); recs.hasMoreElements(); ) {
BankRecord rec = recs.nextElements();
System.out.println("*Bank code: " + rec.bankCode);
System.out.println(" *outwardCount: " + rec.outwardCount);
System.out.println(" *outwardAmt: " + rec.outwardAmt);
System.out.println(" *inwardCount: " + rec.inwardCount);
System.out.println(" *inwardAmt: " + rec.inwardAmt);
System.out.println();
}
I applied for your code but no records are displayed
for (Enumeration<BankRecord> recs = table.elements(); recs.hasMoreElements(); ) {
BankRecord rec = recs.nextElement();
System.out.println("*Bank code: " + rec.bankCode);
System.out.println(" *outwardCount: " + rec.outwardCount);
System.out.println(" *outwardAmt: " + rec.outwardAmt);
System.out.println(" *inwardCount: " + rec.inwardCount);
System.out.println(" *inwardAmt: " + rec.inwardAmt);
System.out.println();
}
sorry!... i put a 's' char at recs.nextElement() before. above code should be okay.
i modifed already that is not a problem friend.
i printed the values on RS1 then all the values are printed and
Rs2 also printed but when i try to print all the results in for loop no values are displaying..
In for loop means Enumeration no values are printed
Can u please solve this problem
lol... i forgot to add to table =)
// put to hash table
Hashtable<String, BankRecord> table = new Hashtable<String, BankRecord>();
// handle rs 1
while (rs1.next()) {
BankRecord rec = new BankRecord();
rec.bankCode = rs1.getString("bankcode");
rec.outwardCount = rs1.getString("outwardcount ");
rec.outwardAmt = rs1.getString("outwardAmt ");
// add this
table.put(rec.bankCode, rec);
}
// handle rs 2
while (rs2.next()) {
// modify this.
BankRecord rec = table.get(rs2.getString("bankcode");
if (rec == null) {
rec = new BankRecord();
rec.bankCode = rs2.getString("bankcode");
rec.inwardCount = rs2.getString("inwardcount ");
rec.inwardAmt = rs2.getString("inwardAmt ");
table.put(rec.bankCode, rec);
} else {
rec.inwardCount = rs2.getString("inwardcount ");
rec.inwardAmt = rs2.getString("inwardAmt ");
}
}
Really fantasticI am really Thankful to uIt's working
hi,by using The same program just i am generating the report based on the Date. i need to enter the Date through command promptmeans when i run the program i need to pass the date parameter to the main method. Can u please solve this problem
The user may enter date in any format but i need the date format as YYYYMMDD. how to check the date format when user enter the wrong date. if user entered the wrong date format then we prompt message has to be displayed .Can u Please solve this problem.
Are you for real? Learn how to program you lazy idiot!
I am sorry i could not get ui am real i really need your help