我想将数据库中的数据显示到JList中,但是它给了错误
non static variable list cannot be referenced from a static context"
at line "list.setModel(listModel)".
我该怎么办?
Connection con=DriverManager.getConnection("jdbc:odbc:Records");
Statement st=con.createStatement();
DefaultListModel listModel=new DefaultListModel();
ResultSet rs=st.executeQuery("Select * from User_table1");
while(rs.next()) {
String name=rs.getString("Filename");
listModel.addElement(name);
System.out.println(name);
}
list.setModel(listModel);
rs.close();
st.close();
最佳答案
您有2种方法可以解决此问题:
因此,如果需要在静态上下文中调用此函数,则应使用第二种方法。
如果需要此方法来保护该类类型的任何对象的list值,则应使用第一种方法。
我支持@Andrew Thompson在第一句话中说的话。
关于java - 非静态变量列表不能从静态上下文中引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16457978/