你是说DatabaseMetaData.getTables()吗,这个我知道,返回所有表public String getTableName(int column) throws SQLExceptionGets the designated column's table name. Parameters: column - the first column is 1, the second is 2, ... Returns: table name or "" if not applicable Throws: SQLException - if a database access error occurs但是我就不知道为什么每次都是"",而无法取得table name!!!!
looks like this function (ResultSetMetaData.getTableName(int i)) is not implemented in the JDBC driver
private static String getTableName(String sql){ int pos=sql.toLowerCase().indexOf("from"); if (pos>0){ String t=sql.toLowerCase().substring(pos+4).trim(); pos=t.indexOf(" "); if (pos>0) return t.substring(0,pos).trim(); else return t; }else return ""; }凑合着用吧
注意大小写,包括schema和表名。如果是Oracle,默认的情况是大写,你看看你是不是小写了。
你去看看jdbc doc,很简单的一个问题,
我建议楼主,好好看看java doc 的sql下面的东西,对你大有裨益!
会不会你的jdbc的driver有问题。
这是JDK1.4文档: public String getTableName(int column) throws SQLExceptionGets the designated column's table name. Parameters: column - the first column is 1, the second is 2, ... Returns: table name or "" if not applicable Throws: SQLException - if a database access error occursJDK1.4下面DEMO中有一例子TableExamples中就有 JDBCAdapter ,里面也用到了 public void setValueAt(Object value, int row, int column) { try { String tableName = metaData.getTableName(column+1); // Some of the drivers seem buggy, tableName should not be null. if (tableName == null) { System.out.println("Table name returned null."); } String columnName = getColumnName(column); String query = "update "+tableName+ " set "+columnName+" = "+dbRepresentation(column, value)+ " where "; // We don't have a model of the schema so we don't know the // primary keys or which columns to lock on. To demonstrate // that editing is possible, we'll just lock on everything. for(int col = 0; col<getColumnCount(); col++) { String colName = getColumnName(col); if (colName.equals("")) { continue; } if (col != 0) { query = query + " and "; } query = query + colName +" = "+ dbRepresentation(col, getValueAt(row, col)); } System.out.println(query); System.out.println("Not sending update to database"); // statement.executeQuery(query); } catch (SQLException e) { // e.printStackTrace(); System.err.println("Update failed"); } Vector dataRow = (Vector)rows.elementAt(row); dataRow.setElementAt(value, column); }上面代码第三行,就是我所说的语句 因为上面也提到了: // Some of the drivers seem buggy, tableName should not be null. 所以charbee(char) 说得对,可以是drivers有问题,但我不知道如何解决啊 ODBC-JDBC、JDBC我都用过了,得到的都是""
throws SQLExceptionGets the designated column's table name. Parameters:
column - the first column is 1, the second is 2, ...
Returns:
table name or "" if not applicable
Throws:
SQLException - if a database access error occurs但是我就不知道为什么每次都是"",而无法取得table name!!!!
String[] table_type={"TABLE"};
ResultSet result=dmd.getTables(null,null,null,table_type);
Vector tableNames=new Vector();
while(result.next()){
tableNames.add(result.getString("TABLE_NAME"));
}
int pos=sql.toLowerCase().indexOf("from");
if (pos>0){
String t=sql.toLowerCase().substring(pos+4).trim();
pos=t.indexOf(" ");
if (pos>0)
return t.substring(0,pos).trim();
else
return t;
}else
return "";
}凑合着用吧
public String getTableName(int column)
throws SQLExceptionGets the designated column's table name. Parameters:
column - the first column is 1, the second is 2, ...
Returns:
table name or "" if not applicable
Throws:
SQLException - if a database access error occursJDK1.4下面DEMO中有一例子TableExamples中就有 JDBCAdapter ,里面也用到了
public void setValueAt(Object value, int row, int column) {
try {
String tableName = metaData.getTableName(column+1);
// Some of the drivers seem buggy, tableName should not be null.
if (tableName == null) {
System.out.println("Table name returned null.");
}
String columnName = getColumnName(column);
String query =
"update "+tableName+
" set "+columnName+" = "+dbRepresentation(column, value)+
" where ";
// We don't have a model of the schema so we don't know the
// primary keys or which columns to lock on. To demonstrate
// that editing is possible, we'll just lock on everything.
for(int col = 0; col<getColumnCount(); col++) {
String colName = getColumnName(col);
if (colName.equals("")) {
continue;
}
if (col != 0) {
query = query + " and ";
}
query = query + colName +" = "+
dbRepresentation(col, getValueAt(row, col));
}
System.out.println(query);
System.out.println("Not sending update to database");
// statement.executeQuery(query);
}
catch (SQLException e) {
// e.printStackTrace();
System.err.println("Update failed");
}
Vector dataRow = (Vector)rows.elementAt(row);
dataRow.setElementAt(value, column); }上面代码第三行,就是我所说的语句
因为上面也提到了:
// Some of the drivers seem buggy, tableName should not be null.
所以charbee(char) 说得对,可以是drivers有问题,但我不知道如何解决啊
ODBC-JDBC、JDBC我都用过了,得到的都是""