Add new entry of Table to A to table B
Hii,
I have 2 tables, say A and B. Table B stores the exact same data as table A. Whenever there is a new entry in tale A, I also want to add that value to table B. I have a Batch JOb in Iava and I want to write this code in it.
Can anybody please help me,I know it's complicated but..plz help me with sample code
and it's very very urgent so your help will be really appreciated!!
Thanks in advance
-ASH
[444 byte] By [
ASH_2007a] at [2007-11-27 9:53:55]

# 1
I'm confused what your problem is -- if you know how to update Table A why not just run that code twice and update Table B on the second time? What exactly is your problem? Are these actual database tables (like MySQL or Oracle)?
# 2
Yes, both of them are actual table. But user can access and update only table A. Table B i have created, no one perform any operation on that. I am creating table B and I am populating that table because I want all the historical data for this year in that. For example,if someone deletes some data from table A it will still exist in table B, so that I can view all the data for this year,even thought they have deleted from application/system.
So I just want new data to be added to table B.
SO I am running batch job to see when ever there is new entry in tbale A,I want that entry to be added in table B.
Are you foloowing me?
In short, I have one table in Teradata and one in DB2.both contains same data.
Whenever there is anew entry added in Teradata table I want to execute insert statement in DB2 also to add the same value..
plz help me..
thanks
# 3
can you just tell me,how can I COMPARE two tables for change?tell me how to do it with sempole code,,if possiblethanks
# 4
Have a stored procedure/trigger, that will take care of this issue. This is the best solution I think.
# 5
I know,but due to usr requirements, I have to do in java class.I am doing that in my java class-batch jobplz helpCan not use stored procedure/triggerMessage was edited by: ASH_2007
# 6
Have some flag to identify any transaction in Table A. Based on the flag update Table B. It's like calling two DB inserts consecutively in your java class.
# 7
Thanks people for help.i have figured it out.I put my values from database into an arraylist and compare it..thanks again..god bless you all
# 8
Yeah I don't see what the problem is, this should be very straightforward.
Are you able to update Table A? If so, when you update A, update B at the same time? And if you don't want to copy deletes to table B, then if you detect a delete on A, ignore it on B.
Does this make sense? For instance, if you had the following functions.....
insertA(stuff) {
// ... insert into A code
insertB(stuff)
}
insertB(stuff) {
// ... insert into B code
}
updateA(stuff) {
// .... update A code
updateB(stuff)
}
updateB(stuff) {
// ...update B code
}
selectA(stuff) {
// ...select from A code
}
deleteA(stuff) {
// ... delete from A code
}
Please tell me if i am missing something because this seems almost trivial if you already know how to add/modify records on table A.
# 9
thanks den2681Actually, i am comparing two arraylist.if al1 has something which al2 does not then add that to al2.so want to compare values of al1 and al2Could you help me?
# 10
I suppose you could write a loop, going over al1 and for each item scan all of al2 to see if they match.
Make sure whatever object you are comparing override the .equals(...) method and make a fair comparison, otherwise your results could be off. You might also want to create a deepClone() function which will make a perfect copy of whatever Object you are comparing.
Please post if you are still confused.
# 11
ArrayList Customers = new ArrayList() ;
ArrayList Customers1 = new ArrayList() ;
String query =
"SELECT DISTINCT CUST_NM "+
"FROM table " + "ORDER BY CUST_NM";
System.out.println("About to execute query #######"+query);
ResultSet resultset = statement.executeQuery(query);
int count =0;
while(resultset.next())
{
Customers.add(resultset.getString(1).trim());
count ++;
}
System.out.println("count for Teradata customer Names:"+count);
//check the DB2 table
String query1 =
"SELECT Customer_Name "+
"FROM table " + "ORDER BY Customer_Name";
System.out.println("About to execute query ########"+query1);
ResultSet resultset1 = statement1.executeQuery(query1);
int count1 =0;
while(resultset1.next())
{
Customers1.add(resultset1.getString(1).trim());
count1 ++;
}
System.out.println("count for Db2 customer Names:"+count1);
boolean comp=false;
for (int i = 0; i < Customers.size(); i++)
{
String cust = Customers.get(i).toString().trim();
comp = false;
System.out.println("Cust Name : " + cust);
select count(*) from tls. where custname = cust
if(Customers1.contains(cust))
{
System.out.println("Has Customer : " + cust + " in the list");
comp = true;
}
if (comp==true)
continue;
else
{
System.out.println("Going to insert customer : " + cust);
String qry =
"INSERT INTO .....
}
resultset.close();
statement.close();
resultset1.close();
statement1.close();
}
catch(Exception exception2)
{
logger.log(2, "Unable to select count(*) from teradata");
logger.log(exception2);
return;
}
This onw doesn't work properly,some names which are same..still takes as different..(goes into comp=false for some names)
Help?
Thanks
Message was edited by:
ASH_2007
# 12
hi Ash,r u find the solution for this problem.Bala
# 13
nothing is jumping out at me that could be wrong here. try some massive debugging...
1) once you fill Customers and Customers1 print out all the values in each and make sure they look right
2) when you get into that comp == false section, print out the value you were searching for and make sure it wasn't in Customers1
as long as you are using strings you should be OK, however, if they differ in case it will return false. For example, the ArrayList.contains(...) method will return false if it compares "Joe Montana" and "JoE Montana" because they differ in case. It is a case-sensitive comparison.