如何往oracle中的blob字段写入照片数据
发布网友
发布时间:2022-04-08 20:21
我来回答
共2个回答
懂视网
时间:2022-04-09 00:42
---恢复内容开始---
1.在IDE中查看blob字段的内容可以采用:
UTL_RAW.CAST_TO_VARCHAR2(blob)的方法,其中blob为表中blob字段的列名。这个方法限定结果不可超过2000字节。
2.
更新blob时,碰到德文乱码问题,最后采用的是在转换为byte[]后,再次转换为new String(ISO-8839-1)就可以
oracle中一些关于blob字段的操作
标签:
热心网友
时间:2022-04-08 21:50
往oracle里面类型为blob写入时,必须先插入一个empty_blob,实行update……
具体java里面写入blob的代码如下:
public class applyPhotoBLOB {
final static String sDBDriver = "oracle.jdbc.driver.OracleDriver";
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection connSDC = null;
Connection conn = null;
String sConnStr = "jdbc:oracle:thin:@127 0.0 1 1521:sle";
String sConnStrSDC = "jdbc:oracle:thin:@10 10 8.12:1521:rac2";
String sDBUid = "test";
String sDBPwd = "test";
String sDBUidSDC = "sdcmanager";
String sDBPwdSdc = "sdcmanager_888";
try
{
applyPhotoBLOB apply = new applyPhotoBLOB();
connSDC = apply.getConn(sConnStrSDC,sDBUidSDC,sDBPwdSdc);
if(connSDC!=null)
{
apply.testBOLB(connSDC);
}
System.out.println("处理完成!");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
if(conn!=null) conn.close();
if(connSDC!=null) connSDC.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
public void testBOLB(Connection conn) throws Exception
{
String strSQL = "Insert Into BKS_XSZPXX(XH,ZPLXM,ZP) Values('3071801040','1',empty_blob())";
updateTable1(strSQL,conn);
conn.setAutoCommit(false);
strSQL = "Select ZP from BKS_XSZPXX where XH='3071801040' For Update";
Statement stmt = null;
ResultSet rs = null;
stmt = conn.createStatement();
rs = stmt.executeQuery(strSQL);
rs.next();
BLOB blob = (BLOB) rs.getBlob("ZP");
OutputStream os = blob.getBinaryOutputStream();// 建立输出流
BufferedOutputStream output = new BufferedOutputStream(os);
BufferedInputStream input = new BufferedInputStream(new File("F:/3071801040.jpg").toURL().openStream());
byte[] buff = new byte[2048000]; //用做文件写入的缓冲
int bytesRead;
while(-1 != (bytesRead = input.read(buff, 0, buff.length)))
{
output.write(buff, 0, bytesRead);
//System.out.println(bytesRead);
}
output.close();
input.close();
rs.close();
conn.commit();
conn.setAutoCommit(true);
stmt.close();
}
private int updateTable1(String strSQL,Connection conn) throws Exception
{
PreparedStatement stmt = null;
int result = 0;
try
{
stmt = conn.prepareStatement(strSQL);
result = stmt.executeUpdate();
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
stmt.close();
}
return result ;
}
public Connection getConn(String StrConn,String uid,String pwd) throws Exception
{
Connection conn = null;
try
{
Class.forName(sDBDriver);
conn = DriverManager.getConnection(StrConn,uid,pwd);
}
catch (Exception e)
{
throw new Exception(e.getMessage());
}
return conn;
}
}
另外:放入business里面的时候,其zp最好定义为InputStream
转载,仅供参考。