how to fix this java.util.Date can not be cast to java.sql.Date

1

hi I'm uploading an excel file to jsp to save it in MySQL the excel file has two fields type date, at the time of reading the respective file I get this error java.util.Date can not be cast to java .sql.Date when executing it and I could not solve it, someone knows how I can fix this I upload the code where I am reading the excel file, the cases where I evaluate the two fields type date are 3 and 4 I do not know how you can evaluate in this part either

    for (int i = 1; i <= firstsheet.getLastRowNum(); i++) {
        fila = firstsheet.getRow(i);
        No = new TablaNovedadEmpleado();
        No.setFecha(fecha);

        int j = 0;
        for (j = 0; j < fila.getLastCellNum(); j++) {
            Cell celda = fila.getCell(j);
            if (j == 0) {
                if (celda == null || celda.getCellType() == Cell.CELL_TYPE_BLANK) {
                    break;
                }
            }

            switch (j) {

                case 1:
                    if (celda.getCellType() == Cell.CELL_TYPE_STRING) {
                        No.setNombres(celda.getStringCellValue());
                    }
                    break;

                case 2:
                    if (celda.getCellType() == Cell.CELL_TYPE_STRING) {
                        No.setTipo_Novedad(celda.getStringCellValue());
                    }
                    break;
                case 3:
                    if (celda.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        No.setFecha_inicio((Date) celda.getDateCellValue());

                    }
                    break;

                case 4:
                    if (celda.getCellType() == Cell.CELL_TYPE_FORMULA) {
                        No.setFecha_fin((Date) celda.getDateCellValue());
                    }
                    break;

                case 5:
                    if (celda.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        No.setDias(celda.getStringCellValue());
                    }
                    break;
                case 6:
                    if (celda.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        No.setHoras(celda.getStringCellValue());
                    }
                    break;
            }

        }
        if (No.getNombres() != null) {
            lst.add(No);

            //guardarArchivoNoveddades(No);
        }
    }
    Collections.sort(lst, TablaNovedadEmpleado.comparet);
    
asked by victormbrt 19.01.2017 в 22:04
source

1 answer

3

The error indicates the problem:

  

java.util.Date can not be cast to java.sql.Date

You are sending a data type of java.util.Date but you expect to receive java.sql.Date

I think the problem arises in these lines of code:

 No.setFecha_inicio((Date) celda.getDateCellValue());

 No.setFecha_fin((Date) celda.getDateCellValue());

what you can do is convert the type java.util.Date to java.sql.Date

for example:

No.setFecha_inicio(new java.sql.Date(((Date) celda.getDateCellValue()).getTime()));

and

No.setFecha_fin(new java.sql.Date(((Date) celda.getDateCellValue()).getTime()));
    
answered by 19.01.2017 в 23:19