我試圖顯示一個消息框時,無論是用戶名或密碼文本框爲空,但是當我運行該項目唯一的文本框顯示的是「JOptionPane.showMessageDialog(NULL,」您的用戶名或密碼不正確「) ;」請幫忙,謝謝!檢查空的文本框
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String user=txtUsername.getText();
char[] pass=txtPassword.getPassword();
if(user == null){
JOptionPane.showMessageDialog(null,"Username Field is empty");
}
else if(pass==null){
JOptionPane.showMessageDialog(null,"Password Field is empty");
}
else{
String sql="select * from account where username=? and password=?";
try{
pst=conn.prepareStatement(sql);
pst.setString(1,txtUsername.getText());
pst.setString(2,txtPassword.getText());
rs=pst.executeQuery();
if(rs.next()){
GridlockMain a=new GridlockMain();
a.setVisible(true);
}
else{
JOptionPane.showMessageDialog(null,"Your Username or Password is incorrect");
txtUsername.setText(null);
txtPassword.setText(null);
}
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,e);
}
}}
我從來沒有聽說過一個字符爲長小於0的字符。如果你要解決這個問題,這個答案將被等同於subash的【答案】(http://stackoverflow.com/a/20263060/369450),這使得這個答案毫無意義。 – cpburnz