xref: /trunk/main/odk/examples/DevelopersGuide/Database/sdbcx.java (revision 34dd1e2512dbacb6a9a7e4c7f17b9296daa8eff3)
1*34dd1e25SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import java.io.*;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.lang.XComponent;
27cdf0e10cSrcweir import com.sun.star.uno.*;
28cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
29cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
30cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
31cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
32cdf0e10cSrcweir import com.sun.star.sdbc.*;
33cdf0e10cSrcweir import com.sun.star.sdbcx.*;
34cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir public class sdbcx
37cdf0e10cSrcweir {
38cdf0e10cSrcweir     private XMultiServiceFactory xORB;
39cdf0e10cSrcweir     private static XConnection con;
40cdf0e10cSrcweir     private XTablesSupplier xTabSup;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir         public static XMultiServiceFactory rSmgr;
43cdf0e10cSrcweir     public static void main(String argv[]) throws java.lang.Exception
44cdf0e10cSrcweir     {
45cdf0e10cSrcweir         try{
46cdf0e10cSrcweir             rSmgr = connect("socket,host=localhost,port=8100");
47cdf0e10cSrcweir                         sdbcx test = new sdbcx(rSmgr);
48cdf0e10cSrcweir                         test.createConnection();
49cdf0e10cSrcweir                         test.displayTableProperties();
50cdf0e10cSrcweir                         // now we dispose the connection to close it
51cdf0e10cSrcweir                         XComponent xComponent = (XComponent)UnoRuntime.queryInterface(XComponent.class,con);
52cdf0e10cSrcweir                         if(xComponent != null)
53cdf0e10cSrcweir                         {
54cdf0e10cSrcweir                                 xComponent.dispose();
55cdf0e10cSrcweir                                 System.out.println("Connection disposed!");
56cdf0e10cSrcweir                         }
57cdf0e10cSrcweir                 }
58cdf0e10cSrcweir         catch(com.sun.star.uno.Exception e)
59cdf0e10cSrcweir         {
60cdf0e10cSrcweir             System.out.println(e);
61cdf0e10cSrcweir             e.printStackTrace();
62cdf0e10cSrcweir         }
63cdf0e10cSrcweir         System.exit(0);
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir         public static XMultiServiceFactory connect( String connectStr )
66cdf0e10cSrcweir         throws com.sun.star.uno.Exception,
67cdf0e10cSrcweir         com.sun.star.uno.RuntimeException, java.lang.Exception
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         // initial serviceManager
70cdf0e10cSrcweir         XMultiServiceFactory xLocalServiceManager =
71cdf0e10cSrcweir             com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         // create a connector, so that it can contact the office
74cdf0e10cSrcweir         Object  xUrlResolver  = xLocalServiceManager.createInstance( "com.sun.star.bridge.UnoUrlResolver" );
75cdf0e10cSrcweir         XUnoUrlResolver urlResolver = (XUnoUrlResolver)UnoRuntime.queryInterface(
76cdf0e10cSrcweir             XUnoUrlResolver.class, xUrlResolver );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir         Object rInitialObject = urlResolver.resolve( "uno:" + connectStr + ";urp;StarOffice.NamingService" );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir         XNamingService rName = (XNamingService)UnoRuntime.queryInterface(
81cdf0e10cSrcweir             XNamingService.class, rInitialObject );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         XMultiServiceFactory xMSF = null;
84cdf0e10cSrcweir         if( rName != null ) {
85cdf0e10cSrcweir             System.err.println( "got the remote naming service !" );
86cdf0e10cSrcweir             Object rXsmgr = rName.getRegisteredObject("StarOffice.ServiceManager" );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir             xMSF = (XMultiServiceFactory)
89cdf0e10cSrcweir                 UnoRuntime.queryInterface( XMultiServiceFactory.class, rXsmgr );
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         return ( xMSF );
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     public sdbcx(XMultiServiceFactory rSmgr )
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         xORB = rSmgr;
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     public void createConnection() throws com.sun.star.uno.Exception
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         // create the Driver with the implementation name
104cdf0e10cSrcweir         Object aDriver = xORB.createInstance("com.sun.star.comp.sdbcx.adabas.ODriver");
105cdf0e10cSrcweir         // query for the interface
106cdf0e10cSrcweir         com.sun.star.sdbc.XDriver xDriver;
107cdf0e10cSrcweir         xDriver = (XDriver)UnoRuntime.queryInterface(XDriver.class,aDriver);
108cdf0e10cSrcweir         if(xDriver != null)
109cdf0e10cSrcweir         {
110cdf0e10cSrcweir             // first create the needed url
111cdf0e10cSrcweir             String adabasURL = "sdbc:adabas::MYDB0";
112cdf0e10cSrcweir             // second create the necessary properties
113cdf0e10cSrcweir             com.sun.star.beans.PropertyValue [] adabasProps = new com.sun.star.beans.PropertyValue[]
114cdf0e10cSrcweir             {
115cdf0e10cSrcweir                 new com.sun.star.beans.PropertyValue("user",0,"test1",com.sun.star.beans.PropertyState.DIRECT_VALUE),
116cdf0e10cSrcweir                 new com.sun.star.beans.PropertyValue("password",0,"test1",com.sun.star.beans.PropertyState.DIRECT_VALUE)
117cdf0e10cSrcweir             };
118cdf0e10cSrcweir             //
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             // now create a connection to adabas
121cdf0e10cSrcweir             con = xDriver.connect(adabasURL,adabasProps);
122cdf0e10cSrcweir             if(con != null)
123cdf0e10cSrcweir             {
124cdf0e10cSrcweir                 System.out.println("Connection could be created!");
125cdf0e10cSrcweir                 // we the XDatabaseDefinitionSupplier interface from the driver to get the XTablesSupplier
126cdf0e10cSrcweir                 XDataDefinitionSupplier xDDSup = (XDataDefinitionSupplier)UnoRuntime.queryInterface(
127cdf0e10cSrcweir                         XDataDefinitionSupplier.class,xDriver);
128cdf0e10cSrcweir                 if(xDDSup != null)
129cdf0e10cSrcweir                 {
130cdf0e10cSrcweir                     xTabSup = xDDSup.getDataDefinitionByConnection(con);
131cdf0e10cSrcweir                     if(xTabSup != null)
132cdf0e10cSrcweir                     {
133cdf0e10cSrcweir                         XNameAccess xTables = xTabSup.getTables();
134cdf0e10cSrcweir                         // now print all table names
135cdf0e10cSrcweir                         System.out.println("Tables available:");
136cdf0e10cSrcweir                         String [] aTableNames = xTables.getElementNames();
137cdf0e10cSrcweir                         for ( int i =0; i<= aTableNames.length-1; i++)
138cdf0e10cSrcweir                             System.out.println(aTableNames[i]);
139cdf0e10cSrcweir                     }
140cdf0e10cSrcweir                 }
141cdf0e10cSrcweir                 else
142cdf0e10cSrcweir                     System.out.println("The driver is not a SDBCX capable!");
143cdf0e10cSrcweir             }
144cdf0e10cSrcweir             else
145cdf0e10cSrcweir                 System.out.println("Connection could not be created!");
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     public void displayTableProperties() throws com.sun.star.uno.Exception
150cdf0e10cSrcweir     {
151cdf0e10cSrcweir         XNameAccess xTables = xTabSup.getTables();
152cdf0e10cSrcweir         String [] aTableNames = xTables.getElementNames();
153cdf0e10cSrcweir         if(0 != aTableNames.length)
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir             Object table = xTables.getByName(aTableNames[0]);
156cdf0e10cSrcweir             XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,table);
157cdf0e10cSrcweir             System.out.println("Name:          " + xProp.getPropertyValue("Name"));
158cdf0e10cSrcweir             System.out.println("CatalogName:   " + xProp.getPropertyValue("CatalogName"));
159cdf0e10cSrcweir             System.out.println("SchemaName:    " + xProp.getPropertyValue("SchemaName"));
160cdf0e10cSrcweir             System.out.println("Description:   " + xProp.getPropertyValue("Description"));
161cdf0e10cSrcweir             // the following property is optional so we first must check if it exists
162cdf0e10cSrcweir             if(xProp.getPropertySetInfo().hasPropertyByName("Type"))
163cdf0e10cSrcweir                 System.out.println("Type:          " + xProp.getPropertyValue("Type"));
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     //###########################################################
168cdf0e10cSrcweir     // 15. example
169cdf0e10cSrcweir     // print all columns of a XColumnsSupplier
170cdf0e10cSrcweir     //###########################################################
171cdf0e10cSrcweir     public static void printColumns(XColumnsSupplier xColumnsSup) throws com.sun.star.uno.Exception,SQLException
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir         System.out.println("Example printColumns");
174cdf0e10cSrcweir         // the table must be at least support a XColumnsSupplier interface
175cdf0e10cSrcweir         System.out.println("--- Columns ---");
176cdf0e10cSrcweir         XNameAccess xColumns = xColumnsSup.getColumns();
177cdf0e10cSrcweir         String [] aColumnNames = xColumns.getElementNames();
178cdf0e10cSrcweir         for ( int i =0; i<= aColumnNames.length-1; i++)
179cdf0e10cSrcweir             System.out.println("    " + aColumnNames[i]);
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir     //###########################################################
182cdf0e10cSrcweir     // 16. example
183cdf0e10cSrcweir     // print all keys inclusive the columns of a key
184cdf0e10cSrcweir     //###########################################################
185cdf0e10cSrcweir     public static void printKeys(XColumnsSupplier xColumnsSup) throws com.sun.star.uno.Exception,SQLException
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         System.out.println("Example printKeys");
188cdf0e10cSrcweir         XKeysSupplier xKeysSup = (XKeysSupplier)UnoRuntime.queryInterface(XKeysSupplier.class,xColumnsSup);
189cdf0e10cSrcweir         if(xKeysSup != null)
190cdf0e10cSrcweir         {
191cdf0e10cSrcweir             System.out.println("--- Keys ---");
192cdf0e10cSrcweir             XIndexAccess xKeys = xKeysSup.getKeys();
193cdf0e10cSrcweir             for ( int i =0; i < xKeys.getCount(); i++)
194cdf0e10cSrcweir             {
195cdf0e10cSrcweir                 Object key = xKeys.getByIndex(i);
196cdf0e10cSrcweir                 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,key);
197cdf0e10cSrcweir                 System.out.println("    " + xProp.getPropertyValue("Name"));
198cdf0e10cSrcweir                 XColumnsSupplier xKeyColumnsSup = ( XColumnsSupplier ) UnoRuntime.queryInterface(XColumnsSupplier.class,xProp);
199cdf0e10cSrcweir                 printColumns(xKeyColumnsSup);
200cdf0e10cSrcweir             }
201cdf0e10cSrcweir         }
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir     //###########################################################
204cdf0e10cSrcweir     // 17. example
205cdf0e10cSrcweir     // print all keys inclusive the columns of a key
206cdf0e10cSrcweir     //###########################################################
207cdf0e10cSrcweir     public static void printIndexes(XColumnsSupplier xColumnsSup) throws com.sun.star.uno.Exception,SQLException
208cdf0e10cSrcweir     {
209cdf0e10cSrcweir         System.out.println("Example printIndexes");
210cdf0e10cSrcweir         XIndexesSupplier xIndexesSup = (XIndexesSupplier)UnoRuntime.queryInterface(XIndexesSupplier.class,xColumnsSup);
211cdf0e10cSrcweir         if(xIndexesSup != null)
212cdf0e10cSrcweir         {
213cdf0e10cSrcweir             System.out.println("--- Indexes ---");
214cdf0e10cSrcweir             XNameAccess xIndexs = xIndexesSup.getIndexes();
215cdf0e10cSrcweir             String [] aIndexNames = xIndexs.getElementNames();
216cdf0e10cSrcweir             for ( int i =0; i<= aIndexNames.length-1; i++)
217cdf0e10cSrcweir             {
218cdf0e10cSrcweir                 System.out.println("    " + aIndexNames[i]);
219cdf0e10cSrcweir                 Object index = xIndexs.getByName(aIndexNames[i]);
220cdf0e10cSrcweir                 XColumnsSupplier xIndexColumnsSup = (XColumnsSupplier)UnoRuntime.queryInterface(XColumnsSupplier.class,index);
221cdf0e10cSrcweir                 printColumns(xIndexColumnsSup);
222cdf0e10cSrcweir             }
223cdf0e10cSrcweir         }
224cdf0e10cSrcweir     }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     //###########################################################
227cdf0e10cSrcweir     // 18. example
228cdf0e10cSrcweir     // column properties
229cdf0e10cSrcweir     //###########################################################
230cdf0e10cSrcweir     public static void printColumnProperties(Object column) throws com.sun.star.uno.Exception,SQLException
231cdf0e10cSrcweir     {
232cdf0e10cSrcweir         System.out.println("Example printColumnProperties");
233cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,column);
234cdf0e10cSrcweir         System.out.println("Name:            " + xProp.getPropertyValue("Name"));
235cdf0e10cSrcweir         System.out.println("Type:            " + xProp.getPropertyValue("Type"));
236cdf0e10cSrcweir         System.out.println("TypeName:        " + xProp.getPropertyValue("TypeName"));
237cdf0e10cSrcweir         System.out.println("Precision:       " + xProp.getPropertyValue("Precision"));
238cdf0e10cSrcweir         System.out.println("Scale:           " + xProp.getPropertyValue("Scale"));
239cdf0e10cSrcweir         System.out.println("IsNullable:      " + xProp.getPropertyValue("IsNullable"));
240cdf0e10cSrcweir         System.out.println("IsAutoIncrement: " + xProp.getPropertyValue("IsAutoIncrement"));
241cdf0e10cSrcweir         System.out.println("IsCurrency:      " + xProp.getPropertyValue("IsCurrency"));
242cdf0e10cSrcweir         // the following property is optional so we first must check if it exists
243cdf0e10cSrcweir         if(xProp.getPropertySetInfo().hasPropertyByName("IsRowVersion"))
244cdf0e10cSrcweir             System.out.println("IsRowVersion:    " + xProp.getPropertyValue("IsRowVersion"));
245cdf0e10cSrcweir         if(xProp.getPropertySetInfo().hasPropertyByName("Description"))
246cdf0e10cSrcweir             System.out.println("Description:     " + xProp.getPropertyValue("Description"));
247cdf0e10cSrcweir         if(xProp.getPropertySetInfo().hasPropertyByName("DefaultValue"))
248cdf0e10cSrcweir             System.out.println("DefaultValue:    " + xProp.getPropertyValue("DefaultValue"));
249cdf0e10cSrcweir     }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir     //###########################################################
252cdf0e10cSrcweir     // 19. example
253cdf0e10cSrcweir     // index properties
254cdf0e10cSrcweir     //###########################################################
255cdf0e10cSrcweir     public static void printIndexProperties(Object index) throws com.sun.star.uno.Exception,SQLException
256cdf0e10cSrcweir     {
257cdf0e10cSrcweir         System.out.println("Example printIndexProperties");
258cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,index);
259cdf0e10cSrcweir         System.out.println("Name:              " + xProp.getPropertyValue("Name"));
260cdf0e10cSrcweir         System.out.println("Catalog:           " + xProp.getPropertyValue("Catalog"));
261cdf0e10cSrcweir         System.out.println("IsUnique:          " + xProp.getPropertyValue("IsUnique"));
262cdf0e10cSrcweir         System.out.println("IsPrimaryKeyIndex: " + xProp.getPropertyValue("IsPrimaryKeyIndex"));
263cdf0e10cSrcweir         System.out.println("IsClustered:       " + xProp.getPropertyValue("IsClustered"));
264cdf0e10cSrcweir     }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir     //###########################################################
267cdf0e10cSrcweir     // 20. example
268cdf0e10cSrcweir     // key properties
269cdf0e10cSrcweir     //###########################################################
270cdf0e10cSrcweir     public static void printKeyProperties(Object key) throws com.sun.star.uno.Exception,SQLException
271cdf0e10cSrcweir     {
272cdf0e10cSrcweir         System.out.println("Example printKeyProperties");
273cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,key);
274cdf0e10cSrcweir         System.out.println("Name:            " + xProp.getPropertyValue("Name"));
275cdf0e10cSrcweir         System.out.println("Type:            " + xProp.getPropertyValue("Type"));
276cdf0e10cSrcweir         System.out.println("ReferencedTable: " + xProp.getPropertyValue("ReferencedTable"));
277cdf0e10cSrcweir         System.out.println("UpdateRule:      " + xProp.getPropertyValue("UpdateRule"));
278cdf0e10cSrcweir         System.out.println("DeleteRule:      " + xProp.getPropertyValue("DeleteRule"));
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir     //###########################################################
282cdf0e10cSrcweir     // 21. example
283cdf0e10cSrcweir     // print all groups and the users with their privileges who belong to this group
284cdf0e10cSrcweir     //###########################################################
285cdf0e10cSrcweir     public static void printGroups(XTablesSupplier xTabSup) throws com.sun.star.uno.Exception,SQLException
286cdf0e10cSrcweir     {
287cdf0e10cSrcweir         System.out.println("Example printGroups");
288cdf0e10cSrcweir         XGroupsSupplier xGroupsSup = (XGroupsSupplier)UnoRuntime.queryInterface(XGroupsSupplier.class,xTabSup);
289cdf0e10cSrcweir         if(xGroupsSup != null)
290cdf0e10cSrcweir         {
291cdf0e10cSrcweir             // the table must be at least support a XColumnsSupplier interface
292cdf0e10cSrcweir             System.out.println("--- Groups ---");
293cdf0e10cSrcweir             XNameAccess xGroups = xGroupsSup.getGroups();
294cdf0e10cSrcweir             String [] aGroupNames = xGroups.getElementNames();
295cdf0e10cSrcweir             for ( int i =0; i < aGroupNames.length; i++)
296cdf0e10cSrcweir             {
297cdf0e10cSrcweir                 System.out.println("    " + aGroupNames[i]);
298cdf0e10cSrcweir                 XUsersSupplier xUsersSup = (XUsersSupplier)UnoRuntime.queryInterface(XUsersSupplier.class,xGroups.getByName(aGroupNames[i]));
299cdf0e10cSrcweir                 if(xUsersSup != null)
300cdf0e10cSrcweir                 {
301cdf0e10cSrcweir                     XAuthorizable xAuth = (XAuthorizable)UnoRuntime.queryInterface(XAuthorizable.class,xUsersSup);
302cdf0e10cSrcweir                     // the table must be at least support a XColumnsSupplier interface
303cdf0e10cSrcweir                     System.out.println("\t--- Users ---");
304cdf0e10cSrcweir                     XNameAccess xUsers = xUsersSup.getUsers();
305cdf0e10cSrcweir                     String [] aUserNames = xUsers.getElementNames();
306cdf0e10cSrcweir                     for ( int j =0; j < aUserNames.length; j++)
307cdf0e10cSrcweir                     {
308cdf0e10cSrcweir                         System.out.println("\t    " + aUserNames[j] + " Privileges: " + xAuth.getPrivileges(aUserNames[j],PrivilegeObject.TABLE));
309cdf0e10cSrcweir                     }
310cdf0e10cSrcweir                 }
311cdf0e10cSrcweir             }
312cdf0e10cSrcweir         }
313cdf0e10cSrcweir     }
314cdf0e10cSrcweir 
315cdf0e10cSrcweir     //###########################################################
316cdf0e10cSrcweir     // 22. example
317cdf0e10cSrcweir     // create the table salesmen
318cdf0e10cSrcweir     //###########################################################
319cdf0e10cSrcweir     public static void createTableSalesMen(XNameAccess xTables) throws com.sun.star.uno.Exception,SQLException
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir         System.out.println("Example createTableSalesMen");
322cdf0e10cSrcweir         XDataDescriptorFactory xTabFac = (XDataDescriptorFactory)UnoRuntime.queryInterface(XDataDescriptorFactory.class,xTables);
323cdf0e10cSrcweir         if(xTabFac != null)
324cdf0e10cSrcweir         {
325cdf0e10cSrcweir             // create the new table
326cdf0e10cSrcweir             XPropertySet xTable = xTabFac.createDataDescriptor();
327cdf0e10cSrcweir             // set the name of the new table
328cdf0e10cSrcweir             xTable.setPropertyValue("Name","SALESMAN");
329cdf0e10cSrcweir             // append the columns
330cdf0e10cSrcweir             XColumnsSupplier xColumSup = (XColumnsSupplier)UnoRuntime.queryInterface(XColumnsSupplier.class,xTable);
331cdf0e10cSrcweir             XDataDescriptorFactory xColFac = (XDataDescriptorFactory)UnoRuntime.queryInterface(XDataDescriptorFactory.class,xColumSup.getColumns());
332cdf0e10cSrcweir             XAppend xAppend = (XAppend)UnoRuntime.queryInterface(XAppend.class,xColFac);
333cdf0e10cSrcweir             // we only need one descriptor
334cdf0e10cSrcweir             XPropertySet xCol = xColFac.createDataDescriptor();
335cdf0e10cSrcweir             // create first column and append
336cdf0e10cSrcweir             xCol.setPropertyValue("Name","SNR");
337cdf0e10cSrcweir             xCol.setPropertyValue("Type",new Integer(DataType.INTEGER));
338cdf0e10cSrcweir             xCol.setPropertyValue("IsNullable",new Integer(ColumnValue.NO_NULLS));
339cdf0e10cSrcweir             xAppend.appendByDescriptor(xCol);
340cdf0e10cSrcweir             // 2nd only set the properties which differs
341cdf0e10cSrcweir             xCol.setPropertyValue("Name","FIRSTNAME");
342cdf0e10cSrcweir             xCol.setPropertyValue("Type",new Integer(DataType.VARCHAR));
343cdf0e10cSrcweir             xCol.setPropertyValue("IsNullable",new Integer(ColumnValue.NULLABLE));
344cdf0e10cSrcweir             xCol.setPropertyValue("Precision",new Integer(50));
345cdf0e10cSrcweir             xAppend.appendByDescriptor(xCol);
346cdf0e10cSrcweir             // 3nd only set the properties which differs
347cdf0e10cSrcweir             xCol.setPropertyValue("Name","LASTNAME");
348cdf0e10cSrcweir             xCol.setPropertyValue("Precision",new Integer(100));
349cdf0e10cSrcweir             xAppend.appendByDescriptor(xCol);
350cdf0e10cSrcweir             // 4nd only set the properties which differs
351cdf0e10cSrcweir             xCol.setPropertyValue("Name","STREET");
352cdf0e10cSrcweir             xCol.setPropertyValue("Precision",new Integer(50));
353cdf0e10cSrcweir             xAppend.appendByDescriptor(xCol);
354cdf0e10cSrcweir             // 5nd only set the properties which differs
355cdf0e10cSrcweir             xCol.setPropertyValue("Name","STATE");
356cdf0e10cSrcweir             xAppend.appendByDescriptor(xCol);
357cdf0e10cSrcweir             // 6nd only set the properties which differs
358cdf0e10cSrcweir             xCol.setPropertyValue("Name","ZIP");
359cdf0e10cSrcweir             xCol.setPropertyValue("Type",new Integer(DataType.INTEGER));
360cdf0e10cSrcweir             xCol.setPropertyValue("Precision",new Integer(10)); // default value integer
361cdf0e10cSrcweir             xAppend.appendByDescriptor(xCol);
362cdf0e10cSrcweir             // 7nd only set the properties which differs
363cdf0e10cSrcweir             xCol.setPropertyValue("Name","BIRTHDATE");
364cdf0e10cSrcweir             xCol.setPropertyValue("Type",new Integer(DataType.DATE));
365cdf0e10cSrcweir             xCol.setPropertyValue("Precision",new Integer(10)); // default value integer
366cdf0e10cSrcweir             xAppend.appendByDescriptor(xCol);
367cdf0e10cSrcweir             // now we create the primary key
368cdf0e10cSrcweir             XKeysSupplier xKeySup = (XKeysSupplier)UnoRuntime.queryInterface(XKeysSupplier.class,xTable);
369cdf0e10cSrcweir             XDataDescriptorFactory xKeyFac = (XDataDescriptorFactory)UnoRuntime.queryInterface(XDataDescriptorFactory.class,xKeySup.getKeys());
370cdf0e10cSrcweir             XAppend xKeyAppend = (XAppend)UnoRuntime.queryInterface(XAppend.class,xKeyFac);
371cdf0e10cSrcweir             XPropertySet xKey = xKeyFac.createDataDescriptor();
372cdf0e10cSrcweir             xKey.setPropertyValue("Type",new Integer(KeyType.PRIMARY));
373cdf0e10cSrcweir             // now append the columns to key
374cdf0e10cSrcweir             XColumnsSupplier xKeyColumSup = (XColumnsSupplier)UnoRuntime.queryInterface(XColumnsSupplier.class,xKey);
375cdf0e10cSrcweir             XDataDescriptorFactory xKeyColFac = (XDataDescriptorFactory)UnoRuntime.queryInterface(XDataDescriptorFactory.class,xKeyColumSup.getColumns());
376cdf0e10cSrcweir             XAppend xKeyColAppend = (XAppend)UnoRuntime.queryInterface(XAppend.class,xKeyColFac);
377cdf0e10cSrcweir             // we only need one descriptor
378cdf0e10cSrcweir             XPropertySet xKeyCol = xKeyColFac.createDataDescriptor();
379cdf0e10cSrcweir             xKeyCol.setPropertyValue("Name","SNR");
380cdf0e10cSrcweir             // append the key column
381cdf0e10cSrcweir             xKeyColAppend.appendByDescriptor(xKeyCol);
382cdf0e10cSrcweir             // apend the key
383cdf0e10cSrcweir             xKeyAppend.appendByDescriptor(xKey);
384cdf0e10cSrcweir             // the last step is to append the new table to the tables collection
385cdf0e10cSrcweir              XAppend xTableAppend = (XAppend)UnoRuntime.queryInterface(XAppend.class,xTabFac);
386cdf0e10cSrcweir              xTableAppend.appendByDescriptor(xTable);
387cdf0e10cSrcweir         }
388cdf0e10cSrcweir     }
389cdf0e10cSrcweir 
390cdf0e10cSrcweir     //###########################################################
391cdf0e10cSrcweir     // 23. example
392cdf0e10cSrcweir     // create a user
393cdf0e10cSrcweir     //###########################################################
394cdf0e10cSrcweir     public static void createUser(XNameAccess xUsers) throws com.sun.star.uno.Exception,SQLException
395cdf0e10cSrcweir     {
396cdf0e10cSrcweir         System.out.println("Example createUser");
397cdf0e10cSrcweir         XDataDescriptorFactory xUserFac = (XDataDescriptorFactory)UnoRuntime.queryInterface(XDataDescriptorFactory.class,xUsers);
398cdf0e10cSrcweir         if(xUserFac != null)
399cdf0e10cSrcweir         {
400cdf0e10cSrcweir             // create the new table
401cdf0e10cSrcweir             XPropertySet xUser = xUserFac.createDataDescriptor();
402cdf0e10cSrcweir             // set the name of the new table
403cdf0e10cSrcweir             xUser.setPropertyValue("Name","BOSS");
404cdf0e10cSrcweir             xUser.setPropertyValue("Password","BOSSWIFENAME");
405cdf0e10cSrcweir             XAppend xAppend = (XAppend)UnoRuntime.queryInterface(XAppend.class,xUserFac);
406cdf0e10cSrcweir             xAppend.appendByDescriptor(xUser);
407cdf0e10cSrcweir         }
408cdf0e10cSrcweir     }
409cdf0e10cSrcweir }
410