01: package com.completex.objective.persistency.examples.ex001;
02:
03: import com.completex.objective.components.log.Log;
04: import com.completex.objective.components.persistency.PersistentObject;
05: import com.completex.objective.components.persistency.core.adapter.DefaultPersistencyAdapter;
06: import com.completex.objective.components.persistency.transact.Transaction;
07: import com.completex.objective.persistency.examples.ExamplesHelper;
08:
09: import java.io.FileInputStream;
10: import java.io.IOException;
11: import java.sql.SQLException;
12: import java.util.Properties;
13:
14: /**
15: * @author Gennady Krizhevsky
16: */
17: public class HelloOdal {
18:
19: public static String configPath = ExamplesHelper.configPath(
20: HelloOdal.class, "hsql.properties");
21:
22: public static void main(String[] args) throws IOException,
23: SQLException {
24: if (args.length > 0) {
25: configPath = args[0];
26: }
27: configPath = ExamplesHelper.resolveConfigPath(configPath);
28: Properties properties = new Properties();
29: properties.load(new FileInputStream(configPath));
30: DefaultPersistencyAdapter persistency = new DefaultPersistencyAdapter(
31: properties, Log.NULL_LOGGER);
32: //
33: // Open transaction:
34: //
35: Transaction transaction = persistency.getTransactionManager()
36: .begin();
37: //
38: // Create table and insert data:
39: //
40: persistency
41: .executeUpdate("CREATE TABLE EX_HELLO_TABLE (NAME VARCHAR(30))");
42: persistency
43: .executeUpdate("INSERT INTO EX_HELLO_TABLE VALUES ('Hello ODAL!')");
44: //
45: // Retrieve data:
46: //
47: PersistentObject po = (PersistentObject) persistency
48: .selectFirst(persistency.getQueryFactory().newQuery(
49: "SELECT * FROM EX_HELLO_TABLE"));
50: printResult(po);
51: //
52: // Drop table:
53: //
54: persistency.executeUpdate("DROP TABLE EX_HELLO_TABLE");
55: //
56: // Commit transaction returning it to the pool:
57: //
58: persistency.getTransactionManager().commit(transaction);
59: }
60:
61: private static void printResult(PersistentObject po) {
62: System.out
63: .println(" Congratulations: your 1st data retrieved from the database: by index: "
64: + po.record().getString(0));
65: System.out
66: .println(" Congratulations: your 1st data retrieved from the database: by name : "
67: + po.record().getString("NAME"));
68: }
69: }
|