Postgresql 讀取txt到DB 插入或更新

*java

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("\t");
                //產品編號,不足六位前面補0
                for(int i=0;i<pronos.length;i++){
                    pronos[i]=padLeftZero(pronos[i]);
                }
                
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    String[] arr = lineTxt.split("\t");
                    // 每一行數據要插入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));
    }
}

***spring

注意,使用 on confict,須要這個創建約束,不然錯誤以下sql

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

具體創建app

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

 

*post

相關文章
相關標籤/搜索