1

I am trying to make a method that converts an array of characters (content) into a BLOB, and updates the database system accordingly. This is my code so far:

public void setFileContent(String fileName, char[] content) {

        // BLOB b = char[] content

        String sql = "UPDATE tbl_blob SET file_content=??? WHERE file_name=\"" + fileName + "\"";

}

What do I have to do in order to convert an array of characters into a BLOB?

Many thanks.

Kurtiss
  • 495
  • 2
  • 7
  • 22

2 Answers2

1

There is something like a BLOB, look at this post, maybe it helps you.

Blob to string and string to blob

But you could also make a the char[] to a String and then to a byte[] to write it in the BLOB

Community
  • 1
  • 1
Baalthasarr
  • 377
  • 1
  • 13
  • Could I convert the char[] into a String, and then convert it into a byte[] array, and then use that to make and save the BLOB? – Kurtiss Jun 06 '14 at 10:12
  • why do you use blob in the first place? If i see it right you try to save a char[] in the DB, why can't you use a varchar insted of a BLOB? – Baalthasarr Jun 06 '14 at 10:15
  • Its how the system I'm implementing was designed; I should ask if it is something we could change. – Kurtiss Jun 06 '14 at 10:23
  • So the idea is to convert the char[] to a String, then use that String to create a BLOB? – Kurtiss Jun 06 '14 at 10:25
  • i have found something with a blob in java, i edited my answer with the link. – Baalthasarr Jun 06 '14 at 10:26
  • Based on your second comment? Would CLOB be any better? – Kurtiss Jun 06 '14 at 10:39
  • If you will write Strings or char[] in the column regulary then CLOB will we better than BLOB because CLOB has already Character encoding while BLOB dosn't have. – Baalthasarr Jun 06 '14 at 10:43
  • OK so I need to convert the char[] into a String, then into a CLOB. – Kurtiss Jun 06 '14 at 10:45
  • if the Column is a CLOB you can do it, but your column is a BLOB so you have to convert it into a BLOB. – Baalthasarr Jun 06 '14 at 10:47
  • Yeah I know it means that the column's type will have to be changed, as well as how the system reads and writes the files, a lot of stuff to do xp – Kurtiss Jun 06 '14 at 10:49
1

Even if it would be better to use a CLOB column to store text, it is perfectly possible to use a BLOB column as well. Since the BLOB column stores binary data (as opposed to CLOB, which stores text), you have to agree on an encoding.

Assuming that you will use UTF-8 to encode your text, you can use a PreparedStatement and set the new value and selection criteria using the setter methods like this:

PreparedStatement pstat = 
    conn.prepareStatement("UPDATE tbl_blob SET file_content=? WHERE file_name=?");

pstat.setBlob(1, new ByteArrayInputStream(new String(content).getBytes("UTF-8")));
pstat.setString(2, myFileName);
pstat.executeUpdate();
jarnbjo
  • 33,923
  • 7
  • 70
  • 94