好的。我写了上面的代码,我需要从另一个类中调用它。我该怎么做?加上它给了我这个错误DefaultTableModel prodt = (DefaultTableModel) protable.getModel();
。错误是非静态变量 protable 无法从静态上下文中引用。
public static void refreshProtable() {
try {
Statement s1 = Db.connectDb().createStatement();
ResultSet rs1 = s1.executeQuery("SELECT * FROM product WHERE status='" + 0 + "'");
DefaultTableModel prodt = (DefaultTableModel) protable.getModel();
while (rs1.next()) {
Vector v1 = new Vector();
v1.add(rs1.getString("pid"));
v1.add(rs1.getString("pname"));
v1.add(rs1.getString("sp_rt"));
v1.add(rs1.getString("sp_wh"));
v1.add(rs1.getString("um"));
Statement s2 = Db.connectDb().createStatement();
ResultSet rs2 = s2.executeQuery("SELECT * FROM stock WHERE pid='" + rs1.getString("pid") + "'");
if (rs2.next()) {
v1.add(rs2.getString("qty"));
}
prodt.addRow(v1);
s2.close();
}
s1.close();
} catch (Exception e) {
e.printStackTrace();
}
}