This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes best way to query and insert into different table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "best way to query and insert into different table" Watch "best way to query and insert into different table" New topic
Author

best way to query and insert into different table

Gary Frick
Greenhorn

Joined: Sep 13, 2002
Posts: 28
What is the best way to move a record from one table to another? In other words, I need to do a query, do some checking on the record, and then insert into another table. Do I have to get each field by its respective type and build a new statement, or is there a simpler approach?
Ken Robinson
Ranch Hand

Joined: Jul 25, 2002
Posts: 52
What you can do for this may depend on if both tables are in the same DB, what DB you are using and where you are willing to put the code (Java side or DB side).
If both tables are in the same DB, I personally would write a Stored Procedure in the database to do this. The DB would take parameters in, check if you want to move the field and then move the field. Depending on your preference, you can either return a flag to tell the Java program the logic worked or throw an exception back to the calling client (java) if it did not work (and this is an exception condition in you app).
I use Oracle where I am working right now. I usually wrap all of my DB inserts in stored procs. If a condition of the insert is not met, I simple throw an exception back to the calling program. Putting the logic in the stored proc allows the same logic to be used from anywhere (Java, VB, Unix) that may call the proc.
Gary Frick
Greenhorn

Joined: Sep 13, 2002
Posts: 28
Ken,
I don't have an option on the server side. Besides I need to pull it to the client-side for visual checks and such before inserting it into another table (which is in the same DB). So, I'm trying to find the stream-lined way of taking the record from the ResultSet and inserting it into a look-a-like table.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: best way to query and insert into different table
 
Similar Threads
Know auto incremented pimary key
Need a Help!!
how to get the inserted record by using resultset in java?
How Do I Insert records in multiple tables thru 1 SQL Query
Help with HQL Insert and Joins using Hibernate 3 / JPA