谁有成功的使用java访问oracle中blob字段的例子?(100分)

  • 主题发起人 主题发起人 yps
  • 开始时间 开始时间
Y

yps

Unregistered / Unconfirmed
GUEST, unregistred user!
我写的代码出错:
File file=new File("f://map.gif");
java.io.FileInputStream fileint=new FileInputStream(file);
int filelength=(int)file.length();
byte buffer[]=new byte[filelength];
fileint.read(buffer,0,filelength);

rs=st.executeQuery("select photo_bin1 from pmanage_employ");
rs.next();
oracle.sql.BLOB my_blob=(oracle.sql.BLOB)rs.getBlob(1);
my_blob.setBytes(buffer);
rs.close();
oracle.jdbc.OraclePreparedStatement ops = (oracle.jdbc.OraclePreparedStatement)conn.prepareStatement("update pmanage_employ set photo_bin=?");

ops.setBLOB(1, my_blob);
ops.executeUpdate();

提示:java.sql.SQLException: ORA-01461: 仅可以为插入 LONG 列的 LONG 值赋值
 
存:
public static void setLargeTextField(PreparedStatement pstmt, int parameterIndex, String value)
throws SQLException
{
Reader bodyReader = null;
try
{
bodyReader = new StringReader(value);
pstmt.setCharacterStream(parameterIndex, bodyReader, value.length());
}
catch(Exception e)
{
e.printStackTrace();
throw new SQLException("Failed to set text field.");
}
}
}
取:
public static String getLargeTextField(ResultSet rs, int columnIndex)
throws SQLException
{
Reader bodyReader = null;
String value = null;
try
{
bodyReader = rs.getCharacterStream(columnIndex);
if(bodyReader == null)
{
String s = null;
return s;
}
char buf[] = new char[256];
StringWriter out = new StringWriter(256);
int i;
while((i = bodyReader.read(buf)) >= 0)
out.write(buf, 0, i);
value = out.toString();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
throw new SQLException("Failed to load text field");
}
finally
{
try
{
bodyReader.close();
}
catch(Exception exception1) { }
}
return value;
}
}
 
后退
顶部