User data persistence

M

Mike

I am writing a Java application that uses about 1000 parameters in
various calculations. All these parameters are displayed to the user in
a JTable, and any can be modified. I would like to persist the user
changes to these parameters, on the local machine. I'm new to Java, and
am considering what the best options are for data persistence. It seems
to me that, in this case, there are too many values to consider saving
them using java.util.prefs.Preferences, and distributing a database
with the app is probably overkill.

I have previously written something similar using Microsoft VB.NET,
where I created an XML file with some initial data, and distributed it
with the application. The XML file was read into a DataSet, which was
bound to DataGrid. If the user changed any values in the grid, the
underlying DataSet was updated, and the XML file re-written to disk. Is
it possible to do something similar in Java? I have looked at the
Connection and ResultSet interfaces, but it does not appear that they
can be used to read and write XML files.

Mike.
 
D

David Harper

Mike said:
I am writing a Java application that uses about 1000 parameters in
various calculations. All these parameters are displayed to the user in
a JTable, and any can be modified. I would like to persist the user
changes to these parameters, on the local machine. I'm new to Java, and
am considering what the best options are for data persistence. It seems
to me that, in this case, there are too many values to consider saving
them using java.util.prefs.Preferences, and distributing a database
with the app is probably overkill.

I have previously written something similar using Microsoft VB.NET,
where I created an XML file with some initial data, and distributed it
with the application. The XML file was read into a DataSet, which was
bound to DataGrid. If the user changed any values in the grid, the
underlying DataSet was updated, and the XML file re-written to disk. Is
it possible to do something similar in Java? I have looked at the
Connection and ResultSet interfaces, but it does not appear that they
can be used to read and write XML files.

You're right that a database is overkill.

Even an XML file is over-complicating what is basically a simple
problem. XML is useful when you need to store a complex hierarchical
data structure, but you have a flat list of key/value pairs, and this
argues strongly for a simpler solution.

You should probably consider using a hash table to store the key-value
pairs internally within your program. As such, java.util.Properties
meets your needs better than java.util.prefs.Preferences because
Properties is sub-classed from java.util.Hashtable with additional
methods to read and write a text file of key-value pairs.

David Harper
Cambridge, England
 
T

Tom N

Mike said:
I am writing a Java application that uses about 1000 parameters in
various calculations. All these parameters are displayed to the user in
a JTable, and any can be modified. I would like to persist the user
changes to these parameters, on the local machine. I'm new to Java, and
am considering what the best options are for data persistence. It seems
to me that, in this case, there are too many values to consider saving
them using java.util.prefs.Preferences, and distributing a database
with the app is probably overkill.

I have previously written something similar using Microsoft VB.NET,
where I created an XML file with some initial data, and distributed it
with the application. The XML file was read into a DataSet, which was
bound to DataGrid. If the user changed any values in the grid, the
underlying DataSet was updated, and the XML file re-written to disk. Is
it possible to do something similar in Java? I have looked at the
Connection and ResultSet interfaces, but it does not appear that they
can be used to read and write XML files.

Have a look at java.beans.XMLEncoder
and java.beans.XMLDecoder
 
Y

Yamin

I'll second that one. XMLEncoder/Decoder makes persistence really
really really easy. All the work has been done for you...no rewriting
the wheel

Encoder.write(any bean object)
Object = Decoder.read();

That's really it. You don't need to write anything fancy...no read
method or write methods...you just make sure anythign you write is a
java bean (all needed members have getter/setter,.default
constructor....)

File size can grow, but you have a fixed structure, so it should be
fine.

Yamin Bismilla
 
V

Virgil Green

Mike said:
I am writing a Java application that uses about 1000 parameters in
various calculations. All these parameters are displayed to the user
in a JTable, and any can be modified. I would like to persist the user
changes to these parameters, on the local machine. I'm new to Java,
and am considering what the best options are for data persistence. It
seems to me that, in this case, there are too many values to consider
saving them using java.util.prefs.Preferences, and distributing a
database with the app is probably overkill.

I have previously written something similar using Microsoft VB.NET,
where I created an XML file with some initial data, and distributed it
with the application. The XML file was read into a DataSet, which was
bound to DataGrid. If the user changed any values in the grid, the
underlying DataSet was updated, and the XML file re-written to disk.
Is it possible to do something similar in Java? I have looked at the
Connection and ResultSet interfaces, but it does not appear that they
can be used to read and write XML files.

Mike.

I'm curious... Why do you think that a bloated (IMHO) XML file would be
okay, but you think that the number of entries would be "too many" for a
Preferences implementation? Note that I have not used the Preferences class.
I'm merely curious. I do have a (possibly irrational) dislike for XML on
principle which helps stoke this curiosity.

Regardless... I'm thinking a Properties implementation would probably
suffice... or a serialized HashTable.
 
R

Raymond DeCampo

Virgil said:
I'm curious... Why do you think that a bloated (IMHO) XML file would be
okay, but you think that the number of entries would be "too many" for a
Preferences implementation? Note that I have not used the Preferences class.
I'm merely curious. I do have a (possibly irrational) dislike for XML on
principle which helps stoke this curiosity.

You might not be pleased to know then that the Preferences API uses XML
under the hood.
Regardless... I'm thinking a Properties implementation would probably
suffice... or a serialized HashTable.

What I don't understand is why the original OP decided to use XML and
then looked at the database interface code and concluded there was
nothing to help him.

Ray
 
R

Roedy Green

I have previously written something similar using Microsoft VB.NET,
where I created an XML file with some initial data, and distributed it
with the application. The XML file was read into a DataSet, which was
bound to DataGrid. If the user changed any values in the grid, the
underlying DataSet was updated, and the XML file re-written to disk. Is
it possible to do something similar in Java? I have looked at the
Connection and ResultSet interfaces, but it does not appear that they
can be used to read and write XML files.

Mike.

Is it just a list or a complex tree of information?

Possibilities:

properties file http://mindprod.com/jgloss/properties.html
a csv file http://mindprod.com/jgloss/csv.html
a serialised file: http://mindprod.com/jgloss/serialization.html
a one item per line text file.
a DataOutputStream : http://mindprod.com/jgloss/fileio.html
xml with custom parser : http://mindprod.com/jgloss/xml.html

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top