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