Java – read the image from excel and store it in SQL server

I have a table called 'tab1'

cl_id int //auto imcrement
cl_image image

I want to read the image in Excel from the image and store 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 store images, I get errors like

Anyone can suggest me a way to achieve this???

Solution

Image, text and other data types are not supported and are not recommended Therefore, change it to varbinary The most important import thing is that the image data type needs to store the size in a special way, rather than expanding varbinary to meet any way up to 2GB Because the image is not suitable for CL_ Image field, so an error was raised

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>