Solved

how to store arraylist in derby database

Posted on 2011-03-12
4
933 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 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