博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java导出csv 代码
阅读量:4597 次
发布时间:2019-06-09

本文共 2012 字,大约阅读时间需要 6 分钟。

java导出csv 代码  

01
package com.du.string;
02
03
import java.io.File;
04
import java.io.FileInputStream;
05
import java.io.FileOutputStream;
06
import java.io.InputStream;
07
import java.sql.Connection;
08
import java.sql.DriverManager;
09
import java.sql.ResultSet;
10
import java.sql.ResultSetMetaData;
11
import java.sql.Statement;
12
import java.util.Properties;
13
14
public class Pro {
15
16
    public void run(){
17
        try{
18
19
20
            Properties prot =new Properties();
21
            InputStream fis=new FileInputStream(new File("./config.properties"));
22
            prot.load(fis);
23
            System.out.println(prot.getProperty("tables"));
24
25
            FileOutputStream fos=new FileOutputStream(prot.getProperty("csvpath"),true);
26
            System.out.println(prot.getProperty("csvpath"));
27
28
29
            Class.forName(prot.getProperty("driver"));
30
            Connection conn=DriverManager.getConnection(prot.getProperty("jdbc"),prot.getProperty("user"),prot.getProperty("password"));
31
32
            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();
51
52
                    if(i<rsmd.getColumnCount()){
53
                        ls=ls+",";
54
                    }
55
                    }
56
                fos.write("\r\n".getBytes());
57
                fos.write(ls.getBytes());
58
            }
59
60
            //fos.write("aaa".getBytes());
61
            fos.close();
62
        }
63
        catch(Exception e){
64
            e.printStackTrace();
65
        }
66
        finally{
67
68
        }
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
    }
79
80
}

转载于:https://www.cnblogs.com/xrkbgan/p/3770771.html

你可能感兴趣的文章
luogu P3809 【模板】后缀排序
查看>>
Red Gate 破解
查看>>
JVM 调优工具
查看>>
SCTF 2014 pwn题目分析
查看>>
集合以及特殊集合
查看>>
USACO 2.2 Runaround Numbers
查看>>
利用 force index优化sql语句性能
查看>>
Matlab画图-非常具体,非常全面
查看>>
365. Water and Jug Problem
查看>>
SQL数据库数据检索top和distinct
查看>>
平衡搜索树--红黑树 RBTree
查看>>
sqlite驱动下载
查看>>
让IE6/IE7/IE8浏览器支持CSS3属性
查看>>
队列实现霍夫曼树
查看>>
【Java】图片高质量缩放类
查看>>
Python :类中设置默认属性并修改
查看>>
磁盘管理综合测试
查看>>
Unity3d Shader开发(三)Pass(Pass Tags,Name,BindChannels )
查看>>
UMLet
查看>>
从父控件移除控件
查看>>