944,156 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 6775
  • Java RSS
Aug 9th, 2007
0

JDBC compare the content of two tables

Expand Post »
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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Aug 9th, 2007
0

Re: JDBC compare the content of two tables

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 10th, 2007
0

Re: JDBC compare the content of two tables

Thanks jwenting,


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

Click to Expand / Collapse  Quote originally posted by jwenting ...
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
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Aug 10th, 2007
0

Re: JDBC compare the content of two tables

Edit: Nevermind, silly me, thinking of something completely different.
Last edited by masijade; Aug 10th, 2007 at 8:29 am.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Aug 10th, 2007
0

Re: JDBC compare the content of two tables

Quote ...
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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 12th, 2007
0

Re: JDBC compare the content of two tables

Hi masijade, any ideas?

Click to Expand / Collapse  Quote originally posted by masijade ...
Edit: Nevermind, silly me, thinking of something completely different.

regards,
George
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Aug 12th, 2007
0

Re: JDBC compare the content of two tables

Hi jwenting,


Click to Expand / Collapse  Quote originally posted by jwenting ...
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
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Aug 12th, 2007
0

Re: JDBC compare the content of two tables

the JDBC API has several options that may work.
getBytes(int) and getObject(int) on ResultSet come to mind.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 12th, 2007
0

Re: JDBC compare the content of two tables

Thanks jwenting,

Click to Expand / Collapse  Quote originally posted by jwenting ...
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
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Installing Jconnector for MySql Server
Next Thread in Java Forum Timeline: illegal start of expression & type





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC