Solved

Saving in Objects

Posted on 2002-04-02
2
159 Views
Last Modified: 2011-09-20
I have a AWT Frame which consists 3 text fields
namely for ex:  emp Name,dept and salary . in addition the frame consists 2 buttons, save and exit.

When i enter the data in the Text Fields and click save
the data should be saved into an object and not inside a database table nor inside a .dat file.

Again I shud retrive back the data from the object and display the contents onto another Screen.

0
Comment
Question by:yrrm
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
msterjev earned 70 total points
ID: 6915242
You can create this object.

public class Credentals implements Serializable
{
     String name;
     String dept;
     String salary;
}

You store variables from text fields as data members of this class.

For example:

credentals.name=nameField.getText();
credentals.dept=deptField.getText();
credentals.salary=salaryField.getText();

The persistance is achieved using ObjectInputStream and ObjectOutputStream.

ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("credentals"));
out.writeObject(credentals);

At the start of the program you can retrieve the credentals with:

ObjectInputStream in=new ObjectInputStream(new FileInputStream("credentals"));
Credentals credentals=(Credentals) in.readObject();
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6915740
The points....
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java JRE greater than 1.6 5 62
tomcat administrtor 12 47
use lov values 2 50
maven module vs maven project 3 23
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question