Java不处理“\\\<br/>”作为从“数据库”列检索的新行 [英] Java is not treating &quot;\n&quot; as new line when retrieved from Database column

查看:213
本文介绍了Java不处理“\\\<br/>”作为从“数据库”列检索的新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在面对一个奇怪的问题。

我正在做的是将数据库(oracle 11g)中的一些值存储为 varchar2 ,并在java中获取值获取的数据。

现在我将 \\\
作为数据库中的值,并使用 rs.getString()。我正在获得正确的价值 \\\

  String换行符= rs.getString( COLUMN_VALUE); 

现在我解析一个HTML页面,并将整个页面作为一个字符串。配置页面有3行时间:08 AM - 11 AM
持续时间:36分钟
($)

$

现在,我将通过持续时间:到一个方法,这将返回我36分钟。
我使用什么逻辑是我得到的索引持续时间:并阅读,直到遇到\\\
。如果我将换行声明为

  String newLine =\\\
;

但是如果我从数据库中获取它不起作用。
我知道我不需要将它存储在数据库中,但我不想硬编码这些项目。所以我必须将其存储在DB中。任何人都可以帮助我吗?

解决方案

如果您打印 newLine 您在输出中得到 \\\
,您可能需要取消从数据库中获取的字符串。 Apache Commons Lang有一个方法:



http://commons.apache.org/proper/commons -lang / javadocs / api-2.6 / org / apache / commons / lang / StringEscapeUtils.html#unescapeJava(java.io.Writer,java.lang.String)



所以你只需要调用

  String newLine = StringEscapeUtils.unescapeJava(rs.getString(column_value) ); 

希望这有助于


I am facing a weird problem.
What I am doing is I am storing some values in DB(oracle 11g) as varchar2 and fetching the values in java and working with the fetched data.
Now I have \n as a value in DB and getting it in java using rs.getString(). I am getting proper value \n.

String newLine=rs.getString("column_value");

Now i parse a HTML page and get the whole page as a string.Suppose the page has 3 lines each depiciting ssome informations like below:

Time: 08 AM - 11 AM
Duration : 36 minutes

Now in code i will pass "Duration :" to a method and that will return me "36 minutes".
What logic i use is i get the index of "Duration :" and read till "\n" is encountered.Code works fine if i declare the newline as

String newLine= "\n";

But if i get it from DB it does not work. I know i don't need to store this in DB but i don't want to hardcode these items. So i have to store it in DB only. Can anyone help me out in this???

解决方案

If when you print the newLine you get \n in the output, you might have to unescape the string you get from the DB. The Apache Commons Lang have a method for that:

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeJava(java.io.Writer, java.lang.String)

So you would have simply to call

 String newLine = StringEscapeUtils.unescapeJava(rs.getString("column_value"));

Hope this helps

这篇关于Java不处理“\\\<br/>”作为从“数据库”列检索的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