TestDefaultTableModel--table model in java&sql


//email:[email protected]

//function:calling database in java table model

package javaSwing;


import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;


public class TestDefaultTableModel extends JFrame
{
Vector colsv=new Vector();
JTable table;
DefaultTableModel tablemodel;//Create the model of DefaultTable
public TestDefaultTableModel(){
this.setLayout(new FlowLayout());
/*
*將String對象“學號”、“姓名”、“生日”、“數學”加入向量中
*用以顯示錶頭
*/
colsv.add("stu_num");
colsv.add("name");
colsv.add("birthday");
colsv.add("math");
colsv.add("english");
tablemodel=new DefaultTableModel(new Vector(),colsv);//Make an object of table model,with the initial vector'colsv'
String str="select * from stu_info";
TestDB db=new TestDB();
ResultSet rs=db.query(str);
/*
*Put the query resultset into the vector
*/
Vector value=new Vector();
try{
while(rs.next()){
Vector vc=new Vector();
vc.add(rs.getString(1));
vc.add(rs.getString(2));
vc.add(rs.getString(3));
vc.add(rs.getString(4));
vc.add(rs.getString(5));
value.add(vc);
}
tablemodel.setDataVector(value,colsv);//Write new raws into the table,and the value is vector value
}catch(SQLException e){
//TODO Auto-generated catch block
e.printStackTrace();
}
table=new JTable(tablemodel);
this.add(new JScrollPane(table));
this.setSize(500,200);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){//Set the listener:The program is over while the window is closed
System.exit(0);
}
});
}
public static void main(String[] args) 
{
new TestDefaultTableModel();
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章