请查阅 数据库元语言(metadata) 相关资料

解决方案 »

  1.   

    网上搜一下,很多.
    /******************************************** 
    格式化输出数据库记录,出自<<使用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; 


      

  2.   

    可以通过rst.getXXX("字段名")来取得值,也可以通过rst.getXXX(int)来取得值
      

  3.   

    谢谢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){...}