java如何将sqlserver中的image转换成oracle中的BLOB

0
Java SQL Oracle C/C++ 16718 次浏览

各位大虾,请问java如何将sqlserver中的image转换成oracle中的BLOB,谢谢!

请尽量让自己的答案能够对别人有帮助

10个答案

默认排序 按投票排序
0

学到很多东西

0
代码视图排版不太美观啊,基本思路应该是先读取image类型,然后在以二进制存入oracle的blob
0
排版不行啊
0
好文章!学习了
0
花了2天才搞定java链接数据库啊 谢谢楼主
数据库/sql server   有蛮多要学哦
0

谢谢大侠

0
把我上面这两个代码结合一下,就能够解决你的问题。
0
再加上-通过java往oracle中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;
    }

}
0
转—>JDBC读取写入SQLServer的Blob(image)字段数据的代码:
import java.sql.*;   
import java.io.*;   

/** 
* JDBC读取写入SQLServer的Blob(image)字段数据的代码. 
*   
* @author 老紫竹研究室(laozizhu.com) 
*   
*/ 
public class Test {   
// 注意下面是jdbc2.0的驱动写法,3.0的请自行查找   
static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";   
static String url = "jdbc:microsoft:sqlserver://192.168.0.202:9999999999;DatabaseName=dddd";   
static String user = "sa";   
static String passwd = "ps";   

public static void method1() throws Exception {   

    Connection conn = null;   
    try {   
      Class.forName(driver);   
      conn = DriverManager.getConnection(url, user, passwd);   
      int op = 0;   
      // 插入   
      if (op == 0) {   
        PreparedStatement ps = conn   
            .prepareStatement("insert   into   tb_file   values   (?,?)");   
        ps.setString(1, "aaa.exe");   
        InputStream in = new FileInputStream("d:/aaa.exe");   
        ps.setBinaryStream(2, in, in.available());   
        ps.executeUpdate();   
        ps.close();   
      } else {   
        // 取出   
        PreparedStatement ps = conn   
            .prepareStatement("select   *   from     tb_file   where   filename   =   ?");   
        ps.setString(1, "aaa.exe");   
        ResultSet rs = ps.executeQuery();   
        rs.next();   
        InputStream in = rs.getBinaryStream("filecontent");   
        System.out.println(in.available());   
        FileOutputStream out = new FileOutputStream("d:/bbb.exe");   
        byte[] b = new byte[1024];   
        int len = 0;   
        while ((len = in.read(b)) != -1) {   
          out.write(b, 0, len);   
          out.flush();   
        }   
        out.close();   
        in.close();   
        rs.close();   
        ps.close();   
      }   
    } catch (Exception ex) {   
      ex.printStackTrace(System.out);   
    } finally {   
      try {   
        conn.close();   
      } catch (Exception ex) {   
      }   
    }   
}   

public static void method2() {   
    String driver = "org.gjt.mm.mysql.Driver";   
    String url = "jdbc:mysql://localhost:3306/exercise";   
    // String url=   
    // "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";   
    String user = "root";   
    String password = "roger";   
    try {   
      Class.forName(driver);   
    } catch (ClassNotFoundException e) {   
      System.out.println("ClassNotFoundException   ->" + e);   
    }   
    try {   
      Connection conn = DriverManager.getConnection(url, user, password);   
      PreparedStatement pstmt = conn   
          .prepareStatement("select   *   from   picture   where   picturename=?");   
      pstmt.setString(1, "z0011.jpg");   
      ResultSet res = pstmt.executeQuery();   
      res.beforeFirst();   
      while (res.next()) {   
        Blob blob = res.getBlob(2);   
        try {   
          FileOutputStream fout = new FileOutputStream("z0018.jpg");   
          fout.write(blob.getBytes(1, (int) blob.length()));   
          System.out.println("z0011.jpg   file   size   ->" 
              + (int) blob.length());   
          fout.flush();   
          fout.close();   
        } catch (IOException e) {   
          System.out.println("create   jpg   file   exception   ->" + e);   
        }   
      }   
      conn.close();   
    } catch (SQLException e) {   
      System.out.println("SQLException   ->" + e);   
    }   
}   
} 
0

我的想法是先将sqlserver中的image存成图片文件,放在文件系统中。然后再用java读取图片文件,最后保存到oracle中。