How to dump byte array to INSERT INTO SQL script (Java + PostgreSQL) -


How can I add a Postgradge SQL base to a SQL script, which contains the following Java Entity Class based data collection? The problem is how can I write the contents of the byte array as STD script on INSERT INTO value

This means that this data is sent through the Java application Can not feed, according to the requirement to populate the data base present on the production environment, some row SQL Script is needed. Thank you.

Entity class

  @Antity @Table (name = "image_table") Public class ImageData applied serializable {@Id @GeneratedValue @Column ( Name = "id") private integer ID; @column (name = "content") private byte [] content; }  

Row sql script is required to be generated

  Insert image_title (id, content) value (' 1 ',' & lt; content of byte [] & gt; '); Insert Image_table (id, content) values ​​('2', '& lt; content of byte [] & gt;'); To answer your question literally: You can write a SQL  INSERT  

scripts for an integer and a blob value, but it would be horrible to avoid strings for hex's bytes instead of you and problems with long-term statements for large blops Can run easily; PostgreSQL has no practical limit, but most regular text editors do.

As PostgreSQL 9.0 you can use the hex format (documentation). Basically, text rendering looks like hex value like e '\ x3FB5419C' etc. You can output your code from e '\ x , then byte [] as a hex string, then closing ' . The hex string can be written with org.apache.commons.codec.binary.Hex.encodeHexString (content) by byte [] content or for a plain Java Measures can be used. Depending on your production environment, you may have to save Backslash \\ and Bella's new look. I recommend that you try with a little blob to see what works for you.

A better way is direct entry assuming that you are using the PostgreSQL JDBC driver, considering that you have a byte [] class member, Instead of code> setBinary () , you should use the setBytes () method (which is the InputStream example):

  Place created ps = conn.prepareStatement (include "image_table (id, content) values ​​(?,?)"); Ps.setInteger (1, 1); Ps.setbytes (2, content); Ps.executeUpdate (); Ps.setInteger (1, 2); Ps.executeUpdate (); Ps.close ();  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -