convert String to Clob and vice versa in Hibernate

后端 未结 3 1894
北海茫月
北海茫月 2021-01-05 04:30

Suppose that I have a Class:

class EventTransaction {
    .....
    private Clob dataXML;

    public Clob getDataXML() {
       return dataXML;
    }

    p         


        
3条回答
  •  别那么骄傲
    2021-01-05 04:47

    Here is code I made a long time ago to convert a Clob to a String. It's meant to be used in a utility class.

        public static String convertClobToString(Clob clob) throws IOException, SQLException {
                Reader reader = clob.getCharacterStream();
                int c = -1;
                StringBuilder sb = new StringBuilder();
                while((c = reader.read()) != -1) {
                     sb.append(((char)c));
                }
    
                return sb.toString();
         }
    

    And if I am not mistaken, to create a Clob you would do something like this

         Clob myClobFile = new SerialClob("my string".toCharArray());
    

提交回复
热议问题