//set up a connection
Connection connection = ...//set up a DatabaseMetaData
DatabaseMetaData dbData = connection.getMetaData();//get ALL tables from this database
TreeSet tableNames = new TreeSet();
ResultSet tableSet = null;String[] types = {"TABLE", "VIEW", "ALIAS", "SYNONYM"};
String userName = dbData.supportsSchemasInTableDefinitions() ? dbData.getUserName() : null;
tableSet = dbData.getTables(null, userName, null, types);
//return table name setwhile (tableSet.next()) {
try {
String tableName = tableSet.getString("TABLE_NAME"); tableName = (tableName == null) ? null : tableName.toUpperCase();
String tableType = tableSet.getString("TABLE_TYPE"); tableType = (tableType == null) ? null : tableType.toUpperCase();
//only allow certain table types
if (tableType != null && !"TABLE".equals(tableType) && !"VIEW".equals(tableType) && !"ALIAS".equals(tableType) && !"SYNONYM".equals(tableType))
continue;
tableNames.add(tableName);
} catch (SQLException sqle) {
//...
}
}
Connection connection = ...//set up a DatabaseMetaData
DatabaseMetaData dbData = connection.getMetaData();//get ALL tables from this database
TreeSet tableNames = new TreeSet();
ResultSet tableSet = null;String[] types = {"TABLE", "VIEW", "ALIAS", "SYNONYM"};
String userName = dbData.supportsSchemasInTableDefinitions() ? dbData.getUserName() : null;
tableSet = dbData.getTables(null, userName, null, types);
//return table name setwhile (tableSet.next()) {
try {
String tableName = tableSet.getString("TABLE_NAME"); tableName = (tableName == null) ? null : tableName.toUpperCase();
String tableType = tableSet.getString("TABLE_TYPE"); tableType = (tableType == null) ? null : tableType.toUpperCase();
//only allow certain table types
if (tableType != null && !"TABLE".equals(tableType) && !"VIEW".equals(tableType) && !"ALIAS".equals(tableType) && !"SYNONYM".equals(tableType))
continue;
tableNames.add(tableName);
} catch (SQLException sqle) {
//...
}
}
ResultSet rs = md.getTable(null, null, null, new String [] {"TABLE"});
这个结果集包含了数据库关于所有库表的信息。表名是第三个域,
用
while (rs.next())
{
tableName[i] = rs.getString(3);
i++;
}
可以得到所有的表名,这是《核心技术卷II》里关于获取表名的方法,应该是跨数据库的。
Vector vector = new Vector();
try{
m_sta = m_con.createStatement();
m_rs = m_sta.executeQuery("select * from tab where tabtype ='TABLE'");
while(m_rs.next()){
vector.addElement("(表)"+m_rs.getString(1));
}
tname = vector.elements();
}
catch(Exception e){
e.printStackTrace();
}
return tname;
}
{
try{
rs = stmt.executeQuery("SELECT * FROM sm_employee");
}catch(SQLException se){
System.out.println("Execute Query failed!");
se.printStackTrace();}
return rs;
}当程序调用此方法时,将获得一个ResultSet对象,如何对这个特定的ResultSet对象操作,从而获得表名 sm_employee?当然,此sql语句很简单,若是多表查询时,通过引入列的索引值也应该能够获得特定的表名。
String tableName = rsmd.getTableName(int column);
这样就可以了!
我想请问,你做过试验吗?如果做过,使用的是哪一种驱动,可否发一份给我?
qu_shihong's solution is right , if your jdbc driver support jdbc api 2.0 , it works fine.
First , I'm sorry to give a error message without any experiments.
Second , a bad news: It said that in JDBC Tutorial this method is not widely supported by DBMSs and many DB returns "". And it doesn't work with Oracle thin driver ( I tested ).
I can not help anymore , I think you need to find another solution.