my problem here is that if i try to leave blank spaces in my app, this code inserts an empty string into database on lines txtIme.getText() and txtPrezime.getText() and for that reason my if statement doesn't recognize .executeUpdate() as 0 and therefore doesn't print that nothing has been changed. Can I somehow change it so the app can reach my if statement if blank spaces are left in JTextField? Thank you.
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();
}
}
In case you dont want to use any lib
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();
}
}
But I recommend use any library for string related operations like
if(StringUtils.isBlank(txtIme.getText())) return;
if(StringUtils.isBlank(txtPrezime.getText())) return;
It in apache common lang package.