数组IndexOutOfBounds无法弄清楚

时间:2019-06-01 19:13:02

标签: java database exception

我正在尝试将大型csv文件加载到数据库中,但是有一些我无法摆脱的异常。我还可以提供一些技巧,以更快地将数据加载到数据库中

acct.class

     public static AcctDocHdr set(String array[]) {

        AcctDocHdr obj = new AcctDocHdr();
        try {
            obj.setAccountId(checkInt(array[0]));
            obj.setDocumentNumberNorm(checkString(array[1]));
            obj.setCompanyCode(checkString(array[2]));
            obj.setFiscalYear(checkString(array[3]));
            obj.setFiscalYear(checkString(array[4]));
            obj.setBranch(checkString(array[5]));
            obj.setCustomerNumberNorm(checkString(array[6]));
            obj.setFkCustomerMapId(checkInt(array[7]));
            obj.setDocumentDateNorm(checkDate(array[8]));
            obj.setBaselineDateNorm(checkDate(array[9]));
            obj.setDueDateNorm(checkDate(array[10]));
            obj.setInvoiceNumberNorm(checkString(array[11]));
            obj.setOpenAmountNorm(checkDouble(array[12]));
            obj.setPaymentTerms(checkString(array[13]));
            obj.setClearingDateNorm(checkDate(array[14]));
            obj.setIsOpen(checkInt(array[15]));
            obj.setOrderType(checkString(array[16]));
            obj.setOrderDate(checkDate(array[17]));
            obj.setBusinessArea(checkString(array[18]));
            obj.setShipDate(checkDate(array[19]));
            obj.setJobId(checkInt(array[20]));
            obj.setTaxAmt(checkDouble(array[21]));
            obj. setCurrentDisputeAmount(checkDouble(array[22]));
            obj.setShipTo(checkString(array[23]));
            obj.setDocumentId(checkInt(array[24]));
            obj.setDocumentDate(checkDate(array[25]));
            obj.setActualOpenAmount(checkDouble(array[26]));
            obj. setDueDate(checkDate(array[27]));
            obj.setInvoiceAge(checkInt(array[28]));
            obj. setIsValidDispute(checkInt(array[29]));
            obj.setPostingKey(checkString(array[30]));
            obj.setStrategyId(checkInt(array[31]));
            obj.setCurrency(checkString(array[32]));
            obj.setDebitCreditIndicator(checkInt(array[33]));
            obj.setValidOpenAmount(checkDouble(array[34]));
            obj.setCustomerName(checkString(array[35]));
            obj.setRetainage_amount(checkDouble(array[36]));


        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
    public static String checkString(String s)
    {
        if(s==null||s.equals(" ")||s.charAt(0)=='\\'||s.equals("\\N") || s.charAt(0)=='S')
        return null;
        else
        return s;
    }
    public static Date checkDate(String s)
    {
       Date d=null;
        try
        {
            d=Date.valueOf(s);


        }
        catch(Exception e)
        {}
        return d;
    }
    public static Double checkDouble(String s)
    {
        Double d=0.0;
        try
        {
            d=Double.parseDouble(s);
        }
        catch(Exception e)
        {}
        return d;
    }
    public static int checkInt(String s)
    {
        int d=0;
        try
        {
            d=Integer.parseInt(s);
        }
        catch(Exception e)
        {}
        return d;
    }

    public static boolean checkBoolean(String s) {
        boolean b = false;
        if(s!=null&&s.equals("1")) b = true;
        return b;
    }

loadintodb.java

       String paramQuery = "INSERT INTO acct_doc_hdr  VALUES(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, 
      ?,?)";

        LinkedList<AcctDocHdr> document = new LinkedList<>();

        try {
            FileReader fr = new FileReader("E:\\HR Docs\\day5\\Sample Dataset.csv");
            BufferedReader bufr = new BufferedReader(fr);
            int count = 0;

            String Line = bufr.readLine();
            while (Line != null && count < 200) {
                String array[] = Line.split(",");
                System.out.println(Line);

                AcctDocHdr ac = AcctDocHdr.set(array);
                document.add(ac);

                Class.forName("com.mysql.jdbc.Driver");
                dbcon = (Connection) DriverManager.getConnection(url, userName, password);
                pstmt = dbcon.prepareStatement(paramQuery);

                ListIterator<AcctDocHdr> iterator = document.listIterator();
                while (iterator.hasNext()) {
                    AcctDocHdr Obje = iterator.next();
                    add(pstmt, Obje);
                    pstmt.addBatch();
                }
                pstmt.executeBatch();
                count++;
            }
            fr.close();
        } catch (Exception e) {
            System.out.println("Unable to read the file");
        }
    }

    private static void add(PreparedStatement pstmt, AcctDocHdr Obje) {

        try {
            pstmt.setInt(1, (int) Obje.getAccountId());
            pstmt.setString(2, Obje.getDocumentNumberNorm());
            pstmt.setString(3, Obje.getCompanyCode());
            pstmt.setString(4, Obje.getFiscalYear());
            pstmt.setString(5, Obje.getBranch());
            pstmt.setString(6, Obje.getCustomerNumberNorm());
            pstmt.setInt(7, (int) Obje.getFkCustomerMapId());
            pstmt.setDate(8, Obje.getDocumentDateNorm());
            pstmt.setDate(9, Obje.getBaselineDateNorm());
            pstmt.setDate(10, Obje.getDueDateNorm());
            pstmt.setString(11, Obje.getInvoiceNumberNorm());
            pstmt.setDouble(12, (double) Obje.getOpenAmountNorm());
            pstmt.setString(13, Obje.getPaymentTerms());
            pstmt.setDate(14, Obje.getClearingDateNorm());
            pstmt.setInt(15, (int) Obje.getIsOpen());
            pstmt.setString(16, Obje.getOrderType());
            pstmt.setDate(17, Obje.getOrderDate());
            pstmt.setString(18, Obje.getBusinessArea());
            pstmt.setDate(19, Obje.getShipDate());
            pstmt.setInt(20, (int) Obje.getJobId());
            pstmt.setDouble(21, (double) Obje.getTaxAmt());
            pstmt.setDouble(22, (double) Obje.getCurrentDisputeAmount());
            pstmt.setString(23, Obje.getShipTo());
            pstmt.setInt(24, (int) Obje.getDocumentId());
            pstmt.setDate(25, Obje.getDocumentDate());
            pstmt.setDouble(26, (double) Obje.getActualOpenAmount());
            pstmt.setDate(27, Obje.getDueDate());
            pstmt.setInt(28, (int) Obje.getInvoiceAge());
            pstmt.setInt(29, (int) Obje.getIsValidDispute());
            pstmt.setString(30, Obje.getPostingKey());
            pstmt.setInt(31, (int) Obje.getStrategyId());
            pstmt.setString(32, Obje.getCurrency());
            pstmt.setInt(33, (int) Obje.getDebitCreditIndicator());
            pstmt.setDouble(34, (double) Obje.getValidOpenAmount());
            pstmt.setString(35, Obje.getCustomerName());
            pstmt.setDouble(36, (double) Obje.getRetainage_amount());

        } catch (Exception e) {

            e.printStackTrace();
        }

控制台(保留量的例外,在csv文件中为空)

java.lang.ArrayIndexOutOfBoundsException: 36
    at com.hr.model.AcctDocHdr.set(AcctDocHdr.java:417)
    at com.hr.model.Loadintodb.main(Loadintodb.java:39)
java.lang.NullPointerException
    at com.hr.model.Loadintodb.add(Loadintodb.java:99)
    at com.hr.model.Loadintodb.main(Loadintodb.java:49)
Unable to read the file

acct.class中的错误行:

 pstmt.setDouble(36, (double) Obje.getRetainage_amount());

加载intodb.class中的错误行:

AcctDocHdr ac = AcctDocHdr.set(array);
add(pstmt, Obje);

0 个答案:

没有答案