准备好的语句将字符串“”而不是 null 插入数据库

Prepared statement inserting String " " into database instead of null

我的问题是,如果我尝试在我的应用程序中保留空格,此代码会在第 txtIme.getText() 和 txtPrezime.getText() 行向数据库中插入一个空字符串,出于这个原因我的 if 语句无法将 .executeUpdate() 识别为 0,因此不会打印出没有任何更改。如果 JTextField 中留有空格,我可以以某种方式更改它以便应用程序可以访问我的 if 语句吗?谢谢。

    private void btnDodajActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {

        izraz = veza.prepareStatement("insert into autor (ime,prezime)" + "value (?,?)");

        izraz.setString(1, txtIme.getText());
        izraz.setString(2, txtPrezime.getText());



        if (izraz.executeUpdate()== 0) {
            JOptionPane.showMessageDialog(getRootPane(), "Nothing was changed.");

        } else {
            ucitajIzBaze();
            ocistiPolja();
        }

        izraz.close();

    } catch (SQLException ex) {
        ex.printStackTrace();
    }

}                          

如果您不想使用任何库

private void btnDodajActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {

        if(txtIme.getText() == null || "".equals(txtIme.getText())) return;
        if(txtPrezime.getText() == null || "".equals(txtPrezime.getText())) return;
        izraz = veza.prepareStatement("insert into autor (ime,prezime)" + "value (?,?)");


        izraz.setString(1, txtIme.getText());
        izraz.setString(2, txtPrezime.getText());



        if (izraz.executeUpdate()== 0) {
            JOptionPane.showMessageDialog(getRootPane(), "Nothing was changed.");

        } else {
            ucitajIzBaze();
            ocistiPolja();
        }

        izraz.close();

    } catch (SQLException ex) {
        ex.printStackTrace();
    }

}    

但我建议使用任何库进行字符串相关操作,例如

if(StringUtils.isBlank(txtIme.getText())) return;
if(StringUtils.isBlank(txtPrezime.getText())) return;

它在 apache common lang 包中。