I'm trying to insert an image into database and with auto increment photo_id.
my table columns are:
PHOTO_ID (primary key),
USERNAME (fkey),
PICTURE,
PICTURE_TITLE,
LIKES
my code is
public class UploadPhotoDao {
public int insertPhoto(UploadPhotoBean uploadBean){
Connection con = null;
PreparedStatement ps = null;
int index = 0;
final String query = "INSERT INTO PHOTOS_DETAILS (PHOTO_ID,USERNAME,PICTURE,PICTURE_TITLE,LIKES) VALUES (PHOTO_ID_SEQUENCE.nextval,?,?,?,?)";
try {
con = ConnectionUtil.getConnection();
ps = con.prepareStatement(query);
ps.setString(1, uploadBean.getUsername());
File file =new File(uploadBean.getPicture());
FileInputStream fis = new FileInputStream(file);
ps.setBinaryStream(2, fis, fis.available());
ps.setString(3, uploadBean.getPictureTitle());
ps.setInt(4, new Integer(0));
index = ps.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
ConnectionUtil.closeQuitly(ps);
ConnectionUtil.closeQuitly(con);
}
return index;
}
}
I'm getting the error:
java.sql.SQLException: ORA-01460: unimplemented or unreasonable conversion requested
InputStream.available()
does not return the size of the stream (which is clearly documented in the Javadocs).You will need to query the size of the file, not the "available bytes" in the inputstream:
depending on the version of your JDBC driver you can also use
But
setBinaryStream(int, InputStream, long)
is defined in JDBC 4.0 and thus not implemented by all driver versions.setBinaryStream(int, InputStream, int)
is JDBC 2.0 (if I'm not mistaken) and should be available in all versions.