This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clone a record in the database or element in an arrayList

 
Reggie McDougal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a result set saved into an ArrayList, how would I go about cloning
a record or element in the arrayList.

Then if I add an new record I have to insert in instead of updating in the database.

How might bI appraoch this problem.

Reg
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reggie,
You have an ArrayList or what? Different resultsets? References to something?

If you had DTO (data transfer objects), it would make sense to clone the object. If they are really database objects, you have to make the copy yourslef and change the fields before adding it. An O/R framework would also be useful in abstracting the database code.
 
Reggie McDougal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok lets just say I want to clone a row in a table, how maight I do that?

Reg
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reg,
Using pure JDBC:
1) Read row from table into data structure
2) Create new row with that data (and different primary key)

As I said earlier, I think you would benefit from using an O/R framework. You are already thinking at a higher level of abstraction (thinking about cloning rather than copying rows) so it may be easier to work with objects.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reggie,

I want to clone a row in a table

Do you mean that you want to insert the same row, a second time? In other words, you want the same row to appear twice in the same table? You are aware, I assume, that you won't be able to do this if there is a unique index (or primary key) defined for the particular table, aren't you?

Or do you want to copy a row from one table to another?

In either case, you can do this via SQL alone. Most databases support something like:

So no need for data structures in java. (Or am I missing something?)

Good Luck,
Avi.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic