Reading an image from Excel and saving it in sqlserver

I have a table called 'tab1'

cl_id int //auto imcrement
cl_image image

I want to read an image from excel with the image and save it in the above table

FileInputStream fileInputStream = new FileInputStream(
            "Delux.xls");
    System.out.println(fileInputStream);
    HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
    HSSFSheet worksheet = workbook.getSheet("Delux");
    Iterator rows = worksheet.rowIterator();
    HSSFRow row = (HSSFRow) rows.next();
      List lst = workbook.getAllPictures();
      Iterator it = lst.iterator();
    while (rows.hasNext()) {
        row = (HSSFRow) rows.next();

//reading the image from excel

        HSSFCell cellP1 = row.getCell((short) 1);
           PictureData pict = (PictureData)it.next();
            String ext = pict.suggestFileExtension();
            byte[] data = pict.getData();

                InputStream is = new ByteArrayInputStream(data);

        try {

            PreparedStatement stmt = getdbconn()
                    .prepareStatement(
                            "insert into tab1 (cl_image) values(?)");

            stmt.setBinaryStream(1, is);
            stmt.executeUpdate();
            is.close();
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        } catch (SQLException e) {

            e.printStackTrace();
        }
    }

but when I dynamically save the image, I get an error like

"String or binary data will be truncated."

Can someone suggest me a way to achieve this?

+4
source share
1 answer

, IMAGE TEXT, , . VARBINARY (). - IMAGE , VARBINARY, 2 . , , , cl_image.

0

Source: https://habr.com/ru/post/1609747/


All Articles