01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: Basic.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package database;
09:
10: import com.uwyn.rife.database.*;
11:
12: import com.uwyn.rife.database.exceptions.DatabaseException;
13: import com.uwyn.rife.database.queries.CreateTable;
14: import com.uwyn.rife.database.queries.DropTable;
15: import com.uwyn.rife.database.queries.Insert;
16: import com.uwyn.rife.database.queries.Select;
17: import com.uwyn.rife.rep.Rep;
18: import com.uwyn.rife.resources.ResourceFinderClasspath;
19: import java.sql.ResultSet;
20: import java.sql.SQLException;
21:
22: public class Basic extends DbQueryManager {
23: public Basic(Datasource datasource) {
24: super (datasource);
25: }
26:
27: public static void main(String[] args) {
28: Rep.initialize("rep/unittests_participants.xml");
29:
30: Basic example = new Basic(Datasources.getRepInstance()
31: .getDatasource("unittestspgsql"));
32: try {
33: example.doIt();
34: } catch (DatabaseException e) {
35: e.printStackTrace();
36: }
37: }
38:
39: public void doIt() throws DatabaseException {
40: CreateTable create = new CreateTable(getDatasource());
41: create.table("example").column("firstname", String.class, 50)
42: .column("lastname", String.class, 50);
43: executeUpdate(create);
44:
45: Insert insert = new Insert(getDatasource());
46: insert.into(create.getTable()).fieldParameter("firstname")
47: .fieldParameter("lastname");
48: DbPreparedStatement insert_stmt = getConnection()
49: .getPreparedStatement(insert);
50: insert_stmt.setString("firstname", "John");
51: insert_stmt.setString("lastname", "Doe");
52: insert_stmt.executeUpdate();
53: insert_stmt.clearParameters();
54: insert_stmt.setString("firstname", "Jane");
55: insert_stmt.setString("lastname", "TheLane");
56: insert_stmt.executeUpdate();
57:
58: Select select = new Select(getDatasource());
59: select.from(create.getTable()).orderBy("firstname");
60: DbStatement select_stmt = executeQuery(select);
61: Processor processor = new Processor();
62: while (fetch(select_stmt.getResultSet(), processor)
63: && processor.wasSuccessful()) {
64: System.out.println(processor.getFirstname() + " "
65: + processor.getLastname());
66: }
67:
68: DropTable drop = new DropTable(getDatasource());
69: drop.table(create.getTable());
70: executeUpdate(drop);
71: }
72:
73: class Processor extends DbRowProcessor {
74: private String mFirstname = null;
75: private String mLastname = null;
76:
77: public String getFirstname() {
78: return mFirstname;
79: }
80:
81: public String getLastname() {
82: return mLastname;
83: }
84:
85: public boolean processRow(ResultSet resultSet)
86: throws SQLException {
87: mFirstname = resultSet.getString("firstname");
88: mLastname = resultSet.getString("lastname");
89:
90: return true;
91: }
92:
93: }
94: }
|