我希望数据保存成功

时间:2011-04-06 17:00:18

标签: java

我有一个JFrame名称意见。在这种形式我给每个字段的错误信息意味着每当我填写表格,如果我把任何字段空,而不是单击保存按钮,它应显示该特定空字段的错误消息为空。我的代码正在保存数据,但是当我把任何字段放空并点击保存按钮时,显示错误信息,表示相关字段为空,但是当我点击该错误信息框的确定按钮时,它还保存数据但是使用该空字段我的代码是:

    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());
    }

2 个答案:

答案 0 :(得分:2)

显示消息后,您的方法将继续运行,因为您没有在任何步骤中中断运行:

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

基本解决方案(虽然非常难看)是在消息出现后添加return

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

更好的解决方案是创建一个JTextAreas数组(我猜这是前缀ta代表的)和一个CheckBox / RadioButtons数组并迭代它们。例如:

在init或GUI构建中:

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]);
}

检查:

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

复选框等相同。

答案 1 :(得分:1)

那是因为你没有阻止它这样做:

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

一旦对showMessageDialog()的调用返回,您就可以继续执行将其保存在数据库中的代码。

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

或者,您可以将代码重构为一系列if / else,如果保存到数据库的代码最后包含在最终else中。