2014-12-21 72 views
-2

當激活loginButton時,它給了我錯誤:java.lang.NullPointerException。我相信這是由於我準備好的聲明。下面是代碼:努力從MYSQL服務器檢索用戶名和密碼

package foodbankdatabase3; 
 

 
import java.awt.*; 
 
import java.awt.event.*; 
 
import java.sql.*; 
 
import javax.swing.*; 
 

 
public class FoodBankDatabase extends JFrame{ 
 
    /* 
 
    Initializing Variables 
 
    */ 
 
    static String testConnect; 
 
    static Connection conn; 
 
    static PreparedStatement pst; 
 
    static ResultSet rs; 
 
    /* 
 
    Initializing JPanels 
 
    */ 
 
    JPanel contPanel = new JPanel(); 
 
    JPanel loginGui = new JPanel(); 
 
    JPanel usernamePanel = new JPanel(); 
 
    JPanel passwordPanel = new JPanel(); 
 
    JPanel mainMenuGui = new JPanel(); 
 
    JPanel loginButtonPanel = new JPanel(); 
 
    JPanel testConnectPanel = new JPanel(); 
 
    /* 
 
    Initializing JLabels 
 
    */ 
 
    JLabel usernameLabel = new JLabel("Username: "); 
 
    JLabel passwordLabel = new JLabel("Password: "); 
 
    JLabel testConnectLabel = new JLabel(testConnect); 
 
    /* 
 
    Initializing JTextFields 
 
    */ 
 
    JTextField usernameTextField = new JTextField(10); 
 
    JPasswordField passwordTextField = new JPasswordField(10); 
 
    /* 
 
    Initializing JButtons 
 
    */ 
 
    JButton loginButton = new JButton("Click here to login"); 
 
    JButton logoutButton = new JButton("Click here to logout"); 
 
    JButton conductInterview = new JButton("Conduct an interview"); 
 
    JButton addClient = new JButton("Add a new client"); 
 
    JButton viewClient = new JButton("View existing client details"); 
 

 
    
 
    CardLayout cardLayout = new CardLayout(); 
 
    
 
    public FoodBankDatabase(){ 
 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 
     initComponents(); 
 
     pack(); 
 
     setVisible(true); 
 
    } 
 
    
 
    private void initComponents(){ 
 
     contPanel.setLayout(cardLayout); 
 
     
 
     /* 
 
     Login Gui Components 
 
     */ 
 
     loginGui.setLayout(new BoxLayout(loginGui, BoxLayout.Y_AXIS)); 
 
     usernamePanel.add(usernameLabel); 
 
     usernamePanel.add(usernameTextField); 
 
     passwordPanel.add(passwordLabel); 
 
     passwordPanel.add(passwordTextField); 
 
     loginButtonPanel.add(loginButton); 
 
     testConnectPanel.add(testConnectLabel); 
 
     loginGui.add(usernamePanel); 
 
     loginGui.add(passwordPanel); 
 
     loginGui.add(loginButtonPanel); 
 
     loginGui.add(testConnectPanel); 
 
     
 
     /* 
 
     Main Menu Gui Components 
 
     */ 
 
     mainMenuGui.setLayout(new BoxLayout(mainMenuGui, BoxLayout.Y_AXIS)); 
 
     mainMenuGui.add(conductInterview); 
 
     mainMenuGui.add(addClient); 
 
     mainMenuGui.add(viewClient); 
 
     mainMenuGui.add(logoutButton); 
 
     
 
     contPanel.add(loginGui, "loginGui"); 
 
     contPanel.add(mainMenuGui, "mainMenuGui"); 
 
     
 
     cardLayout.show(contPanel, "loginGui"); 
 
     
 
     loginButton.addActionListener(new ActionListener() { 
 
      @Override 
 
      public void actionPerformed(ActionEvent e) { 
 
       String sql="SELECT * FROM loginDetails WHERE username=? AND password=?"; 
 
       try{ 
 
        pst = conn.prepareStatement(sql); 
 
        pst.setString(1, usernameTextField.getText()); 
 
        pst.setString(1, passwordTextField.getText()); 
 
        
 
        rs=pst.executeQuery(); 
 
        if(rs.next()){ 
 
         JOptionPane.showMessageDialog(null, "Username and Password are correct"); 
 
        }else{ 
 
         JOptionPane.showMessageDialog(null, "Username and Password are not correct"); 
 
        } 
 
        
 
       }catch(Exception sqle){ 
 
        JOptionPane.showMessageDialog(null, sqle); 
 
       } 
 
      } 
 
     }); 
 
     
 
     logoutButton.addActionListener(new ActionListener() { 
 
      @Override 
 
      public void actionPerformed(ActionEvent e) { 
 
        cardLayout.show(contPanel, "loginGui"); 
 
      } 
 
     }); 
 
     
 
     add(contPanel); 
 
    } 
 
    
 
