Solved

Saving in Objects

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 43
ejb stateless example 2 27
difference between sorce folder and folder in eclipise 3 29
wild fly 8 startup error 2 14
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

807 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