Well, there is no way in Java to automatically do it. So you have to use some third-party framework (maybe Hibernate would be good) or handle it yourself. You have to put to base all the important things about the object. I have done such thing myself, so if you will choose this solution and encounter some troubles maybe I could help.
Regards
Pawel
first of all, thank you for your answer, amorfis. :)
well, I don't understand when you say "You have to put to base all the important things about the object". I have obviously my class structure: are you referencing of it?
I took a look at the Hibernate API and I have a question: can I use it on a standalone Java application, without using the Tomcat context?
so, how you can see, I would handle this problem by myself. have you any code example of your experience?
thanks again! :)
Hi,
I don't know about Hibernate. I only know there is this framework and it handles some objects serialization to some database, but I have never used it. Sorry.
About handling it yourself : You just need to put to database all important attributes of your objects. For example if you have class :
public class Person {
private String m_name;
private String m_address;
public Person(String name, String address) {
...
}
public String getName() {...};
public String getAddress() {...};
}
you need to put to some table (probably PERSONS) columns NAME and ADDRESS and store this data there. So probably you need some DatabaseStore object (or however you call it) with methods:
public Person[] getPersons();
public void storePersons(Person[]);
Of course I imagine your classes are much more complicated, but I hope you catch the idea.
Regards
Pawel
Do you know JDBC and SQL? If not, I think some additional learning is inevitable ;)
Ok, first you need to instantiate database jdbc driver and get connection (try-catch statements ommited):
Class.forName("oracle.jdbc.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@<server_name>:<port_nr>:<database_alias>", <user>, <password>);
String sql = "select * from <table_name> where 1 = 2";
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rset = statement.executeQuery(sql);
for(int i = 0 ; i < 100 ; i++) {
rset.moveToInsertRow();
rset.updateInt(<column_nr>, i);
rset.updateString(<column_nr>, "R");
rset.insertRow();
}
connection.commit();
connection.commit() is not necessary if autoCommit is set to true. <column_nr> can be column number (index), or column name.
Note also, that above select statement will return empty ResultSet, because condition is (1 = 2). It is not required, but it makes sense here, because we do not want to GET any data from ResultSet, but to PUT.
Above example shows how to put data to database, not how to put Objects. So, it is up to you to extract necessary data from objects and put it to database. From my experience I know that it is convenient to create separate classes for storing separate objects types.
Good luck!
Regards
Pawel