Database Program Help- Saving
Author |
Message |
ZZK
|
Posted: Sun Nov 28, 2010 9:03 am Post subject: Database Program Help- Saving |
|
|
I have written all my Java code, but a part of this assignment is to save the things you have done in the program. I have a soccer database, so if i create a player i will need it to stay in the database, and not disappear after i close the program, is there a line of code that i can input so i can save what i have done. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
jcollins1991
|
Posted: Sun Nov 28, 2010 9:53 am Post subject: Re: Database Program Help- Saving |
|
|
The simplest solution would be saving your players to a text file. Look into CSV (http://en.wikipedia.org/wiki/Comma-separated_values) and file saving in Java. |
|
|
|
|
![](images/spacer.gif) |
Barbarrosa
![](http://compsci.ca/v3/uploads/user_avatars/12538789384c18775f61c5e.jpg)
|
Posted: Sun Nov 28, 2010 1:16 pm Post subject: Re: Database Program Help- Saving |
|
|
Look up Java's Serializable interface. Using that, you can save data from and directly restore data to your program. If you need/want it to be compatible with other apps, then you should use a database format, or the Java CSV library if you don't have any relations or complex constraints on the tables (aside from column types, maybe).
You'll need to write functions to save data from and restore data to your program, no matter which method you use.
http://sourceforge.net/projects/javacsv/
One note: the Java CSV library is under the LGPL. If you don't want to freely share your source code with your app, you should distribute it as a separate package.
http://download.oracle.com/javase/6/docs/api/java/io/Serializable.html |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Sun Nov 28, 2010 2:10 pm Post subject: RE:Database Program Help- Saving |
|
|
If you want a real database (I suspect you do not, unless you really want to learn SQL and require that users install a database program as well as your program), then you should look into PostgreSQL (database server), JDBC (java database connectivity) and SQL (the language you need to talk to the database in).
If you just want to save some small structures, you can do this one of many ways:
1) Manual. You write code to output the contents of your structure in some format, whether human-readable or not. You also write code to read from a file and produce the same structure. This is time-consuming and must be tested carefully. You will also need to update this whenever you change the definition of any of the exported classes.
2) Serialization. You specify that your class can be saved in a "string-like" format, but not how that's done. Java decides how to do the conversion between instances of your class and records in a file. The biggest problem here is that different JVMs (from one platform to another, and from one version to another) do this differently, and will not be able to read files saved on other JVMs. There's also the fact that if the definition of the class ever changes, you can't read the serialized versions of the old class.
3) Standardized format. You specify a way of converting your class to a file and a corresponding way of converting it back, and some framework does all the work for you. This is by far the most professional way of doing things. Typically, corporations like to use XML, but I've had great success with YAML and the Java implementation JYaml. Examples:
Loading your object from a file:
code: | MyClass mc = Yaml.loadType ( file, MyClass.class ); |
Saving your object to a file:
code: | Yaml.dump ( mc, file ); |
This method requires the most knowledge of linking with JARs and will be the most frustrating initially, but will save you a lot of work down the line if this is something you need to do frequently. |
|
|
|
|
![](images/spacer.gif) |
|
|