Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to store arraylist in derby database

Posted on 2011-03-12
4
Medium Priority
?
985 Views
Last Modified: 2012-05-11
I have 2d array list (an array list of array lists) of integers. I would like to store this array list in a derby database. How to do that?
0
Comment
Question by:chudyksg
  • 2
4 Comments
 
LVL 2

Assisted Solution

by:preraksheth
preraksheth earned 664 total points
ID: 35120475
It should be quite simple
Use a table with two id columns

create table twodarray
(
    id1 numeric(5) not null,
    id2 numeric(5) not null,
    value numeric(7) not null
);

alter table twodarray add constraint twodarray_pk (id1, id2);

And then store your 2d array in this table - the index of first array to go in i1, and second array in i2.
The value will hold the integer value
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 668 total points
ID: 35121868
OTOH, if you want to just write it as one value, the following will do that job
public static String listToStorageString(List<List<Integer>> list) {
        String result = null;
        ByteArrayOutputStream out = null;

        try {
            out = new ByteArrayOutputStream();

            XMLEncoder enc = new XMLEncoder(out);
            enc.writeObject(list);
            enc.close();
            result = new String(out.toByteArray());
        } finally {
            try {
                out.close();
            } catch (IOException e) { /* ignore */
            }
        }

        return result;
    }

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 668 total points
ID: 35123837
you'll need to use a loop, adding one row at a time.
For performance it would be best to use a batch, or at least a PreparedStatement

http://www.java-tips.org/other-api-tips/jdbc/how-to-execute-a-batch-process-with-jdbc.html
http://www.java2s.com/Tutorial/Java/0340__Database/UsingaLooptoSetValues.htm
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35266047
:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question