tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / odk / examples / DevelopersGuide / Database / sdbcx.java
blobe4e53a3db7e9f057c217fed85201ead8d3b88f53
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.XComponent;
37 import com.sun.star.uno.*;
38 import com.sun.star.bridge.XUnoUrlResolver;
39 import com.sun.star.beans.XPropertySet;
40 import com.sun.star.container.XNameAccess;
41 import com.sun.star.container.XIndexAccess;
42 import com.sun.star.sdbc.*;
43 import com.sun.star.sdbcx.*;
44 import com.sun.star.lang.XMultiServiceFactory;
46 public class sdbcx
48 private final XMultiServiceFactory xORB;
49 private static XConnection con;
50 private XTablesSupplier xTabSup;
52 public static XMultiServiceFactory rSmgr;
54 public static void main(String argv[]) throws java.lang.Exception
56 try{
57 rSmgr = connect("socket,host=localhost,port=8100");
58 sdbcx test = new sdbcx(rSmgr);
59 test.createConnection();
60 test.displayTableProperties();
61 // now we dispose the connection to close it
62 XComponent xComponent = UnoRuntime.queryInterface(XComponent.class,con);
63 if(xComponent != null)
65 xComponent.dispose();
66 System.out.println("Connection disposed!");
69 catch(com.sun.star.uno.Exception e)
71 System.out.println(e);
72 e.printStackTrace();
74 System.exit(0);
76 public static XMultiServiceFactory connect( String connectStr )
77 throws com.sun.star.uno.Exception,
78 com.sun.star.uno.RuntimeException, java.lang.Exception
80 // initial serviceManager
81 XMultiServiceFactory xLocalServiceManager =
82 com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();
84 // create a connector, so that it can contact the office
85 Object xUrlResolver = xLocalServiceManager.createInstance( "com.sun.star.bridge.UnoUrlResolver" );
86 XUnoUrlResolver urlResolver = UnoRuntime.queryInterface(
87 XUnoUrlResolver.class, xUrlResolver );
89 Object rInitialObject = urlResolver.resolve( "uno:" + connectStr + ";urp;StarOffice.NamingService" );
91 XNamingService rName = UnoRuntime.queryInterface(
92 XNamingService.class, rInitialObject );
94 XMultiServiceFactory xMSF = null;
95 if( rName != null ) {
96 System.err.println( "got the remote naming service !" );
97 Object rXsmgr = rName.getRegisteredObject("StarOffice.ServiceManager" );
99 xMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, rXsmgr );
102 return xMSF;
106 public sdbcx(XMultiServiceFactory rSmgr )
108 xORB = rSmgr;
111 public void createConnection() throws com.sun.star.uno.Exception
113 // create the Driver with the implementation name
114 Object aDriver = xORB.createInstance("org.openoffice.comp.connectivity.pq.Driver.noext");
115 // query for the interface
116 com.sun.star.sdbc.XDriver xDriver;
117 xDriver = UnoRuntime.queryInterface(XDriver.class,aDriver);
118 if(xDriver != null)
120 // first create the needed url
121 String URL = "sdbc:postgresql:dbname=MYDB0";
122 // second create the necessary properties
123 com.sun.star.beans.PropertyValue [] Props = new com.sun.star.beans.PropertyValue[]
125 new com.sun.star.beans.PropertyValue("user",0,"test1",com.sun.star.beans.PropertyState.DIRECT_VALUE),
126 new com.sun.star.beans.PropertyValue("password",0,"test1",com.sun.star.beans.PropertyState.DIRECT_VALUE)
129 // now create a connection to the database
130 con = xDriver.connect(URL, Props);
131 if(con != null)
133 System.out.println("Connection could be created!");
134 // with the XDatabaseDefinitionSupplier interface from the driver to get the XTablesSupplier
135 XDataDefinitionSupplier xDDSup = UnoRuntime.queryInterface(
136 XDataDefinitionSupplier.class,xDriver);
137 if(xDDSup != null)
139 xTabSup = xDDSup.getDataDefinitionByConnection(con);
140 if(xTabSup != null)
142 XNameAccess xTables = xTabSup.getTables();
143 // now print all table names
144 System.out.println("Tables available:");
145 String [] aTableNames = xTables.getElementNames();
146 for ( int i =0; i<= aTableNames.length-1; i++)
147 System.out.println(aTableNames[i]);
150 else
151 System.out.println("The driver is not a SDBCX capable!");
153 else
154 System.out.println("Connection could not be created!");
158 public void displayTableProperties() throws com.sun.star.uno.Exception
160 XNameAccess xTables = xTabSup.getTables();
161 String [] aTableNames = xTables.getElementNames();
162 if(0 != aTableNames.length)
164 Object table = xTables.getByName(aTableNames[0]);
165 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class,table);
166 System.out.println("Name: " + xProp.getPropertyValue("Name"));
167 System.out.println("CatalogName: " + xProp.getPropertyValue("CatalogName"));
168 System.out.println("SchemaName: " + xProp.getPropertyValue("SchemaName"));
169 System.out.println("Description: " + xProp.getPropertyValue("Description"));
170 // the following property is optional so we first must check if it exists
171 if(xProp.getPropertySetInfo().hasPropertyByName("Type"))
172 System.out.println("Type: " + xProp.getPropertyValue("Type"));
177 // 15. example
178 // print all columns of a XColumnsSupplier
180 public static void printColumns(XColumnsSupplier xColumnsSup)
182 System.out.println("Example printColumns");
183 // the table must at least support a XColumnsSupplier interface
184 System.out.println("--- Columns ---");
185 XNameAccess xColumns = xColumnsSup.getColumns();
186 String [] aColumnNames = xColumns.getElementNames();
187 for ( int i =0; i<= aColumnNames.length-1; i++)
188 System.out.println(" " + aColumnNames[i]);
191 // 16. example
192 // print all keys including the columns of a key
194 public static void printKeys(XColumnsSupplier xColumnsSup) throws com.sun.star.uno.Exception
196 System.out.println("Example printKeys");
197 XKeysSupplier xKeysSup = UnoRuntime.queryInterface(XKeysSupplier.class,xColumnsSup);
198 if(xKeysSup != null)
200 System.out.println("--- Keys ---");
201 XIndexAccess xKeys = xKeysSup.getKeys();
202 for ( int i =0; i < xKeys.getCount(); i++)
204 Object key = xKeys.getByIndex(i);
205 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class,key);
206 System.out.println(" " + xProp.getPropertyValue("Name"));
207 XColumnsSupplier xKeyColumnsSup = UnoRuntime.queryInterface(XColumnsSupplier.class,xProp);
208 printColumns(xKeyColumnsSup);
213 // 17. example
214 // print all indexes including the columns of an index
216 public static void printIndexes(XColumnsSupplier xColumnsSup) throws com.sun.star.uno.Exception
218 System.out.println("Example printIndexes");
219 XIndexesSupplier xIndexesSup = UnoRuntime.queryInterface(XIndexesSupplier.class,xColumnsSup);
220 if(xIndexesSup != null)
222 System.out.println("--- Indexes ---");
223 XNameAccess xIndexs = xIndexesSup.getIndexes();
224 String [] aIndexNames = xIndexs.getElementNames();
225 for ( int i =0; i<= aIndexNames.length-1; i++)
227 System.out.println(" " + aIndexNames[i]);
228 Object index = xIndexs.getByName(aIndexNames[i]);
229 XColumnsSupplier xIndexColumnsSup = UnoRuntime.queryInterface(XColumnsSupplier.class,index);
230 printColumns(xIndexColumnsSup);
236 // 18. example
237 // column properties
239 public static void printColumnProperties(Object column) throws com.sun.star.uno.Exception
241 System.out.println("Example printColumnProperties");
242 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class,column);
243 System.out.println("Name: " + xProp.getPropertyValue("Name"));
244 System.out.println("Type: " + xProp.getPropertyValue("Type"));
245 System.out.println("TypeName: " + xProp.getPropertyValue("TypeName"));
246 System.out.println("Precision: " + xProp.getPropertyValue("Precision"));
247 System.out.println("Scale: " + xProp.getPropertyValue("Scale"));
248 System.out.println("IsNullable: " + xProp.getPropertyValue("IsNullable"));
249 System.out.println("IsAutoIncrement: " + xProp.getPropertyValue("IsAutoIncrement"));
250 System.out.println("IsCurrency: " + xProp.getPropertyValue("IsCurrency"));
251 // the following property is optional so we first must check if it exists
252 if(xProp.getPropertySetInfo().hasPropertyByName("IsRowVersion"))
253 System.out.println("IsRowVersion: " + xProp.getPropertyValue("IsRowVersion"));
254 if(xProp.getPropertySetInfo().hasPropertyByName("Description"))
255 System.out.println("Description: " + xProp.getPropertyValue("Description"));
256 if(xProp.getPropertySetInfo().hasPropertyByName("DefaultValue"))
257 System.out.println("DefaultValue: " + xProp.getPropertyValue("DefaultValue"));
261 // 19. example
262 // index properties
264 public static void printIndexProperties(Object index) throws com.sun.star.uno.Exception
266 System.out.println("Example printIndexProperties");
267 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class,index);
268 System.out.println("Name: " + xProp.getPropertyValue("Name"));
269 System.out.println("Catalog: " + xProp.getPropertyValue("Catalog"));
270 System.out.println("IsUnique: " + xProp.getPropertyValue("IsUnique"));
271 System.out.println("IsPrimaryKeyIndex: " + xProp.getPropertyValue("IsPrimaryKeyIndex"));
272 System.out.println("IsClustered: " + xProp.getPropertyValue("IsClustered"));
276 // 20. example
277 // key properties
279 public static void printKeyProperties(Object key) throws com.sun.star.uno.Exception
281 System.out.println("Example printKeyProperties");
282 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class,key);
283 System.out.println("Name: " + xProp.getPropertyValue("Name"));
284 System.out.println("Type: " + xProp.getPropertyValue("Type"));
285 System.out.println("ReferencedTable: " + xProp.getPropertyValue("ReferencedTable"));
286 System.out.println("UpdateRule: " + xProp.getPropertyValue("UpdateRule"));
287 System.out.println("DeleteRule: " + xProp.getPropertyValue("DeleteRule"));
291 // 21. example
292 // print all groups and the users with their privileges who belong to this group
294 public static void printGroups(XTablesSupplier xTabSup) throws com.sun.star.uno.Exception,SQLException
296 System.out.println("Example printGroups");
297 XGroupsSupplier xGroupsSup = UnoRuntime.queryInterface(XGroupsSupplier.class,xTabSup);
298 if(xGroupsSup != null)
300 // the table must be at least support a XColumnsSupplier interface
301 System.out.println("--- Groups ---");
302 XNameAccess xGroups = xGroupsSup.getGroups();
303 String [] aGroupNames = xGroups.getElementNames();
304 for ( int i =0; i < aGroupNames.length; i++)
306 System.out.println(" " + aGroupNames[i]);
307 XUsersSupplier xUsersSup = UnoRuntime.queryInterface(XUsersSupplier.class,xGroups.getByName(aGroupNames[i]));
308 if(xUsersSup != null)
310 XAuthorizable xAuth = UnoRuntime.queryInterface(XAuthorizable.class,xUsersSup);
311 // the table must be at least support a XColumnsSupplier interface
312 System.out.println("\t--- Users ---");
313 XNameAccess xUsers = xUsersSup.getUsers();
314 String [] aUserNames = xUsers.getElementNames();
315 for ( int j =0; j < aUserNames.length; j++)
317 System.out.println("\t " + aUserNames[j] + " Privileges: " + xAuth.getPrivileges(aUserNames[j],PrivilegeObject.TABLE));
325 // 22. example
326 // create the table salesmen
328 public static void createTableSalesMen(XNameAccess xTables) throws com.sun.star.uno.Exception,SQLException
330 System.out.println("Example createTableSalesMen");
331 XDataDescriptorFactory xTabFac = UnoRuntime.queryInterface(XDataDescriptorFactory.class,xTables);
332 if(xTabFac != null)
334 // create the new table
335 XPropertySet xTable = xTabFac.createDataDescriptor();
336 // set the name of the new table
337 xTable.setPropertyValue("Name","SALESMAN");
338 // append the columns
339 XColumnsSupplier xColumSup = UnoRuntime.queryInterface(XColumnsSupplier.class,xTable);
340 XDataDescriptorFactory xColFac = UnoRuntime.queryInterface(XDataDescriptorFactory.class,xColumSup.getColumns());
341 XAppend xAppend = UnoRuntime.queryInterface(XAppend.class,xColFac);
342 // we only need one descriptor
343 XPropertySet xCol = xColFac.createDataDescriptor();
344 // create first column and append
345 xCol.setPropertyValue("Name","SNR");
346 xCol.setPropertyValue("Type",Integer.valueOf(DataType.INTEGER));
347 xCol.setPropertyValue("IsNullable",Integer.valueOf(ColumnValue.NO_NULLS));
348 xAppend.appendByDescriptor(xCol);
349 // 2nd only set the properties which differ
350 xCol.setPropertyValue("Name","FIRSTNAME");
351 xCol.setPropertyValue("Type",Integer.valueOf(DataType.VARCHAR));
352 xCol.setPropertyValue("IsNullable",Integer.valueOf(ColumnValue.NULLABLE));
353 xCol.setPropertyValue("Precision",Integer.valueOf(50));
354 xAppend.appendByDescriptor(xCol);
355 // 3rd only set the properties which differ
356 xCol.setPropertyValue("Name","LASTNAME");
357 xCol.setPropertyValue("Precision",Integer.valueOf(100));
358 xAppend.appendByDescriptor(xCol);
359 // 4th only set the properties which differ
360 xCol.setPropertyValue("Name","STREET");
361 xCol.setPropertyValue("Precision",Integer.valueOf(50));
362 xAppend.appendByDescriptor(xCol);
363 // 5th only set the properties which differ
364 xCol.setPropertyValue("Name","STATE");
365 xAppend.appendByDescriptor(xCol);
366 // 6th only set the properties which differ
367 xCol.setPropertyValue("Name","ZIP");
368 xCol.setPropertyValue("Type",Integer.valueOf(DataType.INTEGER));
369 xCol.setPropertyValue("Precision",Integer.valueOf(10)); // default value integer
370 xAppend.appendByDescriptor(xCol);
371 // 7th only set the properties which differ
372 xCol.setPropertyValue("Name","BIRTHDATE");
373 xCol.setPropertyValue("Type",Integer.valueOf(DataType.DATE));
374 xCol.setPropertyValue("Precision",Integer.valueOf(10)); // default value integer
375 xAppend.appendByDescriptor(xCol);
376 // now we create the primary key
377 XKeysSupplier xKeySup = UnoRuntime.queryInterface(XKeysSupplier.class,xTable);
378 XDataDescriptorFactory xKeyFac = UnoRuntime.queryInterface(XDataDescriptorFactory.class,xKeySup.getKeys());
379 XAppend xKeyAppend = UnoRuntime.queryInterface(XAppend.class,xKeyFac);
380 XPropertySet xKey = xKeyFac.createDataDescriptor();
381 xKey.setPropertyValue("Type",Integer.valueOf(KeyType.PRIMARY));
382 // now append the columns to key
383 XColumnsSupplier xKeyColumSup = UnoRuntime.queryInterface(XColumnsSupplier.class,xKey);
384 XDataDescriptorFactory xKeyColFac = UnoRuntime.queryInterface(XDataDescriptorFactory.class,xKeyColumSup.getColumns());
385 XAppend xKeyColAppend = UnoRuntime.queryInterface(XAppend.class,xKeyColFac);
386 // we only need one descriptor
387 XPropertySet xKeyCol = xKeyColFac.createDataDescriptor();
388 xKeyCol.setPropertyValue("Name","SNR");
389 // append the key column
390 xKeyColAppend.appendByDescriptor(xKeyCol);
391 // append the key
392 xKeyAppend.appendByDescriptor(xKey);
393 // the last step is to append the new table to the tables collection
394 XAppend xTableAppend = UnoRuntime.queryInterface(XAppend.class,xTabFac);
395 xTableAppend.appendByDescriptor(xTable);
400 // 23. example
401 // create a user
403 public static void createUser(XNameAccess xUsers) throws com.sun.star.uno.Exception,SQLException
405 System.out.println("Example createUser");
406 XDataDescriptorFactory xUserFac = UnoRuntime.queryInterface(XDataDescriptorFactory.class,xUsers);
407 if(xUserFac != null)
409 // create the new table
410 XPropertySet xUser = xUserFac.createDataDescriptor();
411 // set the name of the new table
412 xUser.setPropertyValue("Name","BOSS");
413 xUser.setPropertyValue("Password","BOSSWIFENAME");
414 XAppend xAppend = UnoRuntime.queryInterface(XAppend.class,xUserFac);
415 xAppend.appendByDescriptor(xUser);
420 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */