Package SiC;
Import java. SQL. date;
Import java. SQL. resultset;
Import java. SQL. resultsetmetadata;
Import java. SQL. sqlexception;
Import java. SQL. timestamp;
Import javax. Swing. event. tablemodellistener;
Import javax. Swing. Table. abstracttablemodel;
Import javax. Swing. Table. tablemodel;
Public class datamodel extends acttablemodel {
Static resultset res = NULL;
Public datamodel (string S, int I)
{
// If (S = NULL) {S = "";};
String SQL = "";
Dbconnected. Connect ();
If (I = 1) {SQL = "select ID, username, password, priv_id, birthday, gender, tele_num, user_desc from t_user where username like '% "+ S +" % '";
};
If (I = 2) {SQL = "select * from" + S ;};
// Search. setgender ();
System. Out. println (SQL );
Res = dbconnected. getmsg (SQL );
Try {
Resultsetmetadata rsmd = res. getmetadata ();
} Catch (final sqlexception e ){
// Todo automatically generates catch Blocks
E. printstacktrace ();
}
}
Public int getcolumncount (){
Int COUNT = 0;
Try {
Final resultsetmetadata rsmd = res. getmetadata ();
Count = rsmd. getcolumncount ();
} Catch (final sqlexception e ){
// Todo automatically generates catch Blocks
E. printstacktrace ();
}
// System. Out. println ("Count =" + count );
Return count;
}
Public int getrowcount (){
Int rows = 0;
Try {
Res. Last ();
Rows = res. getrow ();
} Catch (final sqlexception e ){
// Todo automatically generates catch Blocks
E. printstacktrace ();
}
// System. Out. println ("rows =" + rows );
Return rows;
}
Public object getvalueat (final int rowindex, final int columnindex) {// display on the surface res. getstring (columnindex + 1). Equals ("1 ")
Object value = NULL; // shared value
Try {
Res. Absolute (rowindex + 1 );
Final resultsetmetadata rsmd = res. getmetadata ();
Value = res. GetObject (columnindex + 1 );
// Value = res. GetObject (columnindex + 1 );
If (rsmd. getcolumnname (columnindex + 1). Equals ("gender") {// The column name is gender
// Value = res. GetObject (columnindex + 1); // obtain the column Value
// If (value. Equals (null) {value = res. GetObject (columnindex + 1 );}
// Else
// If (res. getstring (columnindex + 1). Equals ("")){
// Value = res. GetObject (columnindex + 1 );
//
//}
// If (value! = NULL ){
//
//
// If (res. getstring (columnindex + 1). Equals ("1 "))
//{
// Value = "male ";
//}
// Else if (res. getstring (columnindex + 1). Equals ("2 ")){
//
// Value = "female ";
//}
//
//
//
//}
//
If (value = NULL) {value = res. GetObject (columnindex + 1 );}
Else if (res. getstring (columnindex + 1). Equals ("1 ")){
Value = "male ";
}
Else if (res. getstring (columnindex + 1). Equals ("2 ")){
Value = "female ";
}
}
If (rsmd. getcolumnname (columnindex + 1). Equals ("priv_id "))
{
If (value = NULL) {value = res. GetObject (columnindex + 1 );}
Else if (res. getstring (columnindex + 1). Equals ("1 ")){
Value = "sys ";
}
Else if (res. getstring (columnindex + 1). Equals ("2 ")){
Value = "system ";
}
Else if (res. getstring (columnindex + 1). Equals ("3 ")){
Value = "Manger ";
}
Else if (res. getstring (columnindex + 1). Equals ("4 ")){
Value = "guest ";
}
Else {}
}
// If (rsmd. getcolumnname (columnindex + 1). Equals ("Birthday ")){
/// Value = res. GetObject (columnindex + 1). tostring (). substring (0, 10 );
//}
If (value instanceof timestamp ){
Value = res. getdate (columnindex + 1 );
} // Date display
} Catch (final sqlexception e ){
// Todo automatically generates catch Blocks
E. printstacktrace ();
}
Return value;
}
Public String getcolumnname (final int column ){
String result = "";
Try {
System. Out. println ("column =" + column );
Final resultsetmetadata rsmd = res. getmetadata ();
Result = rsmd. getcolumnname (column + 1 );
} Catch (final sqlexception e ){
// Todo logs? When else catch?
E. printstacktrace ();
}
Return result;
}
Public Boolean iscelleditable (final int rowindex, final int columnindex ){
Return true;
}
Public void setvalueat (Object avalue, final int rowindex, final int columnindex) {// write it into the database
Try {
Res. Absolute (rowindex + 1 );
Resultsetmetadata rsmd = res. getmetadata ();
// If (rsmd. getcolumnname (columnindex + 1). Equals ("Birthday "))
//{
//
//
// Avalue = avalue. tostring (); // + "00:00:00. 0 ";
/// Yyyy-mm-dd hh: mm: Ss. fffffffff
/// System. Out. println (avalue );
//
/// Value = res. GetObject (columnindex + 1). tostring (). substring (0, 10 );
//
// Avalue = date. valueof (string) avalue );
//
//
//}
//
// If (avalue instanceof timestamp ){
//
// Avalue = res. getdate (columnindex + 1 );
//
//
//}
// If (rsmd. getcolumnname (columnindex + 1). Equals ("Birthday "))
//{
//
// Avalue = date. valueof (string) avalue );
//
//}
//
//
// If (rsmd. getcolumnname (columnindex + 1). Equals ("gender ")){
//
//
If (avalue. Equals ("male") {avalue = "1 ";
}
If (avalue. Equals ("female") {avalue = "2 ";
}
// Else {} error because else changes all values
//
//
//}
If (rsmd. getcolumnname (columnindex + 1). Equals ("priv_id ")){
If (avalue. Equals ("sys") {avalue = "1 ";
}
Else if (avalue. Equals ("system") {avalue = "2 ";
}
Else if (avalue. Equals ("Manger") {avalue = "3 ";
}
Else if (avalue. Equals ("guest") {avalue = "4 ";
}
}
If (rsmd. getcolumnname (columnindex + 1). Equals ("ID ")){
}
If (rsmd. getcolumnname (columnindex + 1). Equals ("Birthday "))
{
Avalue = date. valueof (string) avalue );
}
Res. updateobject (columnindex + 1, avalue );
Res. updaterow ();
} Catch (final sqlexception e ){
// Todo logs? When else catch?
E. printstacktrace ();
}
}
Public void deleterows (final int selectrows [])
{
// For (int row = 0; row <= selectrows. Length-1; row ++)
//
//{
// Try {
//
// System. Out. println ("");
//
// Res. Absolute (selectrows [row] + 1 );
//
// System. Out. println ("");
// Res. deleterow ();
//
// System. Out. println (row + "2 ");
//
//} Catch (sqlexception e ){
/// Todo auto-generated Catch Block
// E. printstacktrace ();
//}
//
//
//
//
//}
//
//
//
//
//
//
//
//}
For (int row = selectrows. Length-1; row> = 0; row --) {// if you delete multiple rows in reverse mode, the deletion will not occur because you delete one
// The following will go up. So when you delete multiple objects at the same time, if you delete three objects at the same time, the first one will be deleted. Then, the two objects will follow up and cause deletion errors.
// If you delete the data in the reverse direction, deleting the third one will only make up the data that has not been deleted.
// System. Out. println ("ROW =" + row );
Try {
Res. Absolute (selectrows [row] + 1 );
Res. deleterow ();
} Catch (final sqlexception e ){
E. printstacktrace ();
}
}
}
}