java中如何枚举结果集中所有字段名 请查阅 数据库元语言(metadata) 相关资料 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 网上搜一下,很多./******************************************** 格式化输出数据库记录,出自<<使用java进行SQL数据库程序设计>> 返回值为格式化的字符串 ********************************************/ import java.sql.*; class Display extends Object { ResultSet theResultSet; String theResult; public void display() {} public void setResult(ResultSet result) { theResultSet= result; } public String getString() throws SQLException,NoClassDefFoundError { theResult= ""; ResultSetMetaData metaData= theResultSet.getMetaData(); int colcount = metaData.getColumnCount(); int colSize[]= new int[colcount]; String colLabel[]= new String[colcount]; int colType[]= new int[colcount]; String colTName[]= new String[colcount]; int colPrec[]= new int[colcount]; int colScale[]= new int[colcount]; theResult +=" "; for(int i= 1;i<= colcount;i++) { colSize[i-1] = metaData.getColumnDisplaySize(i); colLabel[i-1]= metaData.getColumnLabel(i); colType[i-1] = metaData.getColumnType(i); colTName[i-1]= metaData.getColumnTypeName(i); colPrec[i-1] = metaData.getPrecision(i); colScale[i-1]= metaData.getScale(i); if(colSize[i-1]<1+ colLabel[i-1].length()) colSize[i-1]= 1+colLabel[i-1].length(); theResult+= rightPad(colLabel[i-1],colSize[i-1]); } theResult +=" "; int row= 0; while(theResultSet.next()) { row++; for(int i=1;i<= colcount;i++) { String colvalue= theResultSet.getString(i); if(colvalue== null) colvalue=""; theResult+= rightPad(colvalue,colSize[i-1]); } theResult+=" "; } theResult+=" (" +row+ "rows included) "; return theResult; } private String rightPad(String s,int len) { int curlen= s.length(); if(curlen>len) return repString("*",len); return (s+repString(" ",(len-curlen))); } private String repString(String s,int times) { String result=""; for(int i=0;i<times;i++) result+= s; return result; } } 可以通过rst.getXXX("字段名")来取得值,也可以通过rst.getXXX(int)来取得值 谢谢luotitan(泰坦)的回答本人也找到一个更好的解法:Statement smt=conn.createStatement();ResultSetrst=smt.executeQuery(sql);try { ResultSetMetaData rmd = rst.getMetaData(); //循环枚举出所有字段名 for(int i=0;i<rmd.getColumnCount();i++) { //第一个字段名从1开始 String fn=rmd.getColumnName(i+1); }}catch(Exception ex){...} java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType 请问安装JavaEE之前必须安装JavaSE吗? mysql语句中可以使用while循环吗? jsp mail发送问题 web.xml 中一定要定义 tld 文件吗? struts业务处理 spring 事务管理 问题 关于二级分类,三级分类 和 连动下拉菜单 有关ejb中环境变量的配置问题 struts1+spring+ibatis框架整合出错 struts问题 新手提问:关于freemarker的问题?
/********************************************
格式化输出数据库记录,出自<<使用java进行SQL数据库程序设计>>
返回值为格式化的字符串
********************************************/
import java.sql.*; class Display extends Object
{
ResultSet theResultSet;
String theResult; public void display()
{} public void setResult(ResultSet result)
{
theResultSet= result;
} public String getString()
throws SQLException,NoClassDefFoundError
{
theResult= "";
ResultSetMetaData metaData= theResultSet.getMetaData();
int colcount = metaData.getColumnCount();
int colSize[]= new int[colcount];
String colLabel[]= new String[colcount];
int colType[]= new int[colcount];
String colTName[]= new String[colcount];
int colPrec[]= new int[colcount];
int colScale[]= new int[colcount]; theResult +=" ";
for(int i= 1;i<= colcount;i++)
{
colSize[i-1] = metaData.getColumnDisplaySize(i);
colLabel[i-1]= metaData.getColumnLabel(i);
colType[i-1] = metaData.getColumnType(i);
colTName[i-1]= metaData.getColumnTypeName(i);
colPrec[i-1] = metaData.getPrecision(i);
colScale[i-1]= metaData.getScale(i); if(colSize[i-1]<1+ colLabel[i-1].length())
colSize[i-1]= 1+colLabel[i-1].length();
theResult+= rightPad(colLabel[i-1],colSize[i-1]);
} theResult +=" ";
int row= 0; while(theResultSet.next())
{
row++;
for(int i=1;i<= colcount;i++)
{
String colvalue= theResultSet.getString(i);
if(colvalue== null)
colvalue="";
theResult+= rightPad(colvalue,colSize[i-1]);
}
theResult+=" ";
}
theResult+=" (" +row+ "rows included) ";
return theResult;
} private String rightPad(String s,int len)
{
int curlen= s.length();
if(curlen>len)
return repString("*",len);
return (s+repString(" ",(len-curlen)));
} private String repString(String s,int times)
{
String result="";
for(int i=0;i<times;i++)
result+= s;
return result;
}
}
本人也找到一个更好的解法:
Statement smt=conn.createStatement();
ResultSetrst=smt.executeQuery(sql);
try {
ResultSetMetaData rmd = rst.getMetaData();
//循环枚举出所有字段名
for(int i=0;i<rmd.getColumnCount();i++)
{
//第一个字段名从1开始
String fn=rmd.getColumnName(i+1);
}}
catch(Exception ex){...}