给你个思路,可以先将数据存放在一个集合里面。因为集合是不必定义长度的。然后在根据集合长度来定义OBJ数组。给你贴段代码。希望对你有帮助。
public static Object[][] slectAll(String SQL){
Object[][] obj2 = null;
ArrayList arr = new ArrayList();
Connection conn = DBAccess.getConn();
String sql = SQL;
try {
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
GoodsSell rl = new GoodsSell();
rl.setSellGoods_Id(rs.getString(1));
rl.setSellGoods_Name(rs.getString(2));
rl.setSellGoods_Price(Double.parseDouble(rs.getString(3)));
rl.setSellGoods_Time(rs.getString(4));
rl.setCustomer_Name(rs.getString(5));
rl.setSellGoods_SalesMan(rs.getString(6));
rl.setSell_PaymentWay(rs.getString(7));
rl.setSell_Remark(rs.getString(8));
arr.add(rl);
}
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
obj2 = new Object[arr.size()][8];
for(int i = 0;i<arr.size();i++){
obj2[i][0] = ((GoodsSell)arr.get(i)).getSellGoods_Id();
obj2[i][1] = ((GoodsSell)arr.get(i)).getSellGoods_Name();
obj2[i][2] = ((GoodsSell)arr.get(i)).getSellGoods_Price();
obj2[i][3] = ((GoodsSell)arr.get(i)).getSellGoods_Time();
obj2[i][4] = ((GoodsSell)arr.get(i)).getCustomer_Name();
obj2[i][5] = ((GoodsSell)arr.get(i)).getSellGoods_SalesMan();
obj2[i][6] = ((GoodsSell)arr.get(i)).getSell_PaymentWay();
obj2[i][7] = ((GoodsSell)arr.get(i)).getSell_Remark();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBAccess.closeConn();
}
return obj2;
} 但是现在jf.getContentPane().add(table); table报错画红线!Exception in thread "main" java.lang.Error: Unresolved compilation problem:
table cannot be resolved 一般先将table放在jscrollpane里面 然后将jscrollpane放在jpanel 里面 最后将jpanel放在jframe。
温馨提示:答案为网友推荐,仅供参考