bump product version to 7.2.5.1
[LibreOffice.git] / odk / examples / DevelopersGuide / Database / RowSet.java
blobe053dabb9c2dd727c4805a41a1ab0f1aa8447792
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com.sun.star.lang.XMultiComponentFactory;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XComponentContext;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.sdbc.*;
42 import com.sun.star.sdbcx.Privilege;
43 import com.sun.star.sdb.XRowSetApproveBroadcaster;
45 public class RowSet
47 private static XComponentContext xContext = null;
48 private static XMultiComponentFactory xMCF = null;
49 public static void main(String argv[]) throws java.lang.Exception
51 try {
52 // get the remote office component context
53 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
54 System.out.println("Connected to a running office ...");
55 xMCF = xContext.getServiceManager();
57 catch( Exception e) {
58 System.err.println("ERROR: can't get a component context from a running office ...");
59 e.printStackTrace(System.err);
60 System.exit(1);
63 try{
64 showRowSetEvents();
65 showRowSetRowCount();
66 showRowSetPrivileges();
67 useRowSet();
69 catch(com.sun.star.uno.Exception e)
71 System.err.println(e);
72 e.printStackTrace();
74 System.exit(0);
79 private static void useRowSet() throws com.sun.star.uno.Exception
81 // first we create our RowSet object
82 XRowSet xRowRes = UnoRuntime.queryInterface(
83 XRowSet.class,
84 xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext));
86 System.out.println("RowSet created!");
87 // set the properties needed to connect to a database
88 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
89 xProp.setPropertyValue("DataSourceName","Bibliography");
90 xProp.setPropertyValue("Command","biblio");
91 xProp.setPropertyValue("CommandType",Integer.valueOf(com.sun.star.sdb.CommandType.TABLE));
93 xRowRes.execute();
94 System.out.println("RowSet executed!");
97 XComponent xComp = UnoRuntime.queryInterface(XComponent.class,xRowRes);
98 xComp.dispose();
99 System.out.println("RowSet destroyed!");
102 private static void showRowSetPrivileges() throws com.sun.star.uno.Exception
104 // first we create our RowSet object
105 XRowSet xRowRes = 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 = UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
112 xProp.setPropertyValue("DataSourceName","Bibliography");
113 xProp.setPropertyValue("Command","biblio");
114 xProp.setPropertyValue("CommandType",Integer.valueOf(com.sun.star.sdb.CommandType.TABLE));
116 xRowRes.execute();
117 System.out.println("RowSet executed!");
119 Integer aPriv = (Integer)xProp.getPropertyValue("Privileges");
120 int nPriv = aPriv.intValue();
121 if( (nPriv & Privilege.SELECT) == Privilege.SELECT)
122 System.out.println("SELECT");
123 if( (nPriv & Privilege.INSERT) == Privilege.INSERT)
124 System.out.println("INSERT");
125 if( (nPriv & Privilege.UPDATE) == Privilege.UPDATE)
126 System.out.println("UPDATE");
127 if( (nPriv & Privilege.DELETE) == Privilege.DELETE)
128 System.out.println("DELETE");
130 // now destroy the RowSet
131 XComponent xComp = UnoRuntime.queryInterface(XComponent.class,xRowRes);
132 xComp.dispose();
133 System.out.println("RowSet destroyed!");
136 private static void showRowSetRowCount() throws com.sun.star.uno.Exception
138 // first we create our RowSet object
139 XRowSet xRowRes = UnoRuntime.queryInterface(
140 XRowSet.class,
141 xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext));
143 System.out.println("RowSet created!");
144 // set the properties needed to connect to a database
145 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
146 xProp.setPropertyValue("DataSourceName","Bibliography");
147 xProp.setPropertyValue("Command","biblio");
148 xProp.setPropertyValue("CommandType",Integer.valueOf(com.sun.star.sdb.CommandType.TABLE));
150 xRowRes.execute();
151 System.out.println("RowSet executed!");
153 // now look if the RowCount is already final
154 System.out.println("The RowCount is final: " + xProp.getPropertyValue("IsRowCountFinal"));
156 XResultSet xRes = UnoRuntime.queryInterface(XResultSet.class,xRowRes);
157 xRes.last();
159 System.out.println("The RowCount is final: " + xProp.getPropertyValue("IsRowCountFinal"));
160 System.out.println("There are " + xProp.getPropertyValue("RowCount") + " rows!");
162 // now destroy the RowSet
163 XComponent xComp = UnoRuntime.queryInterface(XComponent.class,xRowRes);
164 xComp.dispose();
165 System.out.println("RowSet destroyed!");
168 private static void showRowSetEvents() throws com.sun.star.uno.Exception
170 // first we create our RowSet object
171 XRowSet xRowRes = UnoRuntime.queryInterface(
172 XRowSet.class,
173 xMCF.createInstanceWithContext("com.sun.star.sdb.RowSet", xContext));
175 System.out.println("RowSet created!");
176 // add our Listener
177 System.out.println("Append our Listener!");
178 RowSetEventListener pRow = new RowSetEventListener();
179 XRowSetApproveBroadcaster xApBroad = UnoRuntime.queryInterface(XRowSetApproveBroadcaster.class,xRowRes);
180 xApBroad.addRowSetApproveListener(pRow);
181 xRowRes.addRowSetListener(pRow);
183 // set the properties needed to connect to a database
184 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
185 xProp.setPropertyValue("DataSourceName","Bibliography");
186 xProp.setPropertyValue("Command","biblio");
187 xProp.setPropertyValue("CommandType",Integer.valueOf(com.sun.star.sdb.CommandType.TABLE));
189 xRowRes.execute();
190 System.out.println("RowSet executed!");
192 // do some movements to check if we got all notifications
193 XResultSet xRes = UnoRuntime.queryInterface(XResultSet.class,xRowRes);
194 System.out.println("beforeFirst");
195 xRes.beforeFirst();
196 // this should lead to no notifications because
197 // we should stand before the first row at the beginning
198 System.out.println("We stand before the first row: " + xRes.isBeforeFirst());
200 System.out.println("next");
201 xRes.next();
202 System.out.println("next");
203 xRes.next();
204 System.out.println("last");
205 xRes.last();
206 System.out.println("next");
207 xRes.next();
208 System.out.println("We stand after the last row: " + xRes.isAfterLast());
209 System.out.println("first");
210 xRes.first();
211 System.out.println("previous");
212 xRes.previous();
213 System.out.println("We stand before the first row: " + xRes.isBeforeFirst());
214 System.out.println("afterLast");
215 xRes.afterLast();
216 System.out.println("We stand after the last row: " + xRes.isAfterLast());
218 // now destroy the RowSet
219 XComponent xComp = UnoRuntime.queryInterface(XComponent.class,xRowRes);
220 xComp.dispose();
221 System.out.println("RowSet destroyed!");
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */