📄 datatable.java
字号:
package com.worker.table;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.util.*;
import java.sql.*;
import javax.swing.table.*;
import com.tool.*;
import com.worker.db.*;
import com.worker.information.*;
import com.worker.main.*;
import com.worker.userpan.*;
/**
*
* @author Administrator
*/
public class DataTable extends DefaultTableModel { //AbstractTableModel {
private DbCon dbcon;
private ManageCommunication mC;
private ResultSet resultSet;
private ResultSetMetaData metaData;
private int numberOfRows;
private String queryList;
// initialize resultSet and obtain its meta data object;
// determine number of rows
public DataTable(String intval) {
try {
dbcon=DbCon.getinstance();
this.queryList=intval;
resultSet=dbcon.setSqlQuery(queryList);
// obtain meta data for ResultSet
metaData = resultSet.getMetaData();
// determine number of rows in ResultSet
resultSet.last(); // move to last row
numberOfRows = resultSet.getRow(); // get row number
}
// catch SQLExceptions and ClassNotFoundExceptions
catch ( Exception exception ) {
exception.printStackTrace();
}
}
// get class that represents column type
public Class getColumnClass( int column ) {
// determine Java class of column
try {
String className =
metaData.getColumnClassName( column + 1 );
// return Class object that represents className
return Class.forName( className );
}
// catch SQLExceptions and ClassNotFoundExceptions
catch ( Exception exception ) {
exception.printStackTrace();
}
// if problems occur above, assume type Object
return Object.class;
}
// get name of a particular column in ResultSet
public String getColumnName( int column ) {
// determine column name
try {
return metaData.getColumnName( column + 1 );
}
// catch SQLExceptions and print error message
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
// if problems, return empty string for column name
return "";
}
// get number of columns in ResultSet
public int getColumnCount() {
// determine number of columns
try {
return metaData.getColumnCount();
}
// catch SQLExceptions and print error message
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
// if problems occur above, return 0 for number of columns
return 0;
}
// return number of rows in ResultSet
public int getRowCount() {
return numberOfRows;
}
// obtain value in particular row and column
public Object getValueAt( int row, int column ) {
// obtain a value at specified ResultSet row and column
try {
resultSet.absolute( row + 1 );
return resultSet.getObject( column + 1 );
}
// catch SQLExceptions and print error message
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
// if problems, return empty string object
return "";
}
//waiting to rewrite according to the workerOb
public boolean isCellEditable(int row,int col) {
return false;
}
// set new database query string
public void setQuery( String query ) {
try {
this.queryList=query;
resultSet=dbcon.setSqlQuery(queryList);
// obtain meta data for ResultSet
metaData = resultSet.getMetaData();
// determine number of rows in ResultSet
resultSet.last(); // move to last row
numberOfRows = resultSet.getRow(); // get row number
// notify JTable that model has changed
mC.gettablestr().updateUI();
}
// catch SQLExceptions that occur when
// performing a new query
catch ( SQLException sqlException ) {
JOptionPane.showMessageDialog( null,
sqlException.toString(),
"Database error",
JOptionPane.ERROR_MESSAGE );
}
}
public void setValueAt(Object aValue, int row, int column) {
try {
String upD;
String test=metaData.getColumnClassName(column + 1);
if (metaData.getColumnClassName(column + 1).equals("java.lang.String"))
upD=new String(
"UPDATE workerin SET "+getColumnName(column)+" = '"+aValue.toString()+
"' WHERE workerID="+getValueAt(row,0).toString());
else
upD=new String(
"UPDATE workerin SET "+getColumnName(column)+" = "+aValue.toString()+
" WHERE workerID="+getValueAt(row,0).toString());
//refresh the interface show
dbcon.setSqlUpdate(upD);
setQuery(queryList);
//ediF.setPreWId(Integer.parseInt(getValueAt(row,0).toString()));
mC.getediteframe().upUI();
}
// catch SQLExceptions that occur when
// performing a new query
catch ( SQLException sqlException ) {
JOptionPane.showMessageDialog( null,
sqlException.toString(),
"Database error",
JOptionPane.ERROR_MESSAGE );
}
}
public void upTableUI() {
try {
resultSet=dbcon.setSqlQuery(queryList);
// obtain meta data for ResultSet
metaData = resultSet.getMetaData();
// determine number of rows in ResultSet
resultSet.last(); // move to last row
numberOfRows = resultSet.getRow(); // get row number
// notify JTable that model has changed
mC.gettablestr().updateUI();
}
// catch SQLExceptions that occur when
// performing a new query
catch ( SQLException sqlException ) {
JOptionPane.showMessageDialog( null,
sqlException.toString(),
"Database error",
JOptionPane.ERROR_MESSAGE );
}
}
public void setmanagecommunication(ManageCommunication va) {
mC=va;
}
} // end class ResultSetTableModel
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -