纳速健身
标题:
mysql检索表的所有列名
[打印本页]
作者:
yaokai
时间:
2009-8-17 10:32
标题:
mysql检索表的所有列名
1.show full fields from te_comp
2.show COLUMNS from table(表名)
3/**
* 取得某表下的所有字段信息
*
* @param table
* @return
*/
public List getColumnInfoesInTable(String table){
List retval = new ArrayList();
try {
Connection connection = getConnection();
// Create a result set
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM " + table);
// Get result set meta data
ResultSetMetaData rsmd = rs.getMetaData();
int numColumns = rsmd.getColumnCount();
// Get the column names; column indices start from 1
for (int i = 1; i < numColumns + 1; i++) {
ColumnInfoBean columnInfoBean = new ColumnInfoBean();
// 字段名
columnInfoBean.setName(rsmd.getColumnName(i));
// 字段类型
columnInfoBean.setTypeName(rsmd.getColumnTypeName(i));
// 字段类型对应的java类名
columnInfoBean.setClassName(rsmd.getColumnClassName(i));
// 显示的长度
columnInfoBean.setDisplaySize(String.valueOf(rsmd
.getColumnDisplaySize(i)));
// Precision
columnInfoBean.setPrecision(String
.valueOf(rsmd.getPrecision(i)));
// Scale
columnInfoBean.setScale(String.valueOf(rsmd.getScale(i)));
retval.add(columnInfoBean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return retval;
}
建议将 ResultSet rs = stmt.executeQuery("SELECT * FROM " + table); 改成 ResultSet rs = stmt.executeQuery("SELECT * FROM " + table + "
欢迎光临 纳速健身 (https://nasue.com/)
Powered by Discuz! X3.4