简体   繁体   中英

load table in Java program

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?

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
        }
    }

}

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)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM