在Eclipse中用Java和MySql資料庫實現登入功能

Ingenuus發表於2007-07-05
try{    
    String managerName
="";
    String managerPassword
="";
                
       //是一個訪問資料庫的封裝類,具體程式碼在
       //http://blog.csdn.net/ingenuus/archive/2007/06/21/1660217.aspx
    ConDB conn 
= new ConDB();
    String sql 
= "select * from loguser";//loguser為表名
    conn.executeQuery(sql);
                
    ResultSet resultSet 
= conn.executeQuery(sql);
    
while(resultSet.next())
    
{
                      managerName 
= resultSet.getString("userName");
                      managerPassword 
= resultSet.getString("userPassword");
                          //此方法是採用SWT做的,以下的text和text_1,是輸入使用者名稱和密碼的框
                          //相當與JAVA中的JTextField和JPasswordField型別的變數
                      
if(text.getText().toString().equals(managerName))
                      
{
                                 
if(text_1.getText().toString().equals(managerPassword))
                            
{
                                        SelectService selectService 
= new SelectService();
                                        shell.setVisible(
false);
                                        selectService.open();
                             }

                            
else
                             
{
                                       JOptionPane.showMessageDialog(
null,"請輸入正確的密碼!!!");
                                       text_1.setText(
"");
                              }

                    
                      }

                      
else
                      
{
                                    JOptionPane.showMessageDialog(
null,"請輸入正確的使用者名稱!!!");
                                    text.setText(
"");
                      }

          }
//end while
                
}
catch(SQLException ee)
{
    System.out.println(ee);
}

相關文章