当前位置: 代码迷 >> J2SE >> ResultSetMetaData 自动获取表列,该怎么解决
  详细解决方案

ResultSetMetaData 自动获取表列,该怎么解决

热度:76   发布时间:2016-04-24 01:28:30.0
ResultSetMetaData 自动获取表列
while(rs.next()){
ResultSetMetaData rsmd = rs.getMetaData();
int counts = rsmd.getColumnCount();

for(int col=0; col < counts; col++) {
String colname = rsmd.getColumnName(col+1);
String colvalue=rs.getString(colname);
sb.append(colname);
sb.append(",");
sb.append(colvalue);
sb.append(",");
}
sb.append("\r\n");
}
}

上面的代码导出的TXT文件是下面这样:
itId,272425,id,6763,code,1217001,itemName,一年内到期的长期负债,itPid,0,itemLevel,272425,attribute,0,calculationFormula,31323130313030,com_id,null,itemBackupName,null,display,1,isRectItem,null,
itId,272426,id,6763,code,1218001,itemName,其他流动负债,itPid,0,itemLevel,272426,attribute,0,calculationFormula,31323130313030,com_id,null,itemBackupName,null,display,1,isRectItem,null,
itId,272427,id,6763,code,1210100,itemName, 流动负债合计,itPid,0,itemLevel,272427,attribute,0,calculationFormula,313230303130303D313231323030312B313231323230312B313231323230322B313231323230332B313231323135312B313231323135332B313231323233322B313231323137312B313231323137362B313231323234312B313231323139312B313232323830312B313231373030312B31323138303031,com_id,null,itemBackupName,null,display,1,isRectItem,null,
itId,272428,id,6763,code,1222501,itemName,长期借款,itPid,0,itemLevel,272428,attribute,0,calculationFormula,31323230313030,com_id,null,itemBackupName,null,display,1,isRectItem,null,
itId,272429,id,6763,code,1222502,itemName,应付债券,itPid,0,itemLevel,272429,attribute,0,calculationFormula,31323230313030,com_id,null,itemBackupName,null,display,1,isRectItem,null,
itId,272430,id,6763,code,1222701,itemName,长期应付款,itPid,0,itemLevel,272430,attribute,0,calculationFormula,31323230313030,com_id,null,itemBackupName,null,display,1,isRectItem,null,

我想改成下面这样,应该怎么改一下代码?
colId, sid, id, colCode, colName, colPid, colLevel, colType, colLenght, colDate, com_id, display, formula, isText
75122,2,6763,BQSJ,期末数|||实际数,0,75122,0,0,2010-07-26 10:51:54.193,1,1,NULL,0
75123,10,6763,BNNC,年初数,0,75123,0,0,2010-07-26 10:51:54.193,1,1,NULL,0
75124,6,6763,BQHB,合并数,0,75124,0,0,2010-07-26 10:51:54.193,1,0,NULL,0
75125,4,6763,BQYS,预算数,0,75125,0,0,2010-07-26 10:51:54.193,1,1,NULL,0
75126,11,6763,YSHB,预算合并数,0,75126,0,0,2010-07-26 10:51:54.210,1,0,NULL,0
75127,2,6764,BQSJ,本期数|||实际数|||本期发生|||本月金额|||本期金额|||本月数,0,75127,0,0,2010-07-26 10:51:54.927,1,1,NULL,0
75128,3,6764,BQLJ,累计数,0,75128,0,0,2010-07-26 10:51:54.927,1,1,NULL,0
75129,4,6764,BQYS,预算数,0,75129,0,0,2010-07-26 10:51:54.927,1,1,NULL,0
75130,5,6764,YSLJ,预算累计数,0,75130,0,0,2010-07-26 10:51:54.927,1,1,NULL,0
75131,6,6764,BQHB,合并数,0,75131,0,0,2010-07-26 10:51:54.927,1,0,NULL,0

------解决方案--------------------
Java code
try {                    Class.forName(dbdriver);                    conn=DriverManager.getConnection(url, us, pw);                    stat=conn.createStatement();                    rs=stat.executeQuery(sql);                    ResultSetMetaData rsmd = rs.getMetaData();                    int counts = rsmd.getColumnCount();                    for (int col = 0; col < counts; col++) {                        String colname = rsmd.getColumnName(col+1);                        sb.append(rsmd.getColumnName(col+1)+",");                        System.out.println(colname);                    }                    sb.setLength(sb.length()-1);                    sb.append("\r\n");                    while(rs.next()){                         sb.append(rs.getInt("colId")+",");                         sb.append(rs.getInt("sid")+",");                         //....自己写                         sb.setLength(sb.length()-1);                         sb.append("\r\n");                    }                } catch (ClassNotFoundException e) {                e.printStackTrace();                } catch (SQLException e) {                e.printStackTrace();                }                return sb.toString();            }