简体   繁体   English

从Sybase插入/检索文件

[英]Inserting/Retrieving a file from Sybase

I am trying to insert a file containing into a Sybase database. 我正在尝试将包含的文件插入Sybase数据库。 I am using JConnect-7.0.7 我正在使用JConnect-7.0.7

Here is my table: 这是我的桌子:

CREATE TABLE blob_test (  
blobVar IMAGE  
)

I am inserting the image using: 我正在使用以下图片插入图片:

String sql = "INSERT INTO blob_test VALUES (convert(binary,?))";
PreparedStatement stmt = conn.prepareStatement(sql);
File image = new File(filePath);
InputStream fis = new FileInputStream(image);
int ilen=(int) image.length();
stmt.setBinaryStream(1, fis, ilen);
stmt.execute();

And trying to retrieve the image using: 并尝试使用以下方法检索图像:

JdbcTemplate jdbcTemplate= new JdbcTemplate(dataSource);
List<Blob> result = jdbcTemplate.query(
         "SELECT blobVar FROM blob_test", new Object[] {}, new RowMapper() {  
                @Override
                public Blob mapRow(ResultSet rs, int rowNum) throws SQLException {
                    return rs.getBlob(1);
                }
            });

for(int i=0;i<result.size();i++){
    Blob b = result.get(i);
    BufferedOutputStream os;

    os = new BufferedOutputStream(new FileOutputStream(new File("output"+i+".zip")));
    os.write(b.getBytes(1, (int) b.length()));
    os.flush();
    os.close();
 }

However, when I execute my code it outputs 1kb zip files that are corrupted. 但是,当我执行代码时,它会输出1kb损坏的zip文件。 My code works exactly as it should for an oracle database. 我的代码与oracle数据库完全一样。

Alternatively, I have also tried this code to retrieve the file: 另外,我也尝试使用以下代码来检索文件:

JdbcTemplate jdbcTemplate= new JdbcTemplate(dataSource);
List<InputStream> result = jdbcTemplate.query(
         "SELECT * FROM blob_test", new Object[] {}, new RowMapper() {
                @Override
                public InputStream mapRow(ResultSet rs, int rowNum) throws SQLException {
                    return lobHandler.getBlobAsBinaryStream(rs, 1);
                }
            });

for(int i=0;i<result.size();i++){
   InputStream in = result.get(i);
   OutputStream out = new FileOutputStream(new File("outputTest"+i+".zip"));
   byte[] buff = new byte[4096];
   int len = 0;

   while ((len = in.read(buff)) != -1) {
       out.write(buff, 0, len);
   }

   out.flush();
   out.close();
}

However, when I run this code I get an exception: 但是,当我运行此代码时,出现异常:

java.io.IOException: JZ0I9: This InputStream was closed.
    at com.sybase.jdbc4.jdbc.ErrorMessage.raiseIOException(Unknown Source)  at com.sybase.jdbc4.jdbc.ErrorMessage.raiseIOException(Unknown Source)
    at com.sybase.jdbc4.jdbc.RawInputStream.checkMe(Unknown Source)
    at com.sybase.jdbc4.jdbc.RawInputStream.read(Unknown Source)
    at com.sybase.jdbc4.jdbc.RawInputStream.read(Unknown Source)
    at blobtester.BlobTester.getOracleBlob(BlobTester.java:86)
    at blobtester.BlobTester.main(BlobTester.java:34)

Again, this works for oracle. 同样,这适用于oracle。 Is there anything I'm missing that is different about Sybase? 我缺少与Sybase不同的东西吗? Is the file being saved/retrieved correctly? 是否正确保存/检索文件?

edit: It turns out the problem was inserting the file into Sybase. 编辑:原来问题出在将文件插入Sybase。 I changed the code to: 我将代码更改为:

File image = new File(filePath); 
InputStream fis = new FileInputStream(image);
LobHandler lobHandler = new DefaultLobHandler();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 
jdbcTemplate.update("INSERT INTO blob_test VALUES (?)", new Object[] {new SqlLobValue(fis, (int)image.length(), lobHandler)}, new int[] {Types.BLOB});

And it worked for both Sybase and oracle. 而且它适用于Sybase和oracle。 Thanks. 谢谢。

Code from this link worked: http://forum.spring.io/forum/spring-projects/data/9931-sybase-and-blob-storing 此链接中的代码有效: http : //forum.spring.io/forum/spring-projects/data/9931-sybase-and-blob-storing

[Just adding an answer, to close the question.] [只需添加答案,即可结束问题。]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM