请问从数据库里读出的数据怎样写到JTable表格里
----------------解决方案--------------------------------------------------------
我真在学,学好了教你
----------------解决方案--------------------------------------------------------
用TableModel
----------------解决方案--------------------------------------------------------
谢谢各位,我弄了一个,如下
class RecordList extends JFrame implements ActionListener
{
JTable jt=null; //表格
Container c=this.getContentPane();
JButton jb1=new JButton("清除记录");
JButton jb2=new JButton("关闭");
JPanel jp=new JPanel();
BorderLayout bl=new BorderLayout();
Connection con=null;
Statement st=null;
ResultSet rs=null;
public RecordList()
{
super("排行榜");
c.setLayout(new FlowLayout());
c.setBackground(new Color(29,250,180));
jp.setLayout(bl);
String[] title={"姓名","次数"};
String[][] data={{null},{null} };
jt=new JTable(6,2);
jt.setRowHeight(30);
//jp.add(jt.getTableHeader(),BorderLayout.NORTH);
jp.add(jt,BorderLayout.CENTER);
c.add(jp);
c.add(jb1);
c.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:xx");
st = con.createStatement();
rs=st.executeQuery("select top 5 * from record order by gsum asc");
int i=0;
while(rs.next())
{
i++;
String str1=rs.getString(1);
String str2=rs.getString(2);
jt.setValueAt(str1,i-1,0);
jt.setValueAt(str2,i-1,1);
}
st.close();
st = null;
con.close();
con = null;
}catch(Exception ex) { ex.printStackTrace();}
this.setSize(220,280);
this.setLocation(200,200);
this.setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(jb1))
{
...
}
----------------解决方案--------------------------------------------------------
我也正在学这个,看了楼上的代码,明白了不少,谢了.
----------------解决方案--------------------------------------------------------
final String[] a_String={"图书类别","借出期限"};
a_Vector=new Vector();
a_AbstractTableModel=new AbstractTableModel(){//创建一个动态数组
public int getColumnCount()
{
return a_String.length;
}
public int getRowCount()
{
return a_Vector.size();
}
public String getColumnName(int col)
{
return a_String[col];
}
public void setValueAt(Object value,int row,int column)
{
}
public Object getValueAt(int row, int col)
{
if(!a_Vector.isEmpty())
{
return((Vector)a_Vector.elementAt(row)).elementAt(col);
}
else
return null;
}
};
a_JTable=new JTable(a_AbstractTableModel);
a_JTable.addMouseListener(new j_Actin3());
a_JTable.setToolTipText("当你孤单你会想起我!");//设置帮助提示
a_JTable.setAutoResizeMode(0);
a_JTable.setCellSelectionEnabled(false);//设置单元格选择方式
a_JTable.setShowVerticalLines(true);//设置是否显示单元格间的分割线
a_JTable.setShowHorizontalLines(true);
a_JTable.setAutoResizeMode(1); // 设置表格为自动排列方式
a_JScrollPane=new JScrollPane(a_JTable);
while(rs.next())
{
Vector a_Vector1=new Vector();
a_Vector1.addElement(rs.getString(1));
a_Vector1.addElement(rs.getString(2));
a_Vector.addElement(a_Vector1);
}
----------------解决方案--------------------------------------------------------