公共类Tablepanel扩展了JPanel {
int row,col;
JTable table;
String num;
Tablepanel()
{
setLayout(new BorderLayout());
try
{
Database d=new Database();
ResultSet rs = null;
Connection con = d.con;
Statement state=null;
state=con.createStatement();
String insertsql="select firstname,lastname,phonenumber from contacts";
rs=state.executeQuery(insertsql);
java.sql.ResultSetMetaData rsm=rs.getMetaData();
int numberofcolums=rsm.getColumnCount();
Vector<String> colums=new Vector<String>(numberofcolums);
for(int i=1;i<=numberofcolums;i++)
{
colums.add(rsm.getColumnName(i));
}
Vector<Vector<String>> data=new Vector<Vector<String>>();
Vector<String> rowdata=new Vector<String>();
while(rs.next())
{
for(int i=1;i<=numberofcolums;i++)
{
rowdata.add(rs.getString(i));
}
data.add(rowdata);
}
table=new JTable(data, colums);
table.setSize(200, 200);
JScrollPane sp=new JScrollPane(table);
add(sp,BorderLayout.NORTH);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
row=table.getSelectedRow();
col=table.getSelectedColumn();
String clcik=(table.getModel().getValueAt(row,col).toString());
num=clcik;
String name=setname(num);
}
});
System.out.println(num);
}catch(Exception ex)
{
System.out.println(ex);
}
}
String setname(String num)
{
return this.num=num;
}
String getname()
{
return this.num;
}
嗨,我正在进行项目电话应用,但是当我从数据库中检索数据时,JTable中存在问题Jtable总是给出相同的值,例如
wahab massod 02158956
wahab masood 02158956
wahab massod 02158956
wahab masood 02158956
请帮帮忙?