日期:2014-05-16 浏览次数:20522 次
// 得到当前数据库下所有的表名
public void getTableNameByCon(Connection con) {
? ?try {
? ?DatabaseMetaData meta = con.getMetaData();
? ?ResultSet rs = meta.getTables(null, null, null,
? ? ?new String[] { "TABLE" });
? ?while (rs.next()) {
? ? ?System.out.println("表名:" + rs.getString(3));
? ? ?System.out.println("表所属用户名:" + rs.getString(2));
? ? ?System.out.println("------------------------------");
? ?}
? ?con.close();
? ?} catch (Exception e) {
? ?try {
? ? ?con.close();
? ?} catch (SQLException e1) {
? ? ?// TODO Auto-generated catch block
? ? ?e1.printStackTrace();
? ?}
? ?// TODO Auto-generated catch block
? ?e.printStackTrace();
? ?}
}
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
//??得到表的所有字段信息
public List<DBTableFieldStruct> getFieldList(String tableName, Connection con)
throws SQLException {
String sql = " SELECT ? * ? FROM ? " + tableName;
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ResultSetMetaData meta = rs.getMetaData();
int columnCount = meta.getColumnCount();
List<DBTableFieldStruct> result = new ArrayList<DBTableFieldStruct>();
for (int i = 0; i < columnCount; i++) {
DBTableFieldStruct field = new DBTableFieldStruct(); ? ? //javabean ? 如下
int cursor = i + 1;
field.setName(meta.getColumnName(cursor));
field.setType(meta.getColumnType(cursor));
field.setSize(meta.getColumnDisplaySize(cursor));
field.setScale(meta.getScale(cursor));
field.setIsNullable(meta.isNullable(cursor));
result.add(field);
}
return result;
}
?
?
?
?
?
public class DBTableFieldStruct {
private String name;
private int type;
private int size;
private int isNullable;
private int scale;
?
public String getName() {
return name;
}
?
public void setName(String name) {
this.name = name;
}
?
public int getType() {
return type;
}
?
public void setType(int type) {
this.type = type;