    private static void testConnection(){ 
 
     try{ 
 
      Connection con = DriverManager.getConnection("jdbc:mysql://sql5.freemysqlhosting.net:3306/sql562010", "sql562010", "bV5*yJ5%"); 
 
      
 
      Statement stat = (Statement) con.createStatement(); 
 
      
 
      testConnect = "Connection successful!"; 
 
     }catch(Exception e){ 
 
      testConnect = "Connection unsuccessful"; 
 
      System.out.println(e); 
 
     } 
 
    } 
 
    
 
    public static void main(String[] args) {   
 
     testConnection(); 
 
     new FoodBankDatabase(); 
 
    } 
 
}

感謝您的幫助:)

它看起來像你的文章大多代碼等等等等

+1

請不要存儲在純文本和加鹽的數據庫中的密碼。 –

+0

我最終會加鹽並散列它,此刻我只是試圖連接並與阻塞的數據庫進行交互 –

+1

您尚未初始化'conn'變量。在你的測試方法中,你定義了一個新的連接,但是你沒有把它分配給'conn'。這就是爲什麼它給出一個空指針。 – PalinDrome555

回答

-2
public void actionPerformed(ActionEvent e) { 
      String sql="SELECT * FROM loginDetails WHERE username? AND password=?"; 

必須

public void actionPerformed(ActionEvent e) { 
      String sql="SELECT * FROM loginDetails WHERE username=? AND password=?"; 
+0

我試過你的建議,但它仍然給出同樣的錯誤! –

+0

你對此正確,但它不會導致NPE。 – EJP

0

沒有你們在哪裏有幫助,我自己修復它。

loginButton.addActionListener(new ActionListener() {@ 
 
    Override 
 
    public void actionPerformed(ActionEvent e) { 
 
    String sql = "SELECT * FROM loginDetails WHERE username=? AND password=?"; 
 
    try { 
 
     pst = conn.prepareStatement(sql); 
 
     pst.setString(1, usernameTextField.getText()); 
 
     pst.setString(1, passwordTextField.getText()); 
 

 
     rs = pst.executeQuery(); 
 
     if (rs.next()) { 
 
     JOptionPane.showMessageDialog(null, "Username and Password are correct"); 
 
     } else { 
 
     JOptionPane.showMessageDialog(null, "Username and Password are not correct"); 
 
     } 
 

 
    } catch (Exception sqle) { 
 
     JOptionPane.showMessageDialog(null, sqle); 
 
    } 
 
    } 
 
});

應改爲:

loginButton.addActionListener(new ActionListener() {@ 
 
    Override 
 
    public void actionPerformed(ActionEvent e) { 
 

 
    try { 
 
     String user = usernameTextField.getText().trim(); 
 
     String pass = passwordTextField.getText().trim(); 
 

 
     String sql = "select username,password from loginDetails where username = '" + user + "'and password = '" + pass + "'"; 
 
     rs = st.executeQuery(sql); 
 

 
     int count = 0; 
 
     while (rs.next()) { 
 
     count++; 
 
     } 
 

 
     if (count == 1) { 
 
     System.out.println("User found, access granted!"); 
 
     cardLayout.show(contPanel, "mainMenuGui"); 
 
     } else if (count > 1) { 
 
     JOptionPane.showMessageDialog(null, "Duplicate user, access denied!"); 
 
     } else { 
 
     JOptionPane.showMessageDialog(null, "Incorrect username or password!"); 
 
     } 
 
    } catch (Exception sqle) { 
 
     System.out.println(sqle); 
 
    } 
 
    } 
 
});

相關問題