java.sql.SQLException: ORA-01461: 只能为插入到 LONG 列而绑定 LONG 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11393786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column
提问by learner
I am using a simple interface (in jsf 1.2 and rich faces 3.3.2, Oracle 11g R1) to let user select picture with rich:fileUpload and save in a table. As a test, i created following table.
我正在使用一个简单的界面(在 jsf 1.2 和 Rich Faces 3.3.2,Oracle 11g R1 中)让用户使用 Rich:fileUpload 选择图片并保存在表格中。作为测试,我创建了下表。
CREATE TABLE TEST
(
MIME_TYPE VARCHAR2 (1000),
PHOTO BLOB,
STUDENT_ID NUMBER NOT NULL
)
code snippet to save the picture to BLOB field is as follows.
将图片保存到 BLOB 字段的代码片段如下。
//......From the uploadFile Listener
public void listener(UploadEvent event) throws Exception {
...
item = event.getUploadItem();
...
StudentPhotoDAO dao = new StudentPhotoDAO();
dao.storePhoto(item.getData(),item.getContentType(),studentId);
...
}
//......From the PhotoDAO ..........................
public void storePhoto(byte data[],String mimeType, Long studentId){
{
...
ByteArrayInputStream bis=new ByteArrayInputStream(data);
String query = "update TEST set PHOTO = ? ,MIME_TYPE = ? where STUDENT_ID=?";
pstmt = conn.prepareStatement(query);
pstmt.setAsciiStream(1,(InputStream)bis,data.length);
pstmt.setString(2,mimeType.toString());
pstmt.setLong(3,studentId);
pstmt.executeUpdate();
}
I get following error:
我收到以下错误:
java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column
Where is the error in the code please.
请问代码错误在哪里。
Thanks.
谢谢。
回答by Heiko Rupp
You specify the student_id
as number
, which seems to map to BigInteger
. See e.g. this table.
您指定student_id
as number
,它似乎映射到BigInteger
. 参见例如这张表。
Either you supply a BigInteger
or you need to change the type of student_id
.
您要么提供 aBigInteger
要么您需要更改student_id
.
回答by svaor
Look at Oracle LONG type description: "LONG is an Oracle data type for storing character data ...". So LONG is not number in Oracle. It's a text.
查看Oracle LONG 类型描述:“LONG 是一种用于存储字符数据的 Oracle 数据类型……”。所以 LONG 在 Oracle 中不是数字。这是一段文字。
I think you got this error because of this:
pstmt.setAsciiStream(1,(InputStream)bis,data.length);
我认为你因为这个而得到这个错误:
pstmt.setAsciiStream(1,(InputStream)bis,data.length);
Try use pstmt.setBinaryStream(int, InputStream, int)
or pstmt.setBinaryStream(int, InputStream, long)
.
尝试使用pstmt.setBinaryStream(int, InputStream, int)
或pstmt.setBinaryStream(int, InputStream, long)
。
回答by Michael R
When binding large binary streams to a BLOB column in Oracle, use PreparedStatement.setBlob()
rather than setAsciiStream()
or setBinaryStream()
.
将大型二进制流绑定到 Oracle 中的 BLOB 列时,请使用PreparedStatement.setBlob()
而不是setAsciiStream()
或setBinaryStream()
。
回答by leonid
I guess using ASCII stream is to blame.
我想使用 ASCII 流是罪魁祸首。
pstmt.setAsciiStream(1, (InputStream) bis, data.length);
Try
尝试
pstmt.setBinaryStream(1, new ByteArrayInputStream(data));
(It looks like Oracle interprets ASCII stream with length parameters as LONG which cannot be more than 4000 bytes. But that's only an unconfirmed guess.)
(看起来 Oracle 将长度参数的 ASCII 流解释为不能超过 4000 字节的 LONG。但这只是未经证实的猜测。)
回答by Simon Dorociak
You are calling
你在打电话
pstmt.setLong(3,studentId);
and you specified column as
并且您将列指定为
STUDENT_ID NUMBER NOT NULL
and how docs says:
以及文档如何说:
An attempt was made to insert a value from a LONG datatype into another datatype. This is not allowed.
试图将 LONG 数据类型中的值插入到另一种数据类型中。这是不允许的。
So just make it like this:
所以就让它像这样:
STUDENT_ID INTEGER NOT NULL
pstmt.setInt(3, studentId);