1

I have a table with 25 rows and 3-4 columns. I want to use this table once to read all data from it and store it in Java program. Since I will need data frequently throughout my life cycle of Java program, I dont want to connect to DB and execute query. How can I achieve this?

5
  • 2
    When you say you don't want to connect and execute... do you mean "I don't want to connect and execute every time I need the data" or "I never want to have to connect to the database to get the information stored in that database". One is possible, the other isn't.
    – corsiKa
    Aug 31, 2011 at 5:23
  • I meant I want to connect once and read data into some collection or object. I want this whole thing to be memory efficient also Aug 31, 2011 at 5:58
  • It sounds like you already know exactly what you want to do. Could you elaborate on what you're not sure about on how to do that?
    – corsiKa
    Aug 31, 2011 at 6:00
  • i dont know where to store this data (25 rows and 3-4 columns) so that I can quickly look them up. (I am building a multithreaded backend to process messages. I need to check validity of the field values in the message) Aug 31, 2011 at 6:07
  • I guess I'd have to see some pseudo code or architecture for further assistance. All I can say is "somewhere your threads can access it". It sounds like you want an UnmodifiableCollection of immutable objects (bindings for the table) that can be passed in as are reference to each of your threads, probably at construction. This assumes whatever spawns the threads could potentially access the database.
    – corsiKa
    Aug 31, 2011 at 6:12

4 Answers 4

4

I would create a class with a member that corresponds to the data in the table.

Then I would create some data structure (a map?) by which you can acquire the data.

If you need to have 'database like' access, use an embeded database like Apache Derby. Slurp the data in once from outside, then store it in your local database which will have very responsive lookup times.

Some pseduo-code

Let this class be your binding, for example... I just made stuff up

class MyObject {
    private final String key;
    private final long stamp;
    private final String name;
    private final int other;
    public MyObject(String key, long stamp, String name, int other) {
        this.key = key;
        this.stamp = stamp;
        this.name = name;
        this.other = other;
    }

    public String toString() {
        return name + " has an other of " + other + " and a stamp of " + stamp;
    }

}

Your application might look something like this

class MyApp {

    Connection con = ... // connect to DB here
    // work with DB
    ResultSet rs = ... // query the DB for your data
    Map<String, MyObject> map = new HashMap<String, MyObject>();
    while(rs.next()) {
        String key = rs.getString(1);
        long stamp = rs.getLong(2);
        String name = rs.getString(3);
        int other = rs.getInteger(4);
        MyObject obj = new MyObject(key,stamp,name,other);
        map.put(key,obj);
    }
    Map<String, MyObject> safeMap = Collections.unmodifiableMap(map);

    // spawn 5 threads (yes we should keep ref's to them - oh well
    for(int i = 0; i < 5; i++) {
        Runnable run = new SomeBackgroundProcess(i,safeMap);
        Thread t = new Thread(run);
        t.start();
    }

}

And a background thread might look something like this

class SomeBackgroundProcess {

    final int awesomeNumber;
    final Map<String,MyObject> map;
    SomeBackgroundProcess(int num, Map<String,MyObject> map) {
        this.awesomeNumber = num;
        this.map = map;
    }

    public void run() {
        InputStream in = // some input stream, for example
        while(true) {
            String key = in.magic(); // somehow you acquired a key!
            MyObject obj = map.get(key);
            if(obj != null) Log.log(obj.toString()); // writes out all about the object it found
        }
    }

}
3
  • 1
    SwingWorker is ideal for this during startup; here's a related example.
    – trashgod
    Aug 31, 2011 at 5:27
  • @trashgod I like it. Obviously unnecessary for backend, but for front end stuff it sure can be nice to know what stage you're on (or more appropriately, what stage takes so long that you have time enough to read it's status message!)
    – corsiKa
    Aug 31, 2011 at 5:32
  • hmmm that should be / maybe 2.nd level (form of asked question) +1
    – mKorbel
    Aug 31, 2011 at 6:32
1

if you want to use JTable for display/edit/input/remove some data then you have to

1) first at all read tutorial (all data are allways stored in the TableModel, read this sctions too,)

2) and then go thought ton os examples here and here

3) than if you have a real question how to some..., then don't hesitage to ask question, and in the case that you want from this forum good or better answer, then you have to ... with short and runnable code that shows your issue(s)

0

You can look up hibernate. It will serve you good as it's used frequently in the industry. You basically provides a properties file with your db info such as server address and credentials and then you make a POJO representing your table and put some annotations on it.

0

you can use cache concept which holds the data till the user session is lost. Please check below link, there are many examples available google it. cache concept

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.