Solved

how to store arraylist in derby database

Posted on 2011-03-12
4
893 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 166 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 167 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 167 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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 functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now