【问题标题】:"Textfield validation not working properly"“文本字段验证无法正常工作”
【发布时间】:2019-08-28 04:09:21
【问题描述】:

验证部分的问题,没有关于空字段的消息显示。马上帮忙

private void addbtnActionPerformed(java.awt.event.ActionEvent evt) {

    try{

    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/dbfinance","root","1234");
    String sql="insert into util(type_,due_date,month_,amount,units,status_) values(?,?,?,?,?,?)";
    PreparedStatement pstm =con.prepareStatement(sql);

    pstm.setString(1,electype.getSelectedItem().toString());
    pstm.setString(2,date.getText().toString());
    pstm.setString(3,jComboBox1.getSelectedItem().toString());
    pstm.setDouble(4,Double.parseDouble(amount.getText()));
    pstm.setString(5,unit.getText());
    pstm.setString(6,status.getText());



   pstm.executeUpdate();
   JOptionPane.showMessageDialog(null, "success");
   con.close();

        }catch(Exception e){

            JOptionPane.showMessageDialog(null,e);

        }
     if(amount.getText().isEmpty()||unit.getText().isEmpty()||status.getText().isEmpty()){

            JOptionPane.showMessageDialog(null,"please enter data");

           // errorname2.setText("fill this field");
    }        
}

【问题讨论】:

  • 你说的是哪个空域?鉴于您分享的信息有限,我们甚至无法重现您面临的问题,因此无法找到解决方案。
  • 在此程序金额文本字段中,单位文本字段和状态文本字段不应为空(用户应填写该字段)。如果为空,应显示消息“请输入数据”。但是问题是没有显示消息。

标签: java netbeans


【解决方案1】:

您不应该在验证之前将数据插入数据库。仅当数据不为空时才插入数据。至于检查数据是否为空,最好修剪文本以便修剪掉任何空格,因为空格表示某些值。

if(amount.getText().trim().isEmpty()||unit.getText().trim().isEmpty()||
            status.getText().trim().isEmpty()){
        JOptionPane.showMessageDialog(null,"please enter data");
        // errorname2.setText("fill this field");
    }

请确保您将修剪后的数据插入到数据库中。

【讨论】:

    【解决方案2】:

    在应用try-catch 语句之前尝试验证您的金额字段是否为空:

    if(amount.getText().isEmpty()||unit.getText().isEmpty()||status.getText().isEmpty()){
            JOptionPane.showMessageDialog(null,"please enter data");
            // errorname2.setText("fill this field");
        }else{
            try{
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/dbfinance","root","1234");
                String sql="insert into util(type_,due_date,month_,amount,units,status_) values(?,?,?,?,?,?)";
                PreparedStatement pstm =con.prepareStatement(sql);
                pstm.setString(1,electype.getSelectedItem().toString());
                pstm.setString(2,date.getText().toString());
                pstm.setString(3,jComboBox1.getSelectedItem().toString());
                pstm.setDouble(4,Double.parseDouble(amount.getText()));
                pstm.setString(5,unit.getText());
                pstm.setString(6,status.getText());
                pstm.executeUpdate();
                JOptionPane.showMessageDialog(null, "success");
                con.close();
    
            }catch(Exception e){
                JOptionPane.showMessageDialog(null,e);
            }
        }
    

    【讨论】:

    • 现在它给出消息“java.lang.NumberFormatException:empty string”。但需要显示消息“请输入数据”。
    • 可能isEmpty() 方法无法正常工作,请尝试将每个字段替换为amount.getText().equals("")
    • 同样的问题。现在它将数据插入数据库而不在状态文本字段中输入任何值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多