从一个表中检索BLOB内容并将其存储在另一个表中

时间:2018-10-08 07:05:12

标签: java blob

我有一个带有斑点字段的表。我需要将该表的数据复制到另一个表中。谁能告诉我在复制时如何处理Blob数据。

for(int i=0;i<jsonarr.length();i++)
{
    JSONObject jsonO=jsonarr.getJSONObject(i);  

    if(jsonO.has("file_content") && jsonO.getString("file_content")!="null")
    {
         PS.setBlob(4, (InputStream) jsonO.get("file_content"));  //Getting Error on this line
    }
}

json数据示例

{

    "territory_id": 10111,
    "file_name": "page.html",
    "file_size": 970,
    "file_content": "UEdoMGJXdytEUW9OQ2p4b1pXRmtQZzBLUEcxbGRHRWdhSFIwY0MxbGNYVnBkajFEYjI1MFpXNTBMVlI1Y0dVZ1kyOXVkR1Z1ZEQwaWRHVjRkQzlvZEcxc095QmphR0Z5YzJWMFBYZHBibVJ2ZDNNdE1USTFNaUkrRFFvOGJXVjBZU0J1WVcxbFBVZGxibVZ5WVhSdmNpQmpiMjUwWlc1MFBTSk5hV055YjNOdlpuUWdWMjl5WkNBeE5TQW9abWxzZEdWeVpXUXBJajROQ2p4emRIbHNaVDROQ2p3aExTME5DaUF2S2lCR2IyNTBJRVJsWm1sdWFYUnBiMjV6SUNvdkRRb2dRR1p2Ym5RdFptRmpaUTBLQ1h0bWIyNTBMV1poYldsc2VUb2lRMkZ0WW5KcFlTQk5ZWFJvSWpzTkNnbHdZVzV2YzJVdE1Ub3lJRFFnTlNBeklEVWdOQ0EySURNZ01pQTBPMzBOQ2tCbWIyNTBMV1poWTJVTkNnbDdabTl1ZEMxbVlXMXBiSGs2UTJGc2FXSnlhVHNOQ2dsd1lXNXZjMlV0TVRveUlERTFJRFVnTWlBeUlESWdOQ0F6SURJZ05EdDlEUW9nTHlvZ1UzUjViR1VnUkdWbWFXNXBkR2x2Ym5NZ0tpOE5DaUJ3TGsxemIwNXZjbTFoYkN3Z2JHa3VUWE52VG05eWJXRnNMQ0JrYVhZdVRYTnZUbTl5YldGc0RRb0plMjFoY21kcGJpMTBiM0E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09-11T09:56:01Z"

}

2 个答案:

答案 0 :(得分:0)

您可以这样做:

if(jsonO.has("file_content") && jsonO.getString("file_content")!="null"){
    String fileContent = jsonO.getString("file_content"); 
    PS.setBlob(4, new ByteInputStream(fileContent.getBytes("UTF-8")));  
}

或:

if(jsonO.has("file_content") && jsonO.getString("file_content")!="null"){
    String fileContent = jsonO.getString("file_content");
    PS.setBlob(4, new SerialBlob(fileContent.getBytes("UTF-8")));  
}

答案 1 :(得分:0)

如果出现任何问题,请发送至,因为您的代码注释是此写信

假设您要将数据保存到另一个表中,这是一个示例

for(int i=0;i<jsonarr.length();i++)
{
JSONObject jsonO=jsonarr.getJSONObject(i);  

if(jsonO.has("file_content") && jsonO.getString("file_content")!="null")
{
     PS.setBlob(4, (InputStream) jsonO.get("file_content")); 
     JDBCUtil.saveDate(PS);//saveData method execute SQL 
}
}

希望我能帮助您