Preserving line breaks when updating and reading from Oracle DB BLOB

和自甴很熟 提交于 2019-12-11 08:42:09

问题


Here is what I am trying to do:

-in Java, read a text file and create a String (preserving line breaks)

-display that String in an SQL BLOB Update (or Insert) statement in the console (Eclipse), but only on one line (can't render the line breaks in the console)

-copy/paste the SQL that printed in the console to Toad and execute

then,

-save the newly updated BLOB from Oracle to a file from Toad and have it render the line breaks

Here is what i've tried so far:

Here is where I read the file-

File file = new File(pathToFile);
fileIn = new FileReader(pathToFile);
BufferedReader br = new BufferedReader(fileIn);
fileContents = new StringBuilder((int) file.length());
String line;
try {
    while ((line = br.readLine()) != null) {
    fileContents.append(line).append("\\n");
} ...

...
return fileContents.toString();

So far, so good. I now have a String that I can put in an SQL statement. So now I create the SQL statement like this-

String myStatement = "UPDATE ...... SET CONTENT = RAWTOHEX('" + myReturnedFileContentsString + "')...

I then print myStatement to the console so I can copy/paste the SQL into Toad (I know I can just programmitcally do it, but I need to do it this way). The statement string prints correctly as one line with \n within the string.

I then copy/paste the SQL into Toad, execute the command,... and it loads the String as a BLOB in the DB just fine.

Next, after refreshing the table, I save the BLOB to a text file, then open that file. And... that's where I start failing. The text renders with the \n in the text. No new lines... it doesn't look like the file I read from to create the statement. I've tried using REGEXP_REPLACE within the RAWTOHEX section to replace the \n with \r\n. I've tried opening the file in different editors and so on...

What am I missing? Is this possible?

Any help is much appreciated.

EDIT

What I'm trying to accomplish:

I have a table in Oracle DB that has the following 2 columns:

-FILE_NAME (primary key)

-CONTENT (BLOB)

Let's just pretend like this table will only ever have one row. I am writing a Java program that will grab the BLOB and covert it to a String. Let's call that String String_Old. Next, the program will read a file and convert its contents to a String. Let's call that String String_New. The program will then compare the two Strings, and if they are different, it will generate SQL to update the CONTENT column in the table with a new Blob created from String_New.

Let's pretend like if we converted the CONTENT Blob in the table right now to a String, it would be the value "a". Now, our file (text file) looks like this:

<html>
 <body>
  <p>Hi, I am Paragraph 1</p>
  <p>Hi, I am Paragraph 2</p>
 </body>
</html>

So I need to read that file and convert it to a String, compare that to "a", and since it's different, I need to generate the SQL, and print it to the console (Eclipse), to update the CONTENT column for the row in the table with a new Blob created from String_New.

So, if I use the approach to read the file that I put in my original answer, but append just \n after reading each line, compare, then create the SQL, I end up with this being printed to the console:

UPDATE table SET CONTENT = RAWTOHEX('<html>
 <body>
  <p>Hi, I am Paragraph 1</p>
  <p>Hi, I am Paragraph 2</p>
 </body>
</html>
') WHERE ....

Okay, so let's ignore a requirement for now and roll with what we have. So, I copy this output in the console, paste it in Toad, execute, and it updates the BLOB. So far so good.

Now, I need to be able to double click that BLOB in the table (within Toad) save it to a file, open the file, and see it as it appeared in the file we read in (it should have all the line breaks and tabs, spaces, etc.).

Given the console output we just copy/pasted/executed, if I load that Blob to a file from the DB, it does indeed look the way I need it to. So, if I was cool with the console output looking that way (with line breaks), I think I'm done. However, I can't have the console SQL output have multiple lines. I need that same SQL statement to look like this in the console (with whatever else might need to be there in order to accomplishment my requirement):

UPDATE table SET CONTENT = RAWTOHEX('<html><body><p>Hi, I am Paragraph 1</p><p>Hi, I am Paragraph 2</p></body></html>') WHERE ...

Basically, it is all on one line in the console. But I still need to be able to copy this SQL, paste/execute in Toad, and then when I save the Blob from the DB to a file and open it, look like the file I read in in the first place (ie, all spaces, tabs, line breaks, etc.)

So now I have that 2 paragraph HTML stored as a Blob in the DB. Now let's pretend I change my file to look like this:

<html>
 <body>
  <p>Hi, I am Paragraph 1</p>
  <p>Hi, I am Paragraph 2</p>
  <p>Hi, I am Paragraph 3</p>
 </body>
</html>

I now need to be able to properly load the old 2 paragraph HTML from the DB, compare it to the file's new 3 paragraph HTML, and generate a single line SQL statement in Eclipse that will update the Blob in the DB and when loaded to file from DB preserves all breaks, spaces, tabs, etc.

I hope this helps. I am a bit vexed with this problem, so if I need to clarify any further, I will be more than happy to.

来源:https://stackoverflow.com/questions/25416475/preserving-line-breaks-when-updating-and-reading-from-oracle-db-blob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!