0

I am trying to insert an image as a blob to my sqlite database but I just can't get it to work as I am catching the error java.lang.ArrayIndexOutOfBoundsException: 0 I tried to do it with one try catch statement but again i couldn't figure it out. The url used to create the connection to the database is correct since I have been using the same url throught the other methods and were working fine. Any help would be greatly appreciated.

This is my code:

public Boolean insertIntoProducts(String name, String description, String filePath, Double price, int quantity) {
        String url = "jdbc:sqlite:C:/Users/User/Desktop/MyDatabase.db";
        Boolean success = false;
        String sqlSelectID = "SELECT ID FROM Products ORDER BY ID DESC";
        // Establishing a connection to the database
        try {
            // Initialising the image file
            File image = new File(filePath);
            FileInputStream fileInput = new FileInputStream(image);
            ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            for (int i; (i = fileInput.read(buf)) != -1;) {
                // read all the bytes and store them in the array. Stores the binary data of the
                // image
                byteArray.write(buf, 0, i);

                // Closing the file input to prevent memory leaks
                fileInput.close();
                try (Connection conn = DriverManager.getConnection(url)){
                    Statement statement = conn.();
                    ResultSet result = statement.executeQuery(sqlSelectID);
                    int id = 0;
                    if (result.next()) {
                        id = result.getInt("ID");
                    }
                    String sql = "INSERT INTO Products(ID,Name,Description,Quantity,Price,Image,isAuthorised) "
                            + "VALUES (" + id + ",'" + name + "','" + description + "'," + quantity + ",'" + price
                            + "','?','0');";
                    PreparedStatement pstatement = conn.prepareStatement(sql);
                    pstatement.setBytes(1, byteArray.toByteArray());
                    // Creating a variable to check if the insertion was successful depending if a
                    // row was added to the table
                    int row = 0;
                    row = pstatement.executeUpdate();
                    if (row > 0) {
                        success = true;
                    }
                } catch (SQLException e) {
                    System.out.println(e);
                }
            }

        } catch (Exception e) {
            System.out.println(e);
        }
        return success;
    }

The error is in the line pstatement.setBytes(1, byteArray.toByteArray()); I have checked the length of the array and it has a correct length but I don't know what it is trying to access in order for this error to be created

cylegend
  • 163
  • 1
  • 2
  • 12

0 Answers0