This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to pass arrays using JDBC to database?

 
Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to pass arrays using JDBC to database?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic