1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
35 import com
.sun
.star
.lang
.XMultiComponentFactory
;
36 import com
.sun
.star
.lang
.XSingleServiceFactory
;
37 import com
.sun
.star
.lang
.XComponent
;
38 import com
.sun
.star
.uno
.UnoRuntime
;
39 import com
.sun
.star
.uno
.XComponentContext
;
40 import com
.sun
.star
.beans
.XPropertySet
;
41 import com
.sun
.star
.container
.XNameAccess
;
42 import com
.sun
.star
.container
.XNameContainer
;
43 import com
.sun
.star
.sdbc
.*;
44 import com
.sun
.star
.sdb
.*;
45 import com
.sun
.star
.sdbcx
.*;
46 import com
.sun
.star
.frame
.*;
48 public class CodeSamples
50 public static XComponentContext xContext
;
51 public static XMultiComponentFactory xMCF
;
53 public static void main(String argv
[]) throws java
.lang
.Exception
56 // get the remote office component context
57 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
58 System
.out
.println("Connected to a running office ...");
59 xMCF
= xContext
.getServiceManager();
62 System
.err
.println("ERROR: can't get a component context from a running office ...");
68 createQuerydefinition( );
69 printQueryColumnNames( );
71 XConnection con
= openConnectionWithDriverManager();
74 SalesMan sm
= new SalesMan( con
);
77 sm
.dropSalesManTable( ); // doesn't matter here
79 catch(com
.sun
.star
.uno
.Exception e
)
82 sm
.createSalesManTable( );
83 sm
.insertDataIntoSalesMan( );
85 sm
.retrieveSalesManData( );
89 Sales sm
= new Sales( con
);
92 sm
.dropSalesTable( ); // doesn't matter here
94 catch(com
.sun
.star
.uno
.Exception e
)
97 sm
.createSalesTable( );
98 sm
.insertDataIntoSales( );
100 sm
.retrieveSalesData( );
101 sm
.displayColumnNames( );
103 displayTableStructure( con
);
105 // printDataSources();
109 System
.err
.println(e
);
115 // check if the connection is not null aand dispose it later on.
116 public static void checkConnection(XConnection con
) throws com
.sun
.star
.uno
.Exception
120 System
.out
.println("Connection was created!");
121 // now we dispose the connection to close it
122 XComponent xComponent
= UnoRuntime
.queryInterface(XComponent
.class,con
);
123 if(xComponent
!= null)
125 // connections must be disposed
126 xComponent
.dispose();
127 System
.out
.println("Connection disposed!");
131 System
.out
.println("Connection could not be created!");
134 // uses the driver manager to create a new connection and dispose it.
135 public static XConnection
openConnectionWithDriverManager() throws com
.sun
.star
.uno
.Exception
137 XConnection con
= null;
138 // create the DriverManager
139 Object driverManager
=
140 xMCF
.createInstanceWithContext("com.sun.star.sdbc.DriverManager",
142 // query for the interface
143 com
.sun
.star
.sdbc
.XDriverManager xDriverManager
;
144 xDriverManager
= UnoRuntime
.queryInterface(XDriverManager
.class,driverManager
);
145 if(xDriverManager
!= null)
147 // first create the needed url
148 String url
= "jdbc:mysql://localhost:3306/TestTables";
149 // second create the necessary properties
150 com
.sun
.star
.beans
.PropertyValue
[] props
= new com
.sun
.star
.beans
.PropertyValue
[]
152 new com
.sun
.star
.beans
.PropertyValue("user",0,"test1",com
.sun
.star
.beans
.PropertyState
.DIRECT_VALUE
),
153 new com
.sun
.star
.beans
.PropertyValue("password",0,"test1",com
.sun
.star
.beans
.PropertyState
.DIRECT_VALUE
),
154 new com
.sun
.star
.beans
.PropertyValue("JavaDriverClass",0,"org.gjt.mm.mysql.Driver",com
.sun
.star
.beans
.PropertyState
.DIRECT_VALUE
)
156 // now create a connection to mysql
157 con
= xDriverManager
.getConnectionWithInfo(url
,props
);
162 // uses the driver directly to create a new connection and dispose it.
163 public static XConnection
openConnectionWithDriver() throws com
.sun
.star
.uno
.Exception
165 XConnection con
= null;
166 // create the Driver with the implementation name
168 xMCF
.createInstanceWithContext("org.openoffice.comp.drivers.MySQL.Driver",
170 // query for the interface
171 com
.sun
.star
.sdbc
.XDriver xDriver
;
172 xDriver
= UnoRuntime
.queryInterface(XDriver
.class,aDriver
);
175 // first create the needed url
176 String url
= "jdbc:mysql://localhost:3306/TestTables";
177 // second create the necessary properties
178 com
.sun
.star
.beans
.PropertyValue
[] props
= new com
.sun
.star
.beans
.PropertyValue
[]
180 new com
.sun
.star
.beans
.PropertyValue("user",0,"test1",com
.sun
.star
.beans
.PropertyState
.DIRECT_VALUE
),
181 new com
.sun
.star
.beans
.PropertyValue("password",0,"test1",com
.sun
.star
.beans
.PropertyState
.DIRECT_VALUE
),
182 new com
.sun
.star
.beans
.PropertyValue("JavaDriverClass",0,"org.gjt.mm.mysql.Driver",com
.sun
.star
.beans
.PropertyState
.DIRECT_VALUE
)
184 // now create a connection to mysql
185 con
= xDriver
.connect(url
,props
);
190 // print all available datasources
191 public static void printDataSources() throws com
.sun
.star
.uno
.Exception
193 // create a DatabaseContext and print all DataSource names
194 XNameAccess xNameAccess
= UnoRuntime
.queryInterface(
196 xMCF
.createInstanceWithContext("com.sun.star.sdb.DatabaseContext",
198 String aNames
[] = xNameAccess
.getElementNames();
199 for(int i
=0;i
<aNames
.length
;++i
)
200 System
.out
.println(aNames
[i
]);
203 // displays the structure of the first table
204 public static void displayTableStructure(XConnection con
) throws com
.sun
.star
.uno
.Exception
206 XDatabaseMetaData dm
= con
.getMetaData();
207 XResultSet rsTables
= dm
.getTables(null,"%","SALES",null);
208 XRow rowTB
= UnoRuntime
.queryInterface(XRow
.class, rsTables
);
209 while ( rsTables
.next() )
211 String catalog
= rowTB
.getString( 1 );
212 if ( rowTB
.wasNull() )
215 String schema
= rowTB
.getString( 2 );
216 if ( rowTB
.wasNull() )
219 String table
= rowTB
.getString( 3 );
220 String type
= rowTB
.getString( 4 );
221 System
.out
.println("Catalog: " + catalog
+ " Schema: " + schema
+ " Table: " + table
+ " Type: " + type
);
222 System
.out
.println("------------------ Columns ------------------");
223 XResultSet rsColumns
= dm
.getColumns(catalog
,schema
,table
,"%");
224 XRow rowCL
= UnoRuntime
.queryInterface(XRow
.class, rsColumns
);
225 while ( rsColumns
.next() )
227 System
.out
.println("Column: " + rowCL
.getString( 4 ) + " Type: " + rowCL
.getInt( 5 ) + " TypeName: " + rowCL
.getString( 6 ) );
233 // quote the given name
234 public static String
quoteTableName(XConnection con
, String sCatalog
, String sSchema
, String sTable
) throws com
.sun
.star
.uno
.Exception
236 XDatabaseMetaData dbmd
= con
.getMetaData();
237 String sQuoteString
= dbmd
.getIdentifierQuoteString();
238 String sSeparator
= ".";
239 String sComposedName
= "";
240 String sCatalogSep
= dbmd
.getCatalogSeparator();
241 if (0 != sCatalog
.length() && dbmd
.isCatalogAtStart() && 0 != sCatalogSep
.length())
243 sComposedName
+= sCatalog
;
244 sComposedName
+= dbmd
.getCatalogSeparator();
246 if (0 != sSchema
.length())
248 sComposedName
+= sSchema
;
249 sComposedName
+= sSeparator
;
250 sComposedName
+= sTable
;
254 sComposedName
+= sTable
;
256 if (0 != sCatalog
.length() && !dbmd
.isCatalogAtStart() && 0 != sCatalogSep
.length())
258 sComposedName
+= dbmd
.getCatalogSeparator();
259 sComposedName
+= sCatalog
;
261 return sComposedName
;
264 // creates a new query definition
265 public static void createQuerydefinition() throws com
.sun
.star
.uno
.Exception
267 XNameAccess xNameAccess
= UnoRuntime
.queryInterface(
269 xMCF
.createInstanceWithContext("com.sun.star.sdb.DatabaseContext",
271 // we use the first datasource
272 XQueryDefinitionsSupplier xQuerySup
= UnoRuntime
.queryInterface(XQueryDefinitionsSupplier
.class,
273 xNameAccess
.getByName( "Bibliography" ));
274 XNameAccess xQDefs
= xQuerySup
.getQueryDefinitions();
275 // create new query definition
276 XSingleServiceFactory xSingleFac
= UnoRuntime
.queryInterface(XSingleServiceFactory
.class, xQDefs
);
278 XPropertySet xProp
= UnoRuntime
.queryInterface(
279 XPropertySet
.class,xSingleFac
.createInstance());
280 xProp
.setPropertyValue("Command","SELECT * FROM biblio");
281 xProp
.setPropertyValue("EscapeProcessing",new Boolean(true));
283 XNameContainer xCont
= UnoRuntime
.queryInterface(XNameContainer
.class, xQDefs
);
286 if ( xCont
.hasByName("Query1") )
287 xCont
.removeByName("Query1");
289 catch(com
.sun
.star
.uno
.Exception e
)
291 xCont
.insertByName("Query1",xProp
);
292 XDocumentDataSource xDs
= UnoRuntime
.queryInterface(XDocumentDataSource
.class, xQuerySup
);
294 XStorable xStore
= UnoRuntime
.queryInterface(XStorable
.class,xDs
.getDatabaseDocument());
298 // prints all column names from Query1
299 public static void printQueryColumnNames() throws com
.sun
.star
.uno
.Exception
301 XNameAccess xNameAccess
= UnoRuntime
.queryInterface(
303 xMCF
.createInstanceWithContext("com.sun.star.sdb.DatabaseContext",
305 // we use the first datasource
306 XDataSource xDS
= UnoRuntime
.queryInterface(
307 XDataSource
.class, xNameAccess
.getByName( "Bibliography" ));
308 XConnection con
= xDS
.getConnection("","");
309 XQueriesSupplier xQuerySup
= UnoRuntime
.queryInterface(XQueriesSupplier
.class, con
);
311 XNameAccess xQDefs
= xQuerySup
.getQueries();
313 XColumnsSupplier xColsSup
= UnoRuntime
.queryInterface(
314 XColumnsSupplier
.class,xQDefs
.getByName("Query1"));
315 XNameAccess xCols
= xColsSup
.getColumns();
316 String aNames
[] = xCols
.getElementNames();
317 for(int i
=0;i
<aNames
.length
;++i
)
318 System
.out
.println(aNames
[i
]);