日期:2014-05-16 浏览次数:20550 次
// 取得数据库中所有表名称
public List getTables() {
List tables = new ArrayList();
Connection con = null;
ResultSet rs = null;
try {
con = DbUtils.getConnection("jwdt");
rs = con.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
while (rs.next()) { // 注意:结果集中存在表的很多信息,表名称在第 3 列
tables.add(rs.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DbUtils.close(con);
}
return tables;
}
// 根据表名称取得表所有字段
public List getColumns(String table) {
List columns = new ArrayList();
Connection con = null;
ResultSet rs = null;
try {
con = DbUtils.getConnection("jwdt");
rs = con.getMetaData().getColumns(null, null, table, null);
while (rs.next()) { // 注意:结果集中存在表的很多信息,字段名称在第 4 列
columns.add(rs.getString(4));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DbUtils.close(con);
}
return columns;
}
// 根据表名称取得表主键字段
// 取得表外键字段的操作使用 getExportedKeys(null, null, table) 方法
public String getPrimaryKeys(String table) {
String keys = "";
Connection con = null;
ResultSet rs = null;
try {
con = DbUtils.getConnection("jwdt");
rs = con.getMetaData().getPrimaryKeys(null, null, table);
while (rs.next()) {// 注意:结果集中存在表的很多信息,字段名称在第 4 列
keys += rs.getString(4) + ",";
}
if (!keys.equals("")) { // 去掉最后面的逗号
keys = keys.substring(0, keys.length() - 1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DbUtils.close(con);
}
return keys;
}