Solved

how to store arraylist in derby database

Posted on 2011-03-12
4
951 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

734 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