当前位置: 代码迷 >> J2SE >> JTable 有关问题 求助
  详细解决方案

JTable 有关问题 求助

热度:676   发布时间:2016-04-24 13:42:17.0
JTable 问题 求助
我想将我数据库里的表格作为JTable的形式显示在面板上,请问怎么做啊,数据库用的是access,头都想疼了。。。在线等

------解决方案--------------------
这不是很简单吗,从数据表里取出结果集,然后放到JTable里。做过数据库程序的人都应该会做。头疼是不是jdbc驱动没有?
------解决方案--------------------
先谢谢楼上的,驱动程序我倒是会,但就是还没做过数据库驱动程序那种,这是第一次,所以感觉不会。你看我下面的代码
try
{
set1=sql.executeQuery( "select * from InMoney ");

rsd=set1.getMetaData();
int i=0;
for( i=0;i <rsd.getColumnCount();i++)
{
col.add(rsd.getColumnName(i));
}
if(set1.next())
{

for(i=1;i <=rsd.getColumnCount();i++)
{
dat.addElement(set1.getString(i));
}



}

}
catch(SQLException sql)
{
sql.printStackTrace();
}
table=new JTable(dat,col);


pane1.add(table);dat,col都是Vector矢量,我这显示驱动程序管理器,无效的描述器索引是怎么回事
------解决方案--------------------
不知道是不是你要得!

public void mCon(){
Connection con=null;
Statement st=null;
ResultSet rs=null;

Myconntection my=new Myconntection();
con=my.getconn();
try {
st = con.createStatement();
rs=st.executeQuery( "select * from books ");
Vector vhead=new Vector(); //标题
vhead.add( "书ID ");
vhead.add( "书名 ");
vhead.add( "作者 ");
vhead.add( "价格 ");
vhead.add( "备注 ");

Vector vdata=new Vector();
Vector ve=null
while(rs.next()){
ve=new Vector();
ve.add( " "+rs.getInt(1));
ve.add(rs.getString(2));
ve.add(rs.getString(3));
ve.add( " "+rs.getFloat(4));
ve.add(rs.getString(5));
vdata.add(ve);
}
DefaultTableModel dtm=new DefaultTableModel(vdata,vhead);
jTable1.setModel(dtm);

} catch (SQLException ex) {
ex.printStackTrace();
}finally{
try {
rs.close();
st.close();
con.close();
} catch (SQLException ex1) {
ex1.printStackTrace();
}

}
}
  相关解决方案