2016-12-01 135 views
0

我試圖使用vert.x插入BLOB到Oracle數據庫中,我得到的上傳文件如何使用Vert.x將Blob插入到Oracle數據庫11g中?

 for (FileUpload f : routingContext.fileUploads()){ 
     System.out.println("file name " + f.fileName()); 
     System.out.println("size name " + f.size()); 
     System.out.println("Uploaded File " + f.uploadedFileName()); 
    } 

我以轉換文件上傳到字節數組:

Buffer fileUploaded = routingContext.vertx().fileSystem().readFileBlocking(f.uploadedFileName()); 

byte[] fileUploadedBytes = fileUploaded.getBytes(); 

現在我想直接將它插入Oracle數據庫,我試圖使用updateWithParams,但我不知道如何將Blob添加到查詢參數中。 感謝您的幫助

+0

看一看這個[問題](http://stackoverflow.com/questions/8348427/how-to-write-update-oracle-blob-in-a-reliable-way)。它解釋瞭如何通過jdbc在oracle db中插入Blob – ZeusNet

+0

謝謝@ZeusNet的回答,但是我使用'JDBCClient Vertx',所以我必須使用'queryWithParams'來創建'preparedStatement'然後我不能調用'setBinaryStream'。 – OLH

+0

但你可能可以將文件的字節包裝到JsonArray中?我不熟悉vert.x,但我會試一試 – ZeusNet

回答

1

這是我的實現來解決我的問題,現在我可以將文件Blob插入到Oracle數據庫中,我希望這將有助於未來的人。

ByteArrayInputStream finalBis = bis; 
    byte[] finalFileUploadedBytes = fileUploadedBytes; 
    DB.getConnection(connection -> { 
     if (connection.succeeded()) { 
      CallableStatement stmt = null; 
      try { 
       stmt = connection.result().getConnection().prepareCall(SQL.INSERT_DOCS_QUERY); 
       stmt.setBinaryStream(1, finalBis, finalFileUploadedBytes.length); 
       stmt.setString(2,desiDoc); 
       stmt.setString(3,sourDoc); 
       logger.debug(stmt); 
       stmt.execute(); 
       finalBis.close(); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      System.out.println("nooot ok"); 
     } 
    }); 
相關問題