Replicating Binaries with Java

1

I'm trying to replicate a binary field representing an image in Oracle. Could you tell me the best way to get this data and insert again? The field in Oracle is LONG RAW. What kind of data do you use in Java? I already tried byte and I could not. Thanks

    
asked by anonymous 09.02.2018 / 15:03

2 answers

1

Data Type and Java-to-Java Type Mappings

 SQL and PL/SQL Data Type    Oracle Mapping            JDBC Mapping

 RAW, LONG RAW               oracle.sql.RAW             byte[]

Try to map it to byte[] .

If you can java.sql.SQLException : Stream has already been closed , try setting useFetchSizeWithLongColumn = true connection properties to OJDBC driver .

View the OracleDriver API

    
09.02.2018 / 19:26
0

Here's a solution:

    String query = "SELECT NOME, IMAGEM FROM LR_TABLE WHERE ID = 1";
    Statement st = conn.createStatement();
    ResultSet rs = st.executeQuery(query);

    try {                   
        String nome = null;         
        while (rs.next()) {
            nome = rs.getString("nome");                    
            byte longRaw[] = rs.getBytes("imagem");             
            InputStream stream = new ByteArrayInputStream(longRaw);

            PreparedStatement ps = conn.prepareStatement("INSERT INTO LR_TABLE (ID, NOME, IMAGEM) VALUES (21, ?, ?)");              
            ps.setString(1, nome);
            ps.setBinaryStream(2, stream);          
            ps.execute();
            ps.close();
        }
    
11.02.2018 / 23:34