package com.lantone.qc.kernel;

import java.sql.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : DataImportQcText
 * @Description :
 * @Author : 楼辉荣
 * @Date: 2020-04-16 18:28
 */
public class DataImportQcText {
    public static void main(String[] args) {
        DataImportQcText test = new DataImportQcText();
        test.transRechome();
    }

    private void transRechome() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = MysqlJdbcUtil.getConnection("3");
            stmt = conn.createStatement();

            DatabaseMetaData dmd = conn.getMetaData();
            ResultSet dmdRs = dmd.getColumns(null, null, "BR_RECHOME", null);

            Map<String, String> colMap = new LinkedHashMap<>();

            String sql = "insert into BR_RECHOME_CX(";

            while(dmdRs.next()){
                colMap.put(dmdRs.getString(4), dmdRs.getString("REMARKS"));
            }
            int i = 0;
            for (Map.Entry<String, String> map : colMap.entrySet()) {
                if (i == 0) {
                    sql = sql + map.getKey();
                } else {
                    sql = sql + "," + map.getKey();
                }
                i++;
            }
            sql = sql + ") select ";
            i = 0;
            for (Map.Entry<String, String> map : colMap.entrySet()) {
                if (i == 0) {
                    sql = sql + map.getKey();
                } else {
                    sql = sql + "," + map.getKey();
                }
                i++;
            }
            sql = sql + " from BR_RECHOME";
            System.out.println(sql);
            dmdRs.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}