Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here i have a login form includes username and password

and i have another form which can records customer data and there are few text boxes to fill.
like customer name;
customer email and customer contact.

the basically what happens is when user get a call from a customer.
user put his or her user name and password, login to the system.
and user has to fill textboxes and after submit it, data saves in a mysql database.

so mysql has a database called customer.
it has five cloumns
cus id,cusName,cusAdress,cusContact,contactUser

So i have created a class called MysqlConnect which holds all setters and getters.
so i have created a getter and setter called setName and getName


Java
public class MysqlConnect {
        
    String Name;
public void setName(String newName){
    Name = newName;
    }
    
    public String getName(){
    return Name;
    }

}


then i set a value in the login form

Java
nameSet = uName.getText();//gets the username from a textbox
        MysqlConnect cont = new MysqlConnect();
        cont.setName(nameSet);


after that i have implement a getter in the other form call customer data.

Java
MysqlConnect cont = new MysqlConnect();
       String namet = cont.getName();
       JOptionPane.showMessageDialog(null, "Data Saved Successfully."+nameUser+"");


But it shows a null value.

What I have tried:

Getters and Setters as i Described?
Posted
Updated 24-Jan-17 22:49pm

1 solution

Yes, because you create a new instance of MysqlConnect each time, so it will not contain any data. You should have one instance of the class created at the start of your application. That way it will always contain the most up to date values.
 
Share this answer
 
Comments
Hemas Ilearn 25-Jan-17 6:32am    
so then how can do the way i want?
please help me?
Richard MacCutchan 25-Jan-17 6:54am    
The way I explained above. If you do not understand the basics of classes and objects then go back to your reference guides and study the relevant sections.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900