• Postgresql 读取txt到DB 插入或更新


    *

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.math.BigDecimal;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.List;
    import java.util.Objects;
    
    import org.springframework.stereotype.Component;
    
    
    
    
    public class Read2DB {
    
    
        /**
         * 重复数据覆盖原来的
         * NaN的数据跳过
         * @param filePath
         */
        public void readTxtFile(String filePath) {
            String[] pronos = new String[13];
            List<ProdNavDaily> navDailies = new ArrayList<>();
    
            PreparedStatement ps = null;
            Connection con = null;
            ResultSet rs = null;
    
            String username = "maple";
            String pwd = "123";
            String connurl = "jdbc:postgresql://localhost:5432/abc";
    
            try {
                con = DriverManager.getConnection(connurl, username, pwd);
                Class.forName("org.postgresql.Driver");
    
                String encoding = "GBK";
                File file = new File(filePath);
                StringBuffer sb=new StringBuffer();
                if (file.isFile() && file.exists()) { // 判断文件是否存在
                    InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    int count = 1;
                    lineTxt = bufferedReader.readLine();
                    // 第一行
                    pronos = lineTxt.split("	");
                    //产品编号,不足六位前面补0
                    for(int i=0;i<pronos.length;i++){
                        pronos[i]=padLeftZero(pronos[i]);
                    }
                    
                    while ((lineTxt = bufferedReader.readLine()) != null) {
                        String[] arr = lineTxt.split("	");
                        // 每一行数据要插入12条数据到数据库中
                        sb=new StringBuffer("INSERT INTO prod_nav_daily (prod_no, nav_date, unit_nav) VALUES ");
                        for (int i = 1; i < pronos.length; i++) {
                            if(arr[i]==null||Objects.equals(arr[i].toLowerCase(), "nan")){//NaN
                                continue;
                            }                        
                            sb.append(" ('" + pronos[i] + "','" + toDate(arr[0]) + "','" + new BigDecimal(arr[i]) + "'),");
                        }
                        sb=new StringBuffer(sb.substring(0, sb.length()-1));//去掉逗号
                        sb.append(" ON CONFLICT (prod_no,nav_date) DO UPDATE SET unit_nav = excluded.unit_nav");
                        ps = con.prepareStatement(sb.toString());
                        ps.executeUpdate();
                    }
                    read.close();
                } else {
                    System.out.println("找不到指定的文件");
                }
            } catch (Exception e) {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            } finally {
                try {
                    con.close();
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                System.out.println("文件读取完毕:"+filePath);
            }
        }
    
        /**
         * 数值转为日期(可以在excel中验证)
         * @param amount
         * @return
         */
        public static Date toDate(String amount) {
            Calendar c = new GregorianCalendar(1900, 0, -1);
    
            Date dt = new Date();
            c.add(Calendar.DATE, Integer.valueOf(amount));
            dt = c.getTime();
            // SimpleDateFormat sdFormat=new SimpleDateFormat("yyyy/MM/dd");
            // System.out.println(sdFormat.format(dt));
            return dt;
    
        }
    
        public static void main(String argv[]) {
            Read2DB readTxt = new Read2DB();
            for(int i=1;i<7;i++){
                String filePath = "E:\345\data"+i+".txt";
                readTxt.readTxtFile(filePath);
            }
            //System.out.println(readTxt.padLeftZero("11"));
            /*String aString="01234";
            System.out.println(aString.substring(0, aString.length()-1));*/
        }
        
        //不足六位,前面补0
        public String padLeftZero(String str){
            return String.format("%06d", Integer.parseInt(str));
            //return String.format("%16s", Integer.parseInt(str));
        }
    }

    ***

    注意,使用 on confict,需要这个建立约束,否则错误如下

    [Err] ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification

    具体建立

    INSERT INTO the_table (id, column_1, column_2) 
    VALUES (1, 'A', 'X'), (2, 'B', 'Y'), (3, 'C', 'Z')
    ON CONFLICT (id) DO UPDATE 
      SET column_1 = excluded.column_1, 
          column_2 = excluded.column_2;
    
    INSERT INTO prod_nav_daily (prod_no, nav_date, unit_nav) VALUES ('0001', current_date, 11) 
    ON CONFLICT (prod_no,nav_date) DO UPDATE SET unit_nav = excluded.unit_nav;
    
    select * from prod_nav_daily
    
    --增加约束
    alter table prod_nav_daily add
    constraint uk_tbl_unique_prod_no_nav_date unique(prod_no,nav_date);
    
    
    SELECT count(*) FROM prod_nav_daily

    *

    有问题在公众号【清汤袭人】找我,时常冒出各种傻问题,然一通百通,其乐无穷,一起探讨


  • 相关阅读:
    毕业设计:参考文献(3)
    毕业设计:参考文献(5)
    毕业设计:参考文献(1)
    毕业设计:参考文献(7)
    毕业设计:参考文献(2)
    2021ICPC沈阳站游记
    2021CCPC广州站游记
    常用linux 脚本
    Jenkins调优实践
    linux工具安装
  • 原文地址:https://www.cnblogs.com/qingmaple/p/6611850.html
Copyright © 2020-2023  润新知