Java - 尝试为SQLite使用.setBytes函数,但得到ArrayIndexOutOfBounds 0错误

时间:2018-04-16 12:28:40

标签: java sqlite jdbc blob

我正在尝试将图像作为blob插入到数据库中,首先我将图像创建为ByteArrayOutsream,然后我将其作为参数继续在我的预准备语句中使用,但是我收到了Animated错误我已经检查过数组的长度和值都是正确的所以我不知道在数组中尝试访问的位置是为了给我这个错误

这是我的代码:

java.lang.ArrayIndexOutOfBoundsException: 0

我在第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"; filePath = filePath.replace("\\", "/"); // Establishing a connection to the database String sql = "INSERT INTO Products(ID,Name,Description,Quantity,Price,Image,isAuthorised) " + "VALUES (" + quantity + ",'" + name + "','" + description + "'," + quantity + ",'" + price + "','?','0');"; try (Connection conn = DriverManager.getConnection(url)) { PreparedStatement pstatement = conn.prepareStatement(sql); File image = new File(filePath); FileInputStream input = new FileInputStream(image); ByteArrayOutputStream barray = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; for(int readNum; (readNum = input.read(buffer)) != -1;) { barray.write(buffer, 0 , readNum); } input.close(); int i = barray.toByteArray().length; byte[] array = new byte[i]; array = barray.toByteArray(); pstatement.setBytes(1, array); pstatement.executeUpdate(); success = true; }catch(Exception e) { System.out.println(e.getCause()); } return success; }

收到错误

0 个答案:

没有答案