2014-01-11 67 views
-3

這是我聲明變量和get和set方法的地方。所有人都在同一班。無法對靜態方法進行靜態引用

public class Users 
{ 
    private String username; 

    public void setUsername(String username) 
    { 
    this.username = username; 
    } 

    public String getUsername() 
    { 
    return username; 
    } 

    public static ArrayList<Users> getAllContacts(String f) { 

    Users Contact; 
    ArrayList<Users> ContactList = new ArrayList<Users>(); 
    String query = "select * from UsersDB where username = ?"; 
    // connect to DB 
    currentCon = DBController.getConnection1(); 

    try { 
     pstmt = currentCon.prepareStatement(query); 

這是它不能靜態引用非靜態方法的部分,它在同一個類中。

pstmt.setString(1, getUsername()); // Cannot make a static reference to the non-static method 
     rs = pstmt.executeQuery(); 
     while (rs.next()) { 
      int id = rs.getInt("id"); 
      String name = rs.getString("name"); 
      int mobile = rs.getInt("phone_Number"); 
      String gender = rs.getString("gender"); 
      String email = rs.getString("email"); 
      String birth = rs.getString("birthDate"); 

      Contact = new Users(name, email, gender, mobile, birth); 
      ContactList.add(Contact); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return ContactList; 
} 

} 
+1

在所有相關問題上向右看。 –

+0

我相信錯誤信息告訴你你需要知道的一切。 –

+1

當您輸入問題標題時,彈出幾個問題;它是這樣做的,因爲許多問題已經在SO上被詢問和回答。 –

回答

0

您需要從實際的Users對象中調用getUsername()方法。可能你的意思是說Contact.getUsername()

相關問題