0

I'm busy rewriting an Informix-4GL program in Java and I'm having some trouble with retrieving data from the database.

At the moment I am using SQLite3 for testing purposes and I am compiling and executing my code from the windows command line (not using any IDE at this point).

The problem I am having is that I am so used to using the Informix-4GL dynamic array of record such as below:

dataArray dynamic array of record
   data_id     integer,
   data_name   char(15),
   data_desc   char(15)
end record

which I am able to populate as I please and then retrieve data (example the third row inserted) using

display dataArray[3].data_id

I would like to know the recommended way of doing something similair in Java, is it possible to store data in a Java equivalent of the dynamic array of record and then select data from it using some form of identifier?

Here's how I did it in Informix-4GL to make it more clear

main

   define

      lv_string string,                   # general variables
      lv_cnt integer,
      lr_table record like table.*,

      dataArray dynamic array of record   # the values I am storing in array
         data_id     integer,
         data_name   char(15),
         data_desc   char(15)
      end record

   let lv_string = "select * from table ",   # prepared statement 01
                   "where 1=1"
   prepare data_prep from lv_string
   declare data_curs cursor for data_prep

   let lv_string = "select * from table02 ",   # prepared statement 01
                   "where id = ?"
   prepare data02_prep from lv_string

   let lv_cnt = 0

   foreach data_curs into lr_table.*   # loop through sql return results

      let lv_cnt =  lv_cnt + 1

      let dataArray[lv_cnt].data_id = lr_table.id       # store variables
      let dataArray[lv_cnt].data_name = lr_table.name

      execute data02_prep using lr_table.id             # store variables from
         into dataArray[lv_cnt].data_desc               # different table

   end foreach

   if dataArray.getLength() = 5 then         # use the data I have stored
      display "there are 5 rows in here"
   end if

   if dataArray.getLength() = 17 then        # very flexible
      display dataArray[5].data_id,
              dataArray[9].data_name
              dataArray[14].data_desc
   end if

end main

This is not the actual program but I just took the features I would like to recreate in java.

What's great about this is that I can query the database once and close the connection, I won't need to query the database again for the entire duration of the program.

7
  • Either you use Map<String, Object> or you create the special class for each case. Commented Mar 16, 2015 at 14:47
  • I think you need Maps ! Commented Mar 16, 2015 at 14:50
  • There are no records in Java. That is replaced by classes. You should read about object-oriented programming and Java. Commented Mar 16, 2015 at 14:51
  • HashMaps won't work in my case as I have multiple variables Commented Mar 16, 2015 at 15:04
  • I tried to use classes but it only got me half of the way, If I did a select on my table I could use while(row.next()) to loop through and then create an object of each row my sql statement returns except that I would be able to select, say the 3rd object. At least not in a way i know how to do Commented Mar 16, 2015 at 15:06

2 Answers 2

1

I think in your case, using an ArrayList is far more easier. example

public class Sample {
    List<Data> datas = new ArrayList<Data>();

    datas.add(new Data("1", "2" ,"3"));
    datas.add(new Data("4", "5" ,"6"));

    datas.get(0); // should give you the first data
}

class Data {
    Object id,name,desc;
    Data(Object id, Object name, Object desc) {
        this.id = id;
        this.name = name;
        this.desc = desc;
    }
}

by the way, i think the problem you have with HashMap (the one that you say got overridden) is because you didn't override equal and hashcode method as these two methods is used to place the object in HashMap.

Hope this is help you :D

=============

Assuming that your row variable is referring to ResultSet, then you could do this.

public void doFill(ResultSet row) {
    while(row.next()) {
        datas.add(new Data(row.getObject("id"), row.getObject("name"), row.getObject("desc")));
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

this answer is getting really warm, If there is a way to make this dynamic (add to the ArrayList using SQL while looping through the results with the while(row.next()) then this could work). PS the issue with the HashMap was that I couldn't enter more than two variables, it was creating the data object that was overriding.
@Trent yes you could. i'll update the answer then. and by two variables, do you mean like trying to insert two new Data object? like this ? map.put(new Data(1)); map.put(new Data(1));
I have been playing with different ways of achieving the result I want and the way I ended up doing it was essentially this answer in a way so I'm marking this as the right answer \:D/
0

Use HashMap. It's basically a dictionary, array by key, ... whatever you want to call it, it's a key-value store where you can choose your key. HashMap has O(1) operations (theoretically) and forbids duplicate keys. Enjoy.

7 Comments

I tried the HashMap but it looks like they only work with 2 values and in my case I have 3
You could put an object in the map as a value, let's say a class that wraps 2 values?
I have a while loop at the moment which is creating objects from a class, the problem is that it seems to be over-riding the object each time it goes through the loop so it doesn't seem to be storing it
In the loop you could have: Obj o = parse(something); map.put(somekey, o); Maybe you could add your code to your question?
I added my Informix-4GL code into my question to make it a bit clearer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.