Binary Replication with Java

Asked

Viewed 46 times

0

I’m trying to replicate a binary field that represents an image on the Oracle. Could you tell me the best way to take this data and insert it again? The field in Oracle is LONG RAW. What kind of data do you use in Java? I tried byte and I couldn’t. Thank you

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 to define useFetchSizeWithLongColumn = true the connection properties for the OJDBC driver.

See the Oracledriver API

  • Okay, cool then put the solution.

0

Below is 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();
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.