如何往oracle中的blob字段写入照片数据
展开全部
往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:sledu";
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
转载,仅供参考。
具体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:sledu";
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
转载,仅供参考。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
今至电子科技有限公司
2024-08-23 广告
2024-08-23 广告
数据库备份是确保数据安全与业务连续性的关键环节。我们上海今至电子科技有限公司高度重视数据保护,定期执行全面的数据库备份策略。这包括使用先进工具和技术,对关键业务数据进行自动化备份,并存储在安全可靠的外部存储介质或云端。通过定期验证备份的完整...
点击进入详情页
本回答由今至电子科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |