下面是我写的CSV输出的类,其他你不用看,只要看======中间的,就可以了,我比较懒,没有整理              String SQL = Sql;

DefaultConnection con = null;
try{
    con = new DefaultConnection();
}catch(SQLException e){
CsvProcessorException ce = new CsvProcessorException(e.getMessage(),"DbToCsvProcessor#setDbToCsvContent(String)");
LoggingManager.write(new ApplicationLog("DbToCsv", "DbToCsv#setDbToCsvContent(Sql): Connection failure.", 10));
throw ce;
} try{
======================================================================
    ResultSet rs = con.executeQuery(SQL);
    ResultSetMetaData rsmd=rs.getMetaData();                            String tableName = ""
int count=rsmd.getColumnCount();
for (int i=1;i<=count ;i++ )
{
String colName = rsmd.getColumnName(i); fileConcent.append("\""+colName+"\"");
if(i!=count){
fileConcent.append(",");
}
}
fileConcent.append("\r\n");
while(rs.next())
{
for (int i=1;i<=count ;i++ )
{
int colType = rsmd.getColumnType(i);
String quote = "";
if (colType==1 || colType==12)
quote = "\"";
fileConcent.append(quote+(rs.getString(i)==null?"":rs.getString(i))+quote);
if(i!=count){
fileConcent.append(",");
}
}
fileConcent.append("\r\n"); }
    rs.close();==================================================================
}catch(SQLException e){
CsvProcessorException ce = new CsvProcessorException(e.getMessage(),"DbToCsvProcessor#setDbToCsvContent(String)");
LoggingManager.write(new ApplicationLog("DbToCsv", "DbToCsv#setDbToCsvContent(Sql) SQL ERROR: "+e+"!!!" + SQL, 10));
throw ce;
        }catch(Exception e){
CsvProcessorException ce = new CsvProcessorException(e.getMessage(),"DbToCsvProcessor#setDbToCsvContent(String)");
LoggingManager.write(new ExceptionLog(ce, 0));
throw ce;
        }finally{
con.closeConnection();
}

解决方案 »

  1.   

    给你注释一下吧!
                  //设置SQL语句
                  String SQL = Sql; 

    DefaultConnection con = null;
    try{
        con = new DefaultConnection();
    }catch(SQLException e){
    CsvProcessorException ce = new CsvProcessorException(e.getMessage(),"DbToCsvProcessor#setDbToCsvContent(String)");
    LoggingManager.write(new ApplicationLog("DbToCsv", "DbToCsv#setDbToCsvContent(Sql): Connection failure.", 10));
    throw ce;
    } try{
    ======================================================================
        ResultSet rs = con.executeQuery(SQL);
        ResultSetMetaData rsmd=rs.getMetaData();                            //表名
                                String tableName = "";
    int count=rsmd.getColumnCount();                            if count >0 
                                      tableName = rs.getTableName();                           //循环写出列名
    for (int i=1;i<=count ;i++ )
    {
    String colName = rsmd.getColumnName(i); fileConcent.append("\""+colName+"\"");
    if(i!=count){
    fileConcent.append(",");
    }
    }
    fileConcent.append("\r\n");                             //循环写出列值
    while(rs.next())
    {
    for (int i=1;i<=count ;i++ )
    {
    int colType = rsmd.getColumnType(i);
    String quote = "";
    if (colType==1 || colType==12)
    quote = "\"";
    fileConcent.append(quote+(rs.getString(i)==null?"":rs.getString(i))+quote);
    if(i!=count){
    fileConcent.append(",");
    }
    }
    fileConcent.append("\r\n"); }
        rs.close();==================================================================
    }catch(SQLException e){
    CsvProcessorException ce = new CsvProcessorException(e.getMessage(),"DbToCsvProcessor#setDbToCsvContent(String)");
    LoggingManager.write(new ApplicationLog("DbToCsv", "DbToCsv#setDbToCsvContent(Sql) SQL ERROR: "+e+"!!!" + SQL, 10));
    throw ce;
            }catch(Exception e){
    CsvProcessorException ce = new CsvProcessorException(e.getMessage(),"DbToCsvProcessor#setDbToCsvContent(String)");
    LoggingManager.write(new ExceptionLog(ce, 0));
    throw ce;
            }finally{
    con.closeConnection();
    }
      

  2.   

    to StevenWSF(出差2个月信誉分少了20分,气死我了!) 请看好题目。有没有人做过解析sql呀?能不能讲一讲?谢谢了。