What I need to do (And I know it's bad practice, but I'm kind of forced to) is upload an image to an SQL DB from Java. Currently, I'm using a prepared statement and trying to use this to upload the bytes from the image.
public void insertImage(Connection conn,String img,String strItemNum)
{
String query;
PreparedStatement pstmt;
try
{
File file = new File(img);
FileInputStream fis = new FileInputStream(file);
byte[] image = new byte[(int) file.length()];
fis.read(image);
System.out.println("image as sent " + image.length);
query = ("SELECT [Item Picture] from [Inventory] where [Item Number] = '" + strItemNum + "'");
pstmt = conn.prepareStatement(query);
System.out.println(pstmt.getMetaData().getColumnName(1) + " of type: " + pstmt.getMetaData().getColumnTypeName(1));
pstmt.setBytes(1,image);
pstmt.executeUpdate();
pstmt.close();
}
catch (IOException | SQLException e)
{
System.err.println(";"+e.getMessage());
e.printStackTrace();
}
}
But this is yielding an SQLException: Invalid parameter index 1. The parameter in my table is of type "image" and I can't get anything to stick. I've tried using .setBlob but from my google research, it appears that Blobs were never implemented very well.
EDIT: Solved by using AVD's answer
changed to
query = ("Update [Inventory] set [Item Picture] = ? where [Item Number] = ?");
pstmt = conn.prepareStatement(query);
pstmt.setBytes(1,image);
pstmt.setString(2, strItemNum);
pstmt.executeUpdate();
pstmt.close();