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
Solved

how to store arraylist in derby database

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 72
Spring Framework HTTPSession management 1 45
difference between sorce folder and folder in eclipise 3 28
Crystal Reports Licensing Questions 4 34
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

856 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