您的位置:首页 > 数据库 > Oracle

java读取mysql数据库的blob字段和Oracle数据库的Blob字段的区别【读取properties文件】

2012-02-08 12:30 399 查看
问题描述:当数据源为mysql时,执行 int len = new Long(tumPhoto.getUserphoto().length()).intValue();  正确,当数据源为oracle时,执行报异常:连接已关闭。
String base64Str = null;
byte[] currentPhoto = null;
if ((tumPhoto != null) && (!"".equals(tumPhoto.getPid().trim()))) {
SummarySrv ss = new SummarySrv();
String path = ss.getWebInfPath().substring(1)+"classes/config/config-database.properties";
java.util.Properties props =  new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(path));
props.load(in);
String value = props.getProperty("dbconn.docstore.driverName");
log.info("dbconn.docstore.driverName:"+value);
//数据源为Oracle
if(value.equals("oracle.jdbc.driver.OracleDriver")){
SerializableBlob blob = (SerializableBlob)tumPhoto.getUserphoto();
BLOB blob2 = (BLOB) blob.getWrappedBlob();
//long l =blob2.getLength();
currentPhoto = blob2.getLocator();
//数据源为MySql
}else if(value.equals("com.mysql.jdbc.Driver")){
long pos = new Long(1).longValue();
int len = new Long(tumPhoto.getUserphoto().length()).intValue();
currentPhoto = tumPhoto.getUserphoto().getBytes(pos, len);
}
} catch (Exception e) {
e.printStackTrace();
}
base64Str = new sun.misc.BASE64Encoder().encode(currentPhoto);
}
 
mysql数据库中: blob类型
 
oracle数据库中:BLOB类型
 
后台实体类:TumPhoto 的部分映射内容:
<class name="com.xbzc.phr.dao.vo.TUmPhoto" table="t_um_photo">
<id name="pid" type="java.lang.String">
<column name="PID" length="128" />
<generator class="uuid.hex" />
</id>
<property name="userphoto" type="java.sql.Blob">
<column name="USERPHOTO" />
</property>

</class>


最终解决方法:区分mysql和oracle对待。如上面的代码。
提示:prperties 属于:import java.util.Properties;


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息