001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of JPersist.
006: *
007: * JPersist is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * JPersist is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with JPersist; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jpersist.example;
020:
021: import java.sql.ResultSet;
022: import java.util.logging.Level;
023: import jpersist.*;
024: import jpersist.interfaces.ResultObject;
025: import java.util.logging.Logger;
026:
027: public class ProxyExample {
028: static Logger logger = Logger.getLogger(ProxyExample.class
029: .getName());
030:
031: public interface Contacts extends ResultObject {
032: public String getContactId();
033:
034: public void setContactId(String contactId);
035:
036: public String getPassword();
037:
038: public void setPassword(String password);
039:
040: public String getFirstName();
041:
042: public void setFirstName(String firstName);
043:
044: public String getLastName();
045:
046: public void setLastName(String lastName);
047:
048: public String getCompanyName();
049:
050: public void setCompanyName(String companyName);
051:
052: public String getEmail();
053:
054: public void setEmail(String email);
055: }
056:
057: public ProxyExample() {
058: DatabaseManager dbm = null;
059:
060: try {
061: try {
062: DatabaseManager.setLogLevel(Level.OFF);
063: dbm = DatabaseManager
064: .getXmlDefinedDatabaseManager("jpersist");
065: Database db = dbm.getDatabase();
066: Contacts c = null;
067:
068: try {
069: db
070: .setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE);
071: c = (Contacts) db.executeQuery(
072: "select * from contacts where 1 = 0")
073: .castToInterface(Contacts.class);
074:
075: /*
076: * A number of databases lack support for inserting data into a result set
077: */
078: for (int i = 0; i < 5; i++) {
079: c.moveToInsertRow();
080:
081: c.setContactId("contactId"
082: + System.currentTimeMillis());
083: c.setPassword("password_" + i);
084: c.setFirstName("First Name " + i);
085: c.setLastName("Last Name " + i);
086: c.setCompanyName("Company Name " + i);
087: c.setEmail("email " + i);
088:
089: c.insertRow();
090: }
091: } catch (JPersistException e) {
092: e.printStackTrace();
093: System.out
094: .println("A number of databases lack support for inserting data into a result set.");
095: }
096:
097: c = (Contacts) db
098: .executeQuery("select * from contacts")
099: .castToInterface(Contacts.class);
100:
101: while (c.hasNext() && c.next() != null)
102: System.out
103: .println(c.getContactId() + ", "
104: + c.getFirstName() + ", "
105: + c.getLastName());
106:
107: db.close();
108: } finally {
109: dbm.close();
110: }
111: } catch (Exception e) {
112: e.printStackTrace();
113: }
114: }
115:
116: public static void main(String[] args) {
117: new ProxyExample();
118: }
119: }
|