Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / Database / RowSet.java
blobc571bb214eb55f6c64a5aff998501ba27ae3922c
1 /*************************************************************************
3 * $RCSfile: RowSet.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:20:14 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import java.io.*;
43 import com.sun.star.comp.helper.RegistryServiceFactory;
44 import com.sun.star.comp.servicemanager.ServiceManager;
45 import com.sun.star.lang.XMultiComponentFactory;
46 import com.sun.star.lang.XServiceInfo;
47 import com.sun.star.lang.XComponent;
48 import com.sun.star.bridge.XUnoUrlResolver;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.uno.XComponentContext;
51 import com.sun.star.beans.XPropertySet;
52 import com.sun.star.container.XNameAccess;
53 import com.sun.star.sdbc.*;
54 import com.sun.star.sdbcx.Privilege;
55 import com.sun.star.sdb.CommandType;
56 import com.sun.star.sdb.XRowSetApproveBroadcaster;
58 public class RowSet
60 private static XComponentContext xContext = null;
61 private static XMultiComponentFactory xMCF = null;
62 public static void main(String argv[]) throws java.lang.Exception
64 try {
65 // get the remote office component context
66 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
67 System.out.println("Connected to a running office ...");
68 xMCF = xContext.getServiceManager();
70 catch( Exception e) {
71 System.err.println("ERROR: can't get a component context from a running office ...");
72 e.printStackTrace(System.out);
73 System.exit(1);
76 try{
77 showRowSetEvents();
78 showRowSetRowCount();
79 showRowSetPrivileges();
80 useRowSet();
82 catch(com.sun.star.uno.Exception e)
84 System.err.println(e);
85 e.printStackTrace();
87 System.exit(0);
90 public static void printDataSources() throws com.sun.star.uno.Exception
92 // create a DatabaseContext and print all DataSource names
93 XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(
94 XNameAccess.class,
95 xMCF.createInstanceWithContext("com.sun.star.sdb.DatabaseContext",
96 xContext));
97 String aNames [] = xNameAccess.getElementNames();
98 for(int i=0;i<aNames.length;++i)
99 System.out.println(aNames[i]);
102 public static void useRowSet() throws com.sun.star.uno.Exception
104 // first we create our RowSet object
105 XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface(
106 XRowSet.class,
107 xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext));
109 System.out.println("RowSet created!");
110 // set the properties needed to connect to a database
111 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
112 xProp.setPropertyValue("DataSourceName","Bibliography");
113 xProp.setPropertyValue("Command","biblio");
114 xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
116 xRowRes.execute();
117 System.out.println("RowSet executed!");
120 XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes);
121 xComp.dispose();
122 System.out.println("RowSet destroyed!");
125 public static void showRowSetPrivileges() throws com.sun.star.uno.Exception
127 // first we create our RowSet object
128 XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface(
129 XRowSet.class,
130 xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext));
132 System.out.println("RowSet created!");
133 // set the properties needed to connect to a database
134 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
135 xProp.setPropertyValue("DataSourceName","Bibliography");
136 xProp.setPropertyValue("Command","biblio");
137 xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
139 xRowRes.execute();
140 System.out.println("RowSet executed!");
142 Integer aPriv = (Integer)xProp.getPropertyValue("Privileges");
143 int nPriv = aPriv.intValue();
144 if( (nPriv & Privilege.SELECT) == Privilege.SELECT)
145 System.out.println("SELECT");
146 if( (nPriv & Privilege.INSERT) == Privilege.INSERT)
147 System.out.println("INSERT");
148 if( (nPriv & Privilege.UPDATE) == Privilege.UPDATE)
149 System.out.println("UPDATE");
150 if( (nPriv & Privilege.DELETE) == Privilege.DELETE)
151 System.out.println("DELETE");
153 // now destroy the RowSet
154 XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes);
155 xComp.dispose();
156 System.out.println("RowSet destroyed!");
159 public static void showRowSetRowCount() throws com.sun.star.uno.Exception
161 // first we create our RowSet object
162 XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface(
163 XRowSet.class,
164 xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext));
166 System.out.println("RowSet created!");
167 // set the properties needed to connect to a database
168 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
169 xProp.setPropertyValue("DataSourceName","Bibliography");
170 xProp.setPropertyValue("Command","biblio");
171 xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
173 xRowRes.execute();
174 System.out.println("RowSet executed!");
176 // now look if the RowCount is already final
177 System.out.println("The RowCount is final: " + xProp.getPropertyValue("IsRowCountFinal"));
179 XResultSet xRes = (XResultSet)UnoRuntime.queryInterface(XResultSet.class,xRowRes);
180 xRes.last();
182 System.out.println("The RowCount is final: " + xProp.getPropertyValue("IsRowCountFinal"));
183 System.out.println("There are " + xProp.getPropertyValue("RowCount") + " rows!");
185 // now destroy the RowSet
186 XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes);
187 xComp.dispose();
188 System.out.println("RowSet destroyed!");
191 public static void showRowSetEvents() throws com.sun.star.uno.Exception
193 // first we create our RowSet object
194 XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface(
195 XRowSet.class,
196 xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext));
198 System.out.println("RowSet created!");
199 // add our Listener
200 System.out.println("Append our Listener!");
201 RowSetEventListener pRow = new RowSetEventListener();
202 XRowSetApproveBroadcaster xApBroad = (XRowSetApproveBroadcaster)UnoRuntime.queryInterface(XRowSetApproveBroadcaster.class,xRowRes);
203 xApBroad.addRowSetApproveListener(pRow);
204 xRowRes.addRowSetListener(pRow);
206 // set the properties needed to connect to a database
207 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
208 xProp.setPropertyValue("DataSourceName","Bibliography");
209 xProp.setPropertyValue("Command","biblio");
210 xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
212 xRowRes.execute();
213 System.out.println("RowSet executed!");
215 // do some movements to check if we got all notifications
216 XResultSet xRes = (XResultSet)UnoRuntime.queryInterface(XResultSet.class,xRowRes);
217 System.out.println("beforeFirst");
218 xRes.beforeFirst();
219 // this should lead to no notifications because
220 // we should stand before the first row at the beginning
221 System.out.println("We stand before the first row: " + xRes.isBeforeFirst());
223 System.out.println("next");
224 xRes.next();
225 System.out.println("next");
226 xRes.next();
227 System.out.println("last");
228 xRes.last();
229 System.out.println("next");
230 xRes.next();
231 System.out.println("We stand after the last row: " + xRes.isAfterLast());
232 System.out.println("first");
233 xRes.first();
234 System.out.println("previous");
235 xRes.previous();
236 System.out.println("We stand before the first row: " + xRes.isBeforeFirst());
237 System.out.println("afterLast");
238 xRes.afterLast();
239 System.out.println("We stand after the last row: " + xRes.isAfterLast());
241 // now destroy the RowSet
242 XComponent xComp = (XComponent)UnoRuntime.queryInterface(XComponent.class,xRowRes);
243 xComp.dispose();
244 System.out.println("RowSet destroyed!");