java中怎么把数据库中数据查询出来在窗体中显示

如题所述

给你个思路,可以先将数据存放在一个集合里面。因为集合是不必定义长度的。然后在根据集合长度来定义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。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-12-08

    确定你所谓的窗体是页面(jsp)还是桌面窗口(swing)

    获取数据是否使用框架或直接用jdbc

    剩下的就是百度一下,一堆一堆的。

jdbc:JDBC数据库连接 or JAVA使用JDBC连接MySQL数据库(已经实现读出来并打印到控制台)

JSP显示:使用<%%>中直接System.out.println();    or  使用jstl的foreach输出JSP 基础之 JSTL <c:forEach>用法

第2个回答  2016-11-05
java有一套连接数据库的接口规范叫做JDBC,你需要下载对应数据库的驱动包才能使用本回答被网友采纳
第3个回答  2017-12-08
直接把数据查询好然后传给Jtable 完毕。
第4个回答  2017-12-08
你说的窗体值什么,是类似于jsp的页面,如果是这样,你可能需要使用js+jsp用于前段的简单展示
相似回答