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

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

给你个思路,可以先将数据存放在一个集合里面。因为集合是不必定义长度的。然后在根据集合长度来定义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 = 0i<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。

//JTable你用错了吧 

private static String tableRows[] = {"编号", "名称", "规格", "数量", "原价"}

    public final static int ROW_MAX = 100 //表格最大行数

    private static String tableColunms[][] = new String[ROW_MAX][tableRows.length]

 

    public static JTable table = new JTable(tableColunms, tableRows)

    public static JScrollPane scrollpane = new JScrollPane(table)//添加滚动条


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9976443.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存