JDBC compare the content of two tables

Reply

Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

JDBC compare the content of two tables

 
0
  #1
Aug 9th, 2007
Hello everyone,


I am using JDBC to compare the content of two database tables -- writing a general function and two tables are input. The two tables have various types of columns, like VARCHAR, BLOB, INT, FLOAT, etc.

I want to save my time to write various statements to get values by different types and compare them, for example, for VARCHAR, I need to use getString and compare the content by String compare function, and for INT column, I need to use getInt and compare with Integer compare function.

I am wondering whether there are any smart way to implement in an uniformed way so that I do not need to write various switches according to column type.


thanks in advance,
George
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: JDBC compare the content of two tables

 
0
  #2
Aug 9th, 2007
Yes and no...
Several things come to mind.

An enum with a lookup method by column type that returns a handler for that column type is the most obvious.
Use a Map of column type names to enum instances for that.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: JDBC compare the content of two tables

 
0
  #3
Aug 10th, 2007
Thanks jwenting,


I am not going to compare the column types, but content of table -- rows.

Originally Posted by jwenting View Post
Yes and no...
Several things come to mind.

An enum with a lookup method by column type that returns a handler for that column type is the most obvious.
Use a Map of column type names to enum instances for that.
The two tables are of the same schema -- same column names and column type.

I am interested in your above method. But I still need to program differently for different column types to write equals (or compareTo) method, right?

Are there any ways to implement in an unified coding way independent of column types?

regards,
George
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,357
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: JDBC compare the content of two tables

 
0
  #4
Aug 10th, 2007
Edit: Nevermind, silly me, thinking of something completely different.
Last edited by masijade; Aug 10th, 2007 at 8:29 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: JDBC compare the content of two tables

 
0
  #5
Aug 10th, 2007
I am interested in your above method. But I still need to program differently for different column types to write equals (or compareTo) method, right?
Of course. How else are you going to get the data out of the resultset?
Last edited by ~s.o.s~; Aug 11th, 2007 at 2:33 pm. Reason: Fixed the quote tags.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: JDBC compare the content of two tables

 
0
  #6
Aug 12th, 2007
Hi masijade, any ideas?

Originally Posted by masijade View Post
Edit: Nevermind, silly me, thinking of something completely different.

regards,
George
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: JDBC compare the content of two tables

 
0
  #7
Aug 12th, 2007
Hi jwenting,


Originally Posted by jwenting View Post
Of course. How else are you going to get the data out of the resultset?
I want to get the data from each column in a unified way and compare in a unified way. Any ideas?


regards,
George
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: JDBC compare the content of two tables

 
0
  #8
Aug 12th, 2007
the JDBC API has several options that may work.
getBytes(int) and getObject(int) on ResultSet come to mind.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: JDBC compare the content of two tables

 
0
  #9
Aug 12th, 2007
Thanks jwenting,

Originally Posted by jwenting View Post
the JDBC API has several options that may work.
getBytes(int) and getObject(int) on ResultSet come to mind.
I want to use getObject method to get the value of each column,
then use equals method of java.lang.Object to compare two columns' values. Is that method workable?

I have a quick question, I think if I use getObject to get a column value
from a table, for example, an Integer column, then when using equals method to compare, I think I am using equals method from java.lang.Integer, other than java.lang.Object, right?

Object int_column_value1 = getObject (...);
Object int_column_value2 = getObject (...);
// to check
int_column_value1.equals (int_column_value2);


regards,
George
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC