java导出csv 代码
01package com.du.string;0203import java.io.File;04import java.io.FileInputStream;05import java.io.FileOutputStream;06import java.io.InputStream;07import java.sql.Connection;08import java.sql.DriverManager;09import java.sql.ResultSet;10import java.sql.ResultSetMetaData;11import java.sql.Statement;12import java.util.Properties;1314public class Pro { 1516 public void run(){ 17 try{ 181920 Properties prot =new Properties();21 InputStream fis=new FileInputStream(new File("./config.properties"));22 prot.load(fis);23 System.out.println(prot.getProperty("tables"));2425 FileOutputStream fos=new FileOutputStream(prot.getProperty("csvpath"),true);26 System.out.println(prot.getProperty("csvpath"));272829 Class.forName(prot.getProperty("driver"));30 Connection conn=DriverManager.getConnection(prot.getProperty("jdbc"),prot.getProperty("user"),prot.getProperty("password"));3132 Statement st=conn.createStatement();33 ResultSet rs= st.executeQuery("select * from "+prot.getProperty("tables"));34 ResultSetMetaData rsmd=rs.getMetaData();35 String columnC="";36 for(int i=1;i<=rsmd.getColumnCount();i++){ 37 columnC=columnC+rsmd.getColumnName(i).toString();38 if(i<rsmd.getColumnCount()){ 39 columnC=columnC+",";40 }41 }42 fos.write(columnC.getBytes());43 while (rs.next()){ 44 String ls="";45 for(int i=1;i<=rsmd.getColumnCount();i++){ 46 String field=rsmd.getColumnTypeName(i);47// if(field.equals("VARCHAR")){ 48// ls=ls+rs.getString(i);49// }50 ls=ls+rs.getObject(i).toString();5152 if(i<rsmd.getColumnCount()){ 53 ls=ls+",";54 }55 }56 fos.write("\r\n".getBytes());57 fos.write(ls.getBytes());58 }5960 //fos.write("aaa".getBytes());61 fos.close();62 }63 catch(Exception e){ 64 e.printStackTrace();65 }66 finally{ 6768 }69 }70 public static void main(String[] args) { 71 Pro p=new Pro();72 try{ 73 p.run();74 }75 catch(Exception e){ 76 e.printStackTrace();77 }78 }7980}