我正在尝试将图像作为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;
}
行