简体   繁体   English

Java将excel文件(POI)导入mysql关于空白列

[英]Java import excel file(POI) into mysql about blank column

I tried to import excel file into database(MySQL).我试图将 excel 文件导入数据库(MySQL)。

When Excel has empty column, Console window shows error message当 Excel 有空列时,控制台窗口显示错误消息

... Import rows 16 Import rows 17 Import rows 18 Import rows 19 Import rows 20 Import rows 21 Import rows 22 Import rows 23 Exception in thread "main" java.lang.NullPointerException at jdbcTest.main(jdbcTest.java:68) ... 导入行 16 导入行 17 导入行 18 导入行 19 导入行 20 导入行 21 导入行 22 导入行 23 线程“main”中的异常 java.lang.NullPointerException at jdbcTest.main(jdbcTest.java:68)

How to fix this error?.如何修复此错误?。 My source code is我的源代码是

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String url    =   "jdbc:mysql://localhost/dahan?characterEncoding=utf8";
    String user    =   "root";
    String password   =   "apmsetup";

    try{
        Class.forName("com.mysql.jdbc.Driver");  
        Connection con = (Connection) DriverManager.getConnection(url,user,password);  
        con.setAutoCommit(false);  
        PreparedStatement pstm1 = null ; 
        FileInputStream input = new FileInputStream("C:/Users/hyunwoo/Downloads/Personal Contacts.xls");  
        POIFSFileSystem fs = new POIFSFileSystem( input );  
        @SuppressWarnings("resource")
        HSSFWorkbook wb = new HSSFWorkbook(fs);  
        HSSFSheet sheet = wb.getSheetAt(0); 
        Row row;
        String del = "DROP TABLE IF EXISTS `dahanMail`";
        /*String cre = "CREATE TABLE `dahanMail` (`Fullname`  varchar(50),`Firstname`  varchar(50),`Lastname`  varchar(50) ,`Nickname`  varchar(50),"
                + "`Company`  varchar(500),`Department`  varchar(500),`Position`  varchar(500),`Mail1`  varchar(50),"
                + "`Mail2`  varchar(50),`Number1`  varchar(50),`Number2`  varchar(50),`Number3`  varchar(50),"
                + "`PhoneNumber1`  varchar(50),`PhoneNumber2`  varchar(50),`Fax1`  varchar(50),`Fax2`  varchar(50),"
                + "`Address`  varchar(50),`Website`  varchar(50),`id`  int NOT NULL ,PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=utf8;";*/
        pstm1 = (PreparedStatement) con.prepareStatement(del);
        pstm1.execute();
        del = "CREATE TABLE `dahanMail` (`Fullname`  varchar(50) ,`Firstname`  varchar(50),`Lastname`  varchar(50) ,"
                + "`id`  int NOT NULL ,PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        pstm1 = (PreparedStatement) con.prepareStatement(del);
        pstm1.execute();

        //String sql = "INSERT INTO dahanmail VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        String sql = "INSERT INTO dahanmail VALUES(?,?,?,?)";
        PreparedStatement pstm = (PreparedStatement) con.prepareStatement(sql);


        for(int i = sheet.getFirstRowNum(); i<=sheet.getLastRowNum(); i++){
            row = sheet.getRow(i);

                   pstm.setString(1, row.getCell(0).getStringCellValue());
                   pstm.setString(2,row.getCell(1).getStringCellValue());
                   pstm.setString(3,row.getCell(2).getStringCellValue());
                /* pstm.setString(4,row.getCell(3).getStringCellValue());
                   pstm.setString(5,row.getCell(4).getStringCellValue());
                   pstm.setString(6,row.getCell(5).getStringCellValue());
                   pstm.setString(7,row.getCell(6).getStringCellValue());
                   pstm.setString(8,row.getCell(7).getStringCellValue());
                   pstm.setString(9,row.getCell(8).getStringCellValue());
                   pstm.setString(10,row.getCell(9).getStringCellValue());
                   pstm.setString(11,row.getCell(10).getStringCellValue());
                   pstm.setString(12,row.getCell(11).getStringCellValue());
                   pstm.setString(13,row.getCell(12).getStringCellValue());
                   pstm.setString(14,row.getCell(13).getStringCellValue());
                   pstm.setString(15,row.getCell(14).getStringCellValue());
                   pstm.setString(16,row.getCell(15).getStringCellValue());
                   pstm.setString(17,row.getCell(16).getStringCellValue());
                   pstm.setString(18,row.getCell(17).getStringCellValue());*/
                   pstm.setInt(4,(int)row.getCell(18).getNumericCellValue());
                   pstm.execute();
                   pstm.clearParameters();
                   System.out.println("Import rows "+i);
               // Do something useful with the cell's contents

        }
        con.commit();
        pstm.close();
        con.close();
        input.close();
        System.out.println("Success import excel to mysql table");
    }catch(ClassNotFoundException e){
        System.out.println(e);
    }catch(SQLException ex){
        System.out.println(ex);
    }catch(IOException ioe){
        System.out.println(ioe);
    }
}
}

It is well-known (and documented in multiple other answers), that POI's get cell method will return null on empty cells.众所周知(并记录在多个其他答案中),POI 的 get cell 方法将在空单元格上返回 null。

Your code then calls getStringCellValue on the null, which causes the Exception.然后您的代码在 null 上调用 getStringCellValue,这会导致异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM