下面是我写的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();
}
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();
}
解决方案 »
- 关于java多线程的一个异常IllegalMonitorStateException
- Swing 如何弹出模式窗体,同时窗体上有最大化、最小化和关闭按钮
- 如何获得当前日期并显示在GUI组件中显示
- Socket读取时抛错java.net.SocketException: Connection reset
- 怎么将java中的byte,Int,double,float等类型转变为gb2312编码?
- JNI本地方法里面怎么把char* 转换为jstring类型啊?`
- Help me! 一个关于Sun开发者网站用户注册问题.
- 找书
- 请问用java做的工具条,是怎样使界面呈现花纹的??
- TCP转发如何实现?
- Calendar类的疑问
- 有没有人把一个一维的ArrayList转化成通用的Object,其ArrayList的每一个element 对应Object的一个属性
//设置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();
}