amitbidwai
amitbidwai

Reputation: 21

i want the data to be save sucessfully

I have a JFrame name opinion. in this form I have given a error message for each field means whenever I am filling the form and if I put any field empty and than click save button it should display that error message for that perticular empty field is empty. my code is saving the data but when I put any field empty and click on save button it shoowing the error msg that perticular field is empty but when I click on ok button of that error msg box it also saving the data but with that empty field my code is:

    String name= (String)cbNM.getSelectedItem();
    if(name.equals(""))
        JOptionPane.showMessageDialog(null," ENTER THE NAME");

    String socialservice=(String)cbsocial.getSelectedItem();
    if(socialservice.equals(""))
        JOptionPane.showMessageDialog(null," SELECT THE SOCIAL SERVICE");

    String whichservice=tasocial.getText();
    if(whichservice.equals(""))
        JOptionPane.showMessageDialog(null," ENTER SOCIAL SERVICE U LIKE");

    String expectation=taexpectation.getText();
    if(expectation.equals(""))
        JOptionPane.showMessageDialog(null," ENTER UR EXPECTATION FROM WANI SAMAJ");

    String revealinfo=(String)cbreveal.getSelectedItem();
    if(revealinfo.equals(""))
        JOptionPane.showMessageDialog(null,"SELECT UR DECISION ON REVEAL THE INFORMATION");

    String addr=taaddress.getText();
    if(addr.equals(""))
        JOptionPane.showMessageDialog(null," ENTER THE ADDRESS");



    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con=DriverManager.getConnection("jdbc:odbc:wanisamajDB");
        Statement stmt=con.createStatement();
        String qry= "INSERT INTO Opinion(PersonName,LikeSocialService,WhichService,Expectation,RevealInformation,Address) VALUES('"+name+"','"+socialservice+"','"+whichservice+"','"+expectation+"','"+revealinfo+"','"+addr+"')";
        stmt.executeUpdate(qry);
        JOptionPane.showMessageDialog(null,"RECORD IS SAVED SUCCESSFULLY ");
        con.close();

    } catch(SQLException eM) {
        JOptionPane.showMessageDialog(null,"RECORD IS NOT SAVED");
    } catch(Exception et) {
        System.out.println("error:"+et.getMessage());
    }

Upvotes: 0

Views: 153

Answers (2)

MByD
MByD

Reputation: 137282

After you show the message, your method continue to run because you didn't break the run in any step:

if(name.equals(""))
        JOptionPane.showMessageDialog(null," ENTER THE NAME");

The basic solution (though pretty ugly) is to add return after message appears:

if(name.equals(""))
{
        JOptionPane.showMessageDialog(null," ENTER THE NAME");
        return;
}

A better solution is to create an array of the JTextAreas (I guess that's what the prefix ta stands for) and an array of the CheckBox / RadioButtons and iterate them. For example:

At init or GUI building:

String    tainputnames[] = {"Social Service", "Expectation", "Address"};
JTextArea tainput[] = new JTextArea[tainputnames.length];
for (int i=0; i < tainputnames.length; i++)
{
    tainput[i] = new JTextArea("");
    tainput[i].setName(tainputnames[i]);
}

At the check:

for (int i = 0; i < tainputnames.length; i++)
{
    if (tainput[i].getText().equals("")
    {
        OptionPane.showMessageDialog(null, "Enter " + tainputnames[i]);
        return;
    }
}

the same for check boxes etc.

Upvotes: 2

Brian Roach
Brian Roach

Reputation: 76898

That's because you're not stopping it from doing so:

if(whichservice.equals(""))
    JOptionPane.showMessageDialog(null," ENTER SOCIAL SERVICE U LIKE");

Once the call to showMessageDialog() returns you let it continue down to your code that saves it in the database.

if(whichservice.equals(""))
{
    JOptionPane.showMessageDialog(null," ENTER SOCIAL SERVICE U LIKE");
    return;
}

Or, you could restructure your code to be a series of if/else if with the code that saves to the database being contained in a final else at the end.

Upvotes: 1

Related Questions