无法对非静态方法进行静态引用

时间:2014-01-11 15:20:41

标签: java sql

这是我声明变量并获取和设置方法的地方。所有人都在同一个班级内。

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 :(得分:0)

您需要从实际的getUsername()对象调用Users方法。你的意思可能是说Contact.getUsername()