• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Clone a record in the database or element in an arrayList

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41914
910
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41914
910
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This tiny ad is wafer thin:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic