📄 jdbcadapter.java
字号:
public DatabaseMetaData getDBMetaData() {
return dbMD;
}
public String [] getPrimaryKeys(String tableName) {
ArrayList kn = new ArrayList(16);
int [] ks = new int[8];
int ncomp=0;
ResultSet rs = null;
String schema;
try {
// Debug.println("JDBCAdapter: getPrimaryStrings tableName:" + tableName);
int indexSchema = tableName.indexOf('.');
if (indexSchema >= 0) schema = tableName.substring(0, indexSchema);
else schema = schemaLogin;
dbMD = conn.getMetaData();
rs = dbMD.getPrimaryKeys(catalog, schema, tableName.substring(indexSchema+1));
while (rs.next()) {
try {
kn.add(rs.getString("COLUMN_NAME"));
ks[ncomp] = rs.getInt("KEY_SEQ") - 1;
ncomp++;
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: getPrimaryKeys rs.getResults SQLException");
SQLExceptionHandler.prtSQLException(ex);
}
}
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: dbMD.getPrimaryKeys SQLException");
SQLExceptionHandler.prtSQLException(ex);
}
finally {
try {
rs.close();
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: getPrimaryKeys rs.close SQLException");
SQLExceptionHandler.prtSQLException(ex);
}
}
String[] keyNames = new String[kn.size()];
// Debug.println("JDBCAdapter: ncomp:" + ncomp + " kn.size:" + kn.size());
int j;
for (int i = 0; i < ncomp; i++) {
j = ks[i];
keyNames[i] = (String) kn.get(j);
}
return keyNames;
}
public String [] getTableNames(String schema) {
String[] tableNames = null;
ArrayList vtr = new ArrayList(128);
try {
dbMD = conn.getMetaData();
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: getTableNames getMetaData SQLException");
SQLExceptionHandler.prtSQLException(ex);
}
vtr.addAll(getMetaDataTableNames(DEFAULT_SCHEMA));
// vtr.addAll(getMetaDataTableNames(null));
vtr.addAll(getMetaDataTableNames(schema));
tableNames = new String[vtr.size()];
vtr.toArray(tableNames);
return tableNames;
}
private Collection getMetaDataTableNames(String schema) {
String [] types = {"TABLE"}; // types[0] = "TABLE";
ResultSet rs = null;
ArrayList vtr = new ArrayList(128);
try {
rs = dbMD.getTables(catalog, schema, "%", types);
while (rs.next()) {
try {
// String tmpStr = rs.getString("TABLE_NAME");
// Debug.println("result table:" + tmpStr);
// vtr.add(tmpStr);
vtr.add(rs.getString("TABLE_NAME"));
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: getTableNames rs.getString SQLException");
SQLExceptionHandler.prtSQLException(ex);
}
}
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: getTableNames dbMD.getTables SQLException");
SQLExceptionHandler.prtSQLException(ex);
}
finally {
try {
rs.close();
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: getTableNames rs.close SQLException");
SQLExceptionHandler.prtSQLException(ex);
}
}
return vtr;
}
public ResultSet getColumnMetaData(String tableName){
ResultSet rs = null;
String schema;
try {
int indexSchema = tableName.indexOf('.');
if (indexSchema >= 0) schema = tableName.substring(0, indexSchema);
else schema = schemaLogin;
dbMD = conn.getMetaData();
rs = dbMD.getColumns(catalog, schema, tableName.substring(indexSchema+1), null);
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: getColumnMetaData dbMD.getColumns SQLException");
SQLExceptionHandler.prtSQLException(ex);
}
return rs;
}
public String [] getColumnNames(String tableName) {
String[] colNames = null;
ArrayList tn = new ArrayList(64);
ResultSet rs = null;
String schema;
try {
int indexSchema = tableName.indexOf('.');
if (indexSchema >= 0) schema = tableName.substring(0, indexSchema);
else schema = schemaLogin;
dbMD = conn.getMetaData();
rs = dbMD.getColumns(catalog, null, tableName.substring(indexSchema+1), null);
while (rs.next()){
try {
tn.add(rs.getString("COLUMN_NAME"));
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: getColumnNames rs.getString SQLException");
SQLExceptionHandler.prtSQLException(ex);
}
}
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: getColumnNames dbMD.getColumns SQLException");
SQLExceptionHandler.prtSQLException(ex);
}
finally {
try {
rs.close();
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: getColumnNames rs.close SQLException");
SQLExceptionHandler.prtSQLException(ex);
}
}
colNames = new String[tn.size()];
tn.toArray(colNames);
return colNames;
}
public int getColumnType(int icol) {
if (icol >= 0 && icol <= colTypes.length) return colTypes[icol];
else return -1;
}
public int getColumnScale(int icol) {
if (icol >= 0 && icol <= colTypes.length) return colScale[icol];
else return -1;
}
public ColumnData [] getColumnData(String tableName) {
String schema;
String tablestr = tableName;
int indexSchema = tablestr.indexOf('.');
if (indexSchema >= 0) {
schema = tableName.substring(0, indexSchema);
tablestr = tableName.substring(indexSchema+1);
}
else schema = schemaLogin;
// Debug.println("JDBCAdapter: getColumnData dbMD schema:" + schema + " table:" + tablestr);
return ColumnData.getColumnData(conn, schema.toUpperCase(), tablestr.toUpperCase());
}
public boolean isColumnAKey(int colIndex) {
if (colIndex < 0 || colIndex > colAKey.length) return false;
return colAKey[colIndex];
}
public boolean isColumnNullable(int colIndex) {
if (columnDataArray == null) return false;
if (colIndex < 0 || colIndex > columnDataArray.length || columnDataArray.length == 0) return false;
return columnDataArray[colIndex].isNullable;
}
public int getQueryTableCount() {
if (queryTables == null) return 0;
return queryTables.length;
}
public String getQueryTableName(int index) {
if (index < 0 || index > queryTables.length) return "";
return queryTables[index];
}
public int getKeyColumnCount() {
return colAKey.length;
}
public void setInsertedRowIndex(int index) {
if (index > getRowCount()) return;
insertedRowIndex = index;
}
public int getInsertedRowIndex() {
return insertedRowIndex;
}
public boolean isInsertable() {
return insertDBflag;
}
public void disableInsert() {
insertDBflag = false;
}
public void enableInsert() {
insertDBflag = true;
}
public boolean isUpdatable() {
return updateDBflag;
}
public void disableUpdate() {
updateDBflag = false;
}
public void enableUpdate() {
updateDBflag = true;
}
public int getUpdateCount() {
return updateCount;
}
public ResultSet getResultSet() {
return resultSet;
}
public int getTableKeyColumnIndex(String colName) {
if (colName == null) return -1;
for (int i = 0 ; i < colNames.length; i++) {
if (colName.trim().equals(colNames[i]) ) return i;
}
return -1;
}
// End of added methods - AWW
//////////////////////////////////////////////////////////////////////////
//
// Implementation of the TableModel Interface
//
//////////////////////////////////////////////////////////////////////////
public String getColumnName(int colIndex) {
if (colNames == null) return "";
if (colIndex < 0 || colIndex > colNames.length) return "";
return colNames[colIndex];
}
public int findColumn(String name) {
for (int i = 0; i < colNames.length; i++) {
if (colNames[i].equals(name)) return i;
}
return -1;
}
public Class getColumnClass(int colIndex) {
Class rval = null;
if (columnDataArray == null) columnDataArray = getColumnData(colTbls[colIndex]);
// Debug.println("JDBCAdapter: getColumnClass table:" + colTbls[colIndex] + " columnDataArray.length:" + columnDataArray.length);
for ( int i=0 ; i < columnDataArray.length; i++) {
if (columnDataArray[i].columnName.equals(colNames[colIndex]) ) {
String type = columnDataArray[i].typeName;
if (type.equals("VARCHAR2")) {
rval = String.class;
}
else if (type.equals("NUMBER")) {
if (columnDataArray[i].decimalDigits == 0) rval = Integer.class; // colScale[colIndex];
else rval = Double.class;
}
else if (type.equals("FLOAT")) {
rval = Double.class;
}
else if (type.equals("DATE")) {
rval = java.sql.Date.class;
// Debug.println("JDBCAdapter DATE: col:" + colIndex + " name:" + colNames[colIndex] + " className:" + type + " numberType:" + columnDataArray[i].dataType);
}
else if (type.equals("TIMESTAMP")) {
rval = java.sql.Timestamp.class;
// Debug.println("JDBCAdapter TIMESTAMP: col:" + colIndex + " name:" + colNames[colIndex] + " className:" + type + " numberType:" + columnDataArray[i].dataType);
}
// Debug.println("JDBCAdapter: col:" + colIndex + " name:" + colNames[colIndex] +
// " className:" + type + " numberType:" + columnDataArray[i].dataType);
break;
}
}
if (rval == null) {
// Debug.println("JDBCAdapter: getColumnClass: DEBUG Default Object.class type for col: " + colIndex + " colName:" + colNames[colIndex]);
// Debug.println("JDBCAdapter: getColumnClass: DEBUG columnDataArray.length:" + columnDataArray.length);
if (columnDataArray.length > 0) Debug.println("JDBCAdapter: getColumnClass: DEBUG class not found for cda:\n" +
columnDataArray[colIndex].toString());
rval = Object.class;
}
return rval;
}
public boolean isCellEditable(int rowIndex, int colIndex) {
if (insertDBflag && insertedRowIndex == rowIndex) {
return true;
}
else if (insertDBflag) return false;
else if (! updateDBflag) return false;
// cell editing based on primary keys discrimination;
boolean retVal = true;
try {
String tableName = rsMD.getTableName(colIndex+1);
if (tableName == null) {
System.out.println("JDBAdapter isCellEditable: Table name returned null.");
}
if (queryTables.length == 1) tableName = colTbls[colIndex];
// Debug.println("JDBCAdapter: isCellEditable at getPrimaryKeys table:" + tableName);
if (tableName.toUpperCase().indexOf("ASSOC") >= 0) return true;
String [] pkeys = getPrimaryKeys(tableName);
for (int ikey = 0; ikey < pkeys.length; ikey++) {
// Debug.println("JDBCAdapter: irow:" + rowIndex + " column:" + colIndex + " ikey:" + ikey + " pkey[ikey]:" + pkeys[ikey]);
// Debug.println("JDBCAdapter: findcolumn:" + findColumn(pkeys[ikey]));
if (findColumn(pkeys[ikey]) == colIndex) {
retVal = false;
break;
}
}
}
catch (SQLException ex) {
System.err.println("JDBCAdapter: isCellEditable rdMD.getTableName SQLException");
SQLExceptionHandler.prtSQLException(ex);
retVal = false;
}
// Debug.println("JDBCAdapter: isCellEditable:" + retVal);
return retVal;
}
public int getColumnCount() {
return colNames.length;
}
// Data cell methods
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -