File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How to pass arrays using JDBC to database? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to pass arrays using JDBC to database?" Watch "How to pass arrays using JDBC to database?" New topic
Author

How to pass arrays using JDBC to database?

Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
How to pass arrays using JDBC to database?


Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

There's no array type in a database. Do you mean to a stored procedure? If so, what stored procedure language?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Aug 02, 2005
Posts: 153
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Aug 02, 2005
Posts: 153
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass arrays using JDBC to database?