主要的几个点:
1、确定表有多少行。
2、按表的行数画表格。
给你个示列
import java.awt.BorderLayout
import java.awt.Color
import java.awt.Font
import java.beans.Statement
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException
import java.util.Vector
给你个思路,可以先将数据存放在一个集合里面。因为集合是不必定义长度的。然后在根据集合长度来定义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。
在面板上用table显示数据库的中的信息
代码如下:
try{
Database db = new Database()
db.conn()
Statement stmt = db.conn.createStatement()
ResultSet rset = stmt.executeQuery("SELECT * FROM goods")//前面的为数据库部分(每个人视情况而定)
DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel()
dtm.setRowCount(0)
while (rset.next()) {
Object[] row = {
rset.getString(1),
rset.getDouble(2),
rset.getDouble(3),//这三个为表各个字段的值,从数据库中得到的
}
dtm.addRow(row)
}
rset.close()
stmt.close()
db.closeConn()
} catch (ClassNotFoundException ex) {
Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex)
} catch (SQLException ex) {
Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)