2015-09-15 25 views
0

我想防止我的java程序從受理/插入數據庫中的空白或「」值。我已經將列的用戶名和密碼設置爲非空,但它一直接受空白或「」值。如何防止/被添加到Java中的MySQL數據庫限制空值?

繼承人部分我添加值到DB代碼:

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

     try{ 
      theQuery("insert into accounts (username,password) values ('"+user1.getText()+"', '"+pass1.getText()+"')"); 
     } catch (Exception e) { 
      JOptionPane.showMessageDialog(null, "Unable to add record to the database"); 
     } 

} 

附:密碼不會被散列,因爲這僅用於活動。

+0

你需要查詢其添加 –

回答

0

嘗試這樣的:

String str=user1.getText(); 

     if ("".equals(str)){ //User have not entered anything. 
     JOptionPane.showMessageDialog(null,"Please enter your name."); 
     nameField.requestFocusInWindow(); 
     //Do NOT loop here. 
    } 
    else { 
     //Do everything you need to do when the user have entered something 
    } 
+0

由於之前檢查空值!我前一陣子有這個想法,並試圖構建它,但你做得更簡單。 – Rye15

相關問題