The home page defines a public model class,CodeAs follows:
/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/
Package tablemodel;
Import java. SQL. resultset;
Import java. util. Collections list;
Import javax. Swing. Table. abstracttablemodel;
Import zhinengkaiguanjiankong. Comm. loggerutil;
/**
* Database and jtable Data Binding record data storage objects
* @ Author http://www.my400800.cn
*/
Public class tablemodel extends acttablemodel {
Private int column = 4;
Private string [] columnname = NULL;
Private collections list <string []> resultset = NULL;
Public tablemodel (){
}
/** Creates a new instance of tablemodel */
Public tablemodel (resultset RS, string [] dscolumnname, string [] columnname) throws exception {
If (dscolumnname. length! = Columnname. Length ){
Throw new exception ("the specified jtable column is inconsistent with the specified database column and data binding is not allowed ");
}
This. columnname = columnname;
Column = columnname. length;
Resultset = new pipeline list <string []> ();
Try {
While (Rs. Next ()){
String [] ROW = new string [column];
For (INT I = 0; I <column; I ++ ){
Row [I] = Rs. getstring (dscolumnname [I]);
}
Resultset. Add (ROW );
}
} Catch (exception e ){
Loggerutil. writeerrlog (E );
Throw E;
// System. Out. println ("Err ");
}
}
/**
* Retrieve the total number of records
* @ Return total number of records
*/
Public int getrowcount (){
Return resultset. Size ();
}
/**
* Retrieve the total number of Columns
* @ Return total number of Columns
*/
Public int getcolumncount (){
Return column;
}
/**
* Get the data of the specified column in the specified row
* @ Param rowindex row
* @ Param columnindex Column
* @ Return
*/
Public object getvalueat (INT rowindex, int columnindex ){
String [] ROW = resultset. Get (rowindex );
Return row [columnindex];
}
/**
* Get the specified column name
* @ Param I specifies the column
* @ Return specifies the column name to return
*/
Public String getcolumnname (int I ){
Return columnname [I];
}
}
Model Implementation class. In this column, set the jtable column and database Field names.
/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/
Package tablemodel;
Import java. SQL. resultset;
/**
*
* @ Author 173.114
*/
Public class zhinengkaiguanjiankongviewtablemodel extends tablemodel {
Public zhinengkaiguanjiankongviewtablemodel (resultset RS) throws exception {
/**
* Record the RS database Field names listed in jtable
*/
Super (RS, new string [] {"ID", "i64val", "timeval", "val14", "val22", "adv", "val24 ", "val13 "},
New String [] {"ID", "name", "time", "status", "field", "attribute", "Remarks", "Switch ID "});
}
}
Data Binding code
Tablemodel = new zhinengkaiguanjiankongviewtablemodel (database obtained resultset );
Jtbl_datalist.setmodel (tablemodel );
The above code binds the data to the jtable through the model.