Solved

Saving in Objects

Posted on 2002-04-02
2
157 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 72
Java Timer (static) 9 36
difference of if loops 23 38
arguments to jar 5 7
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now