1

データベースに画像を挿入しようとしていますが、photo_id は自動インクリメントされます。

私のテーブルの列は次のとおりです。

PHOTO_ID  (primary key),
USERNAME  (fkey),
PICTURE,
PICTURE_TITLE,
LIKES 

私のコードは

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;
    }
}

エラーが発生します:

java.sql.SQLException: ORA-01460: 実装されていない、または不合理な変換が要求されました
4

1 に答える 1

0

InputStream.available()ストリームのサイズを返しませ(Javadocs に明確に記載されています)。

入力ストリームの「使用可能なバイト」ではなく、ファイルのサイズを照会する必要があります。

ps.setBinaryStream(2, fis, (int)file.length());

JDBC ドライバーのバージョンによっては、使用することもできます

ps.setBinaryStream(2, fis, file.length());

ただしsetBinaryStream(int, InputStream, long)、JDBC 4.0 で定義されているため、すべてのドライバー バージョンで実装されているわけではありません。setBinaryStream(int, InputStream, int)JDBC 2.0 であり (私が間違っていなければ)、すべてのバージョンで利用できるはずです。

于 2013-01-07T10:45:30.563 に答える