0

I have in my Entity class a field which is a CLOB type which represents a column in my Oracle. database. I want to write into this field a object from this class:

class Person{
    String firstName;
    String lastName;
    Integer age;
    // getters, setters, constructors
}

Something like this:

Person personObject = new Person();
Clob myClob = saveAsClob(personObject);
// save it into Oracle DB

How to achieve something like this? Convert my object into JSONObject and then save it as String? Or maybe is there any other way?

chebad
  • 919
  • 1
  • 13
  • 29

1 Answers1

0

I don't know what you mean by archive, but if you want to archive it in a json file, it will be like that:

ObjectMapper mapper = new ObjectMapper();
Person obj = new Person();

//Object to JSON in file, you can change the link
mapper.writeValue(new File("c:\\file.json"), obj);

//Object to JSON in String
String jsonInString = mapper.writeValueAsString(obj);
Dev web
  • 81
  • 1
  • 13
  • My bad. I want something like this, here is this flow: createPersonObj --> createClobObj --> (put into `OracleDB`) --> getClobObj --> getPersonObj. When I said archive - I thought about `to achieve`. There was my typo, sorry. – chebad Jul 10 '18 at 11:02
  • in this case I think you have to pass by a table, you can create a function like that public void insertClobInDataBase(Person obj){ con = DriverManager.getConnection(.....); String sql = "insert into table (attribut) values (?); PreparedStatment psmt = con.prepareStatement(sql); psmt.setClob(1, obj.get...) //getter used in the class int rs =psmt.executeUpdate(); then you can call it everytime you have a new Clob type, but in your case I don't think so, for a person you can use the 3 attributs – Dev web Jul 10 '18 at 11:04
  • Ok I know it, but I don't know how to convert a `Person` object into `Clob` object. Do you know maybe solution for that? – chebad Jul 10 '18 at 11:07
  • check this one https://stackoverflow.com/questions/44411710/converting-object-to-clob – Dev web Jul 10 '18 at 11:08