• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How to pass arrays using JDBC to database?

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to pass arrays using JDBC to database?
 
author & internet detective
Posts: 42165
937
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
There's no array type in a database. Do you mean to a stored procedure? If so, what stored procedure language?
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Insert every value in a new row. If those values are to be related to a single entity, then use a chain table. This way you can retrieve it back into the entity using a query with a JOIN clause.
 
Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well What i meant is that instead of entering single rows in the table every time can we directly pass a arraylist from java which holds say 5 employee objects which correspond to employee records/rows in emp table in the database.

I understand that we can use prepared statement and then pass the objects one by one. But my question is how do we pass the entire array to database so all records gets inserted at once.

 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can store Java objects serialized in a binary field of the database. But that's a bad practice and bad datamodel. You're going to tight couple Java with a RDBMS. I wouldn't recommend that.

Just create a chain table where in you insert every entity as a new row referenced with a foreign key to the parent entity -if any.

You should not design from Java to datamodel, but from datamodel to Java.
 
Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah i know this way. Do you mean the one below right?

Use Case : Let us suppose that we have a java bean Employee and we are trying to send an array of employee records at a time to database.



Is this bad way though? I am asking this question as it was asked in one of the interviews. I googled and found this solution.

reply
    Bookmark Topic Watch Topic
  • New Topic