問(wèn)題描述
我正在嘗試將圖像插入數(shù)據(jù)庫(kù)并使用自動(dòng)增量 photo_id.
我的表格列是:
PHOTO_ID(主鍵),用戶名 (fkey),圖片,PICTURE_TITLE,喜歡
我的代碼是
公共類 UploadPhotoDao {公共 int insertPhoto(UploadPhotoBean uploadBean){連接 con = null;PreparedStatement ps = null;整數(shù)索引 = 0;最終字符串查詢=插入照片詳細(xì)信息(PHOTO_ID,用戶名,圖片,圖片標(biāo)題,喜歡)值(PHOTO_ID_SEQUENCE.nextval,?,?,?,?)";嘗試 {con = ConnectionUtil.getConnection();ps = con.prepareStatement(查詢);ps.setString(1, uploadBean.getUsername());文件文件 =new File(uploadBean.getPicture());FileInputStream fis = new FileInputStream(file);ps.setBinaryStream(2, fis, fis.available());ps.setString(3, uploadBean.getPictureTitle());ps.setInt(4, 新整數(shù)(0));index = ps.executeUpdate();}捕捉(SQLException e){e.printStackTrace();} 捕捉(FileNotFoundException e){//TODO 自動(dòng)生成的 catch 塊e.printStackTrace();} 捕捉(IOException e){//TODO 自動(dòng)生成的 catch 塊e.printStackTrace();}最后 {ConnectionUtil.closeQuitly(ps);ConnectionUtil.closeQuitly(con);}回報(bào)指數(shù);}}
我收到了錯(cuò)誤:
<上一頁(yè)>java.sql.SQLException: ORA-01460: 請(qǐng)求未實(shí)現(xiàn)或不合理的轉(zhuǎn)換InputStream.available()
不返回的大小流(Javadocs 中有明確記錄).
您需要查詢文件的大小,而不是輸入流中的可用字節(jié)":
ps.setBinaryStream(2, fis, (int)file.length());
根據(jù)您的 JDBC 驅(qū)動(dòng)程序版本,您也可以使用
ps.setBinaryStream(2, fis, file.length());
但 setBinaryStream(int, InputStream, long)
是在 JDBC 4.0 中定義的,因此并非所有驅(qū)動(dòng)程序版本都實(shí)現(xiàn).setBinaryStream(int, InputStream, int)
是 JDBC 2.0(如果我沒(méi)記錯(cuò)的話),應(yīng)該在所有版本中都可用.
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:
ps.setBinaryStream(2, fis, (int)file.length());
depending on the version of your JDBC driver you can also use
ps.setBinaryStream(2, fis, file.length());
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.
這篇關(guān)于試圖將圖像插入數(shù)據(jù)庫(kù)得到 ORA-01460:未實(shí)現(xiàn)或不合理的轉(zhuǎn)換請(qǐng)求錯(cuò)誤的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!