2 回答

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊
在驗(yàn)證之前,您不應(yīng)該將數(shù)據(jù)插入數(shù)據(jù)庫(kù)。僅當(dāng)數(shù)據(jù)不為空時(shí)才插入數(shù)據(jù)。至于檢查數(shù)據(jù)是否為空,最好修剪文本,以便在給定的空白表示某個(gè)值的情況下,任何空白都會(huì)被修剪掉。
if(amount.getText().trim().isEmpty()||unit.getText().trim().isEmpty()||
status.getText().trim().isEmpty()){
JOptionPane.showMessageDialog(null,"please enter data");
// errorname2.setText("fill this field");
}
請(qǐng)確保您將修剪后的數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中。

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
在應(yīng)用語(yǔ)句之前嘗試驗(yàn)證您的金額字段是否為空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);
}
}
添加回答
舉報(bào)