import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.table.*;
public class Teldirectory {
public Teldirectory () {
JFrame f=new JFrame();
TelTable tt=new TelTable();
JTable table=new JTable(tt);
table.getPreferredScrollableViewportSize(new Dimension(550,30));
JScrollPane e=new JScrollPane(table);
f.getContentPane().add(s,BorderLayout.CENTER);
f.setTitle("电话簿");
f.pack();
f.setVisible(true);
}
}
class TelTable extends AbstractTableModel{ //tableModel
DataBaseManager db=new DataBaseManager();
ResultSet rs;
ResultSetMetaData rsmd;
String strSql="select * from communication";
try{
rs=db.getResult(strSql);
rsmd=rs.getMetaData();
int dbrow=rsmd.getColumnCount(); //获取列数
int dbcol=0; //获取行数
while(rs.next()){ //不知道用什么方法统计行数
dbcol++;
}
System.out.println("cow:"+dbrow);
System.out.println("col:"+dbcol);
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
String[] name={"ID","姓名","手机","电话","QQ"};
String[][] data=new String[dbrow][dbcol];
rs.first();
try{
int i=1;
while(rs.next()){
for(int j=0;j<name.length;j++)
data[i][j]=rs.getString(j);
i++;
}
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
public int getColumnCount() {
return name.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return name[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
代码很多错误,我实在想不通是怎么回事,要做的事情是做一个电话本,从数据库提出数据,用表格展示,可是看了书和API,找不到想要的方法,请大家帮忙看看应该怎么修改,麻烦大家帮忙一下~