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