1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * The Contents of this file are made available subject to the terms of 4*cdf0e10cSrcweir * the BSD license. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 7*cdf0e10cSrcweir * All rights reserved. 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * Redistribution and use in source and binary forms, with or without 10*cdf0e10cSrcweir * modification, are permitted provided that the following conditions 11*cdf0e10cSrcweir * are met: 12*cdf0e10cSrcweir * 1. Redistributions of source code must retain the above copyright 13*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer. 14*cdf0e10cSrcweir * 2. Redistributions in binary form must reproduce the above copyright 15*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer in the 16*cdf0e10cSrcweir * documentation and/or other materials provided with the distribution. 17*cdf0e10cSrcweir * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18*cdf0e10cSrcweir * contributors may be used to endorse or promote products derived 19*cdf0e10cSrcweir * from this software without specific prior written permission. 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*cdf0e10cSrcweir * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*cdf0e10cSrcweir * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*cdf0e10cSrcweir * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*cdf0e10cSrcweir * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*cdf0e10cSrcweir * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*cdf0e10cSrcweir * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28*cdf0e10cSrcweir * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29*cdf0e10cSrcweir * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30*cdf0e10cSrcweir * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31*cdf0e10cSrcweir * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*cdf0e10cSrcweir * 33*cdf0e10cSrcweir *************************************************************************/ 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir import java.io.*; 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir import com.sun.star.comp.helper.RegistryServiceFactory; 38*cdf0e10cSrcweir import com.sun.star.comp.servicemanager.ServiceManager; 39*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 40*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 41*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 42*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 43*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 44*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 45*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 46*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 47*cdf0e10cSrcweir import com.sun.star.container.XNameAccess; 48*cdf0e10cSrcweir import com.sun.star.container.XNameContainer; 49*cdf0e10cSrcweir import com.sun.star.sdbc.*; 50*cdf0e10cSrcweir import com.sun.star.sdb.*; 51*cdf0e10cSrcweir import com.sun.star.sdbcx.*; 52*cdf0e10cSrcweir import com.sun.star.frame.*; 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir public class CodeSamples 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir public static XComponentContext xContext; 57*cdf0e10cSrcweir public static XMultiComponentFactory xMCF; 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir public static void main(String argv[]) throws java.lang.Exception 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir try { 62*cdf0e10cSrcweir // get the remote office component context 63*cdf0e10cSrcweir xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 64*cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 65*cdf0e10cSrcweir xMCF = xContext.getServiceManager(); 66*cdf0e10cSrcweir } 67*cdf0e10cSrcweir catch(Exception e) { 68*cdf0e10cSrcweir System.err.println("ERROR: can't get a component context from a running office ..."); 69*cdf0e10cSrcweir e.printStackTrace(); 70*cdf0e10cSrcweir System.exit(1); 71*cdf0e10cSrcweir } 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir try{ 74*cdf0e10cSrcweir createQuerydefinition( ); 75*cdf0e10cSrcweir printQueryColumnNames( ); 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir XConnection con = openConnectionWithDriverManager(); 78*cdf0e10cSrcweir if ( con != null ) { 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir SalesMan sm = new SalesMan( con ); 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir try { 83*cdf0e10cSrcweir sm.dropSalesManTable( ); // doesn't matter here 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir sm.createSalesManTable( ); 89*cdf0e10cSrcweir sm.insertDataIntoSalesMan( ); 90*cdf0e10cSrcweir sm.updateSalesMan( ); 91*cdf0e10cSrcweir sm.retrieveSalesManData( ); 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir Sales sm = new Sales( con ); 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir try { 98*cdf0e10cSrcweir sm.dropSalesTable( ); // doesn't matter here 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) 101*cdf0e10cSrcweir { 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir sm.createSalesTable( ); 104*cdf0e10cSrcweir sm.insertDataIntoSales( ); 105*cdf0e10cSrcweir sm.updateSales( ); 106*cdf0e10cSrcweir sm.retrieveSalesData( ); 107*cdf0e10cSrcweir sm.displayColumnNames( ); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir displayTableStructure( con ); 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir // printDataSources(); 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir catch(Exception e) 114*cdf0e10cSrcweir { 115*cdf0e10cSrcweir System.err.println(e); 116*cdf0e10cSrcweir e.printStackTrace(); 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir System.exit(0); 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir // check if the connection is not null aand dispose it later on. 122*cdf0e10cSrcweir public static void checkConnection(XConnection con) throws com.sun.star.uno.Exception 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir if(con != null) 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir System.out.println("Connection was created!"); 127*cdf0e10cSrcweir // now we dispose the connection to close it 128*cdf0e10cSrcweir XComponent xComponent = (XComponent)UnoRuntime.queryInterface(XComponent.class,con); 129*cdf0e10cSrcweir if(xComponent != null) 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir // connections must be disposed 132*cdf0e10cSrcweir xComponent.dispose(); 133*cdf0e10cSrcweir System.out.println("Connection disposed!"); 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir else 137*cdf0e10cSrcweir System.out.println("Connection could not be created!"); 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir // uses the driver manager to create a new connection and dispose it. 141*cdf0e10cSrcweir public static XConnection openConnectionWithDriverManager() throws com.sun.star.uno.Exception 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir XConnection con = null; 144*cdf0e10cSrcweir // create the DriverManager 145*cdf0e10cSrcweir Object driverManager = 146*cdf0e10cSrcweir xMCF.createInstanceWithContext("com.sun.star.sdbc.DriverManager", 147*cdf0e10cSrcweir xContext); 148*cdf0e10cSrcweir // query for the interface 149*cdf0e10cSrcweir com.sun.star.sdbc.XDriverManager xDriverManager; 150*cdf0e10cSrcweir xDriverManager = (XDriverManager)UnoRuntime.queryInterface(XDriverManager.class,driverManager); 151*cdf0e10cSrcweir if(xDriverManager != null) 152*cdf0e10cSrcweir { 153*cdf0e10cSrcweir // first create the needed url 154*cdf0e10cSrcweir String url = "jdbc:mysql://localhost:3306/TestTables"; 155*cdf0e10cSrcweir // second create the necessary properties 156*cdf0e10cSrcweir com.sun.star.beans.PropertyValue [] props = new com.sun.star.beans.PropertyValue[] 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir new com.sun.star.beans.PropertyValue("user",0,"test1",com.sun.star.beans.PropertyState.DIRECT_VALUE), 159*cdf0e10cSrcweir new com.sun.star.beans.PropertyValue("password",0,"test1",com.sun.star.beans.PropertyState.DIRECT_VALUE), 160*cdf0e10cSrcweir new com.sun.star.beans.PropertyValue("JavaDriverClass",0,"org.gjt.mm.mysql.Driver",com.sun.star.beans.PropertyState.DIRECT_VALUE) 161*cdf0e10cSrcweir }; 162*cdf0e10cSrcweir // now create a connection to mysql 163*cdf0e10cSrcweir con = xDriverManager.getConnectionWithInfo(url,props); 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir return con; 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir // uses the driver directly to create a new connection and dispose it. 169*cdf0e10cSrcweir public static XConnection openConnectionWithDriver() throws com.sun.star.uno.Exception 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir XConnection con = null; 172*cdf0e10cSrcweir // create the Driver with the implementation name 173*cdf0e10cSrcweir Object aDriver = 174*cdf0e10cSrcweir xMCF.createInstanceWithContext("org.openoffice.comp.drivers.MySQL.Driver", 175*cdf0e10cSrcweir xContext); 176*cdf0e10cSrcweir // query for the interface 177*cdf0e10cSrcweir com.sun.star.sdbc.XDriver xDriver; 178*cdf0e10cSrcweir xDriver = (XDriver)UnoRuntime.queryInterface(XDriver.class,aDriver); 179*cdf0e10cSrcweir if(xDriver != null) 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir // first create the needed url 182*cdf0e10cSrcweir String url = "jdbc:mysql://localhost:3306/TestTables"; 183*cdf0e10cSrcweir // second create the necessary properties 184*cdf0e10cSrcweir com.sun.star.beans.PropertyValue [] props = new com.sun.star.beans.PropertyValue[] 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir new com.sun.star.beans.PropertyValue("user",0,"test1",com.sun.star.beans.PropertyState.DIRECT_VALUE), 187*cdf0e10cSrcweir new com.sun.star.beans.PropertyValue("password",0,"test1",com.sun.star.beans.PropertyState.DIRECT_VALUE), 188*cdf0e10cSrcweir new com.sun.star.beans.PropertyValue("JavaDriverClass",0,"org.gjt.mm.mysql.Driver",com.sun.star.beans.PropertyState.DIRECT_VALUE) 189*cdf0e10cSrcweir }; 190*cdf0e10cSrcweir // now create a connection to mysql 191*cdf0e10cSrcweir con = xDriver.connect(url,props); 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir return con; 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir // print all available datasources 197*cdf0e10cSrcweir public static void printDataSources() throws com.sun.star.uno.Exception 198*cdf0e10cSrcweir { 199*cdf0e10cSrcweir // create a DatabaseContext and print all DataSource names 200*cdf0e10cSrcweir XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( 201*cdf0e10cSrcweir XNameAccess.class, 202*cdf0e10cSrcweir xMCF.createInstanceWithContext("com.sun.star.sdb.DatabaseContext", 203*cdf0e10cSrcweir xContext)); 204*cdf0e10cSrcweir String aNames [] = xNameAccess.getElementNames(); 205*cdf0e10cSrcweir for(int i=0;i<aNames.length;++i) 206*cdf0e10cSrcweir System.out.println(aNames[i]); 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir // displays the structure of the first table 210*cdf0e10cSrcweir public static void displayTableStructure(XConnection con) throws com.sun.star.uno.Exception 211*cdf0e10cSrcweir { 212*cdf0e10cSrcweir XDatabaseMetaData dm = con.getMetaData(); 213*cdf0e10cSrcweir XResultSet rsTables = dm.getTables(null,"%","SALES",null); 214*cdf0e10cSrcweir XRow rowTB = (XRow)UnoRuntime.queryInterface(XRow.class, rsTables); 215*cdf0e10cSrcweir while ( rsTables.next() ) 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir String catalog = rowTB.getString( 1 ); 218*cdf0e10cSrcweir if ( rowTB.wasNull() ) 219*cdf0e10cSrcweir catalog = null; 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir String schema = rowTB.getString( 2 ); 222*cdf0e10cSrcweir if ( rowTB.wasNull() ) 223*cdf0e10cSrcweir schema = null; 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir String table = rowTB.getString( 3 ); 226*cdf0e10cSrcweir String type = rowTB.getString( 4 ); 227*cdf0e10cSrcweir System.out.println("Catalog: " + catalog + " Schema: " + schema + " Table: " + table + " Type: " + type); 228*cdf0e10cSrcweir System.out.println("------------------ Columns ------------------"); 229*cdf0e10cSrcweir XResultSet rsColumns = dm.getColumns(catalog,schema,table,"%"); 230*cdf0e10cSrcweir XRow rowCL = (XRow)UnoRuntime.queryInterface(XRow.class, rsColumns); 231*cdf0e10cSrcweir while ( rsColumns.next() ) 232*cdf0e10cSrcweir { 233*cdf0e10cSrcweir System.out.println("Column: " + rowCL.getString( 4 ) + " Type: " + rowCL.getInt( 5 ) + " TypeName: " + rowCL.getString( 6 ) ); 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir } 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir // quote the given name 240*cdf0e10cSrcweir public static String quoteTableName(XConnection con, String sCatalog, String sSchema, String sTable) throws com.sun.star.uno.Exception 241*cdf0e10cSrcweir { 242*cdf0e10cSrcweir XDatabaseMetaData dbmd = con.getMetaData(); 243*cdf0e10cSrcweir String sQuoteString = dbmd.getIdentifierQuoteString(); 244*cdf0e10cSrcweir String sSeparator = "."; 245*cdf0e10cSrcweir String sComposedName = ""; 246*cdf0e10cSrcweir String sCatalogSep = dbmd.getCatalogSeparator(); 247*cdf0e10cSrcweir if (0 != sCatalog.length() && dbmd.isCatalogAtStart() && 0 != sCatalogSep.length()) 248*cdf0e10cSrcweir { 249*cdf0e10cSrcweir sComposedName += sCatalog; 250*cdf0e10cSrcweir sComposedName += dbmd.getCatalogSeparator(); 251*cdf0e10cSrcweir } 252*cdf0e10cSrcweir if (0 != sSchema.length()) 253*cdf0e10cSrcweir { 254*cdf0e10cSrcweir sComposedName += sSchema; 255*cdf0e10cSrcweir sComposedName += sSeparator; 256*cdf0e10cSrcweir sComposedName += sTable; 257*cdf0e10cSrcweir } 258*cdf0e10cSrcweir else 259*cdf0e10cSrcweir { 260*cdf0e10cSrcweir sComposedName += sTable; 261*cdf0e10cSrcweir } 262*cdf0e10cSrcweir if (0 != sCatalog.length() && !dbmd.isCatalogAtStart() && 0 != sCatalogSep.length()) 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir sComposedName += dbmd.getCatalogSeparator(); 265*cdf0e10cSrcweir sComposedName += sCatalog; 266*cdf0e10cSrcweir } 267*cdf0e10cSrcweir return sComposedName; 268*cdf0e10cSrcweir } 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir // creates a new query definition 271*cdf0e10cSrcweir public static void createQuerydefinition() throws com.sun.star.uno.Exception 272*cdf0e10cSrcweir { 273*cdf0e10cSrcweir XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( 274*cdf0e10cSrcweir XNameAccess.class, 275*cdf0e10cSrcweir xMCF.createInstanceWithContext("com.sun.star.sdb.DatabaseContext", 276*cdf0e10cSrcweir xContext)); 277*cdf0e10cSrcweir // we use the first datasource 278*cdf0e10cSrcweir XQueryDefinitionsSupplier xQuerySup = (XQueryDefinitionsSupplier) 279*cdf0e10cSrcweir UnoRuntime.queryInterface(XQueryDefinitionsSupplier.class, 280*cdf0e10cSrcweir xNameAccess.getByName( "Bibliography" )); 281*cdf0e10cSrcweir XNameAccess xQDefs = xQuerySup.getQueryDefinitions(); 282*cdf0e10cSrcweir // create new query definition 283*cdf0e10cSrcweir XSingleServiceFactory xSingleFac = (XSingleServiceFactory) 284*cdf0e10cSrcweir UnoRuntime.queryInterface(XSingleServiceFactory.class, xQDefs); 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface( 287*cdf0e10cSrcweir XPropertySet.class,xSingleFac.createInstance()); 288*cdf0e10cSrcweir xProp.setPropertyValue("Command","SELECT * FROM biblio"); 289*cdf0e10cSrcweir xProp.setPropertyValue("EscapeProcessing",new Boolean(true)); 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir XNameContainer xCont = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xQDefs); 292*cdf0e10cSrcweir try 293*cdf0e10cSrcweir { 294*cdf0e10cSrcweir if ( xCont.hasByName("Query1") ) 295*cdf0e10cSrcweir xCont.removeByName("Query1"); 296*cdf0e10cSrcweir } 297*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) 298*cdf0e10cSrcweir {} 299*cdf0e10cSrcweir xCont.insertByName("Query1",xProp); 300*cdf0e10cSrcweir XDocumentDataSource xDs = (XDocumentDataSource)UnoRuntime.queryInterface(XDocumentDataSource.class, xQuerySup); 301*cdf0e10cSrcweir 302*cdf0e10cSrcweir XStorable xStore = (XStorable)UnoRuntime.queryInterface(XStorable.class,xDs.getDatabaseDocument()); 303*cdf0e10cSrcweir xStore.store(); 304*cdf0e10cSrcweir } 305*cdf0e10cSrcweir 306*cdf0e10cSrcweir // prints all column names from Query1 307*cdf0e10cSrcweir public static void printQueryColumnNames() throws com.sun.star.uno.Exception 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( 310*cdf0e10cSrcweir XNameAccess.class, 311*cdf0e10cSrcweir xMCF.createInstanceWithContext("com.sun.star.sdb.DatabaseContext", 312*cdf0e10cSrcweir xContext)); 313*cdf0e10cSrcweir // we use the first datasource 314*cdf0e10cSrcweir XDataSource xDS = (XDataSource)UnoRuntime.queryInterface( 315*cdf0e10cSrcweir XDataSource.class, xNameAccess.getByName( "Bibliography" )); 316*cdf0e10cSrcweir XConnection con = xDS.getConnection("",""); 317*cdf0e10cSrcweir XQueriesSupplier xQuerySup = (XQueriesSupplier) 318*cdf0e10cSrcweir UnoRuntime.queryInterface(XQueriesSupplier.class, con); 319*cdf0e10cSrcweir 320*cdf0e10cSrcweir XNameAccess xQDefs = xQuerySup.getQueries(); 321*cdf0e10cSrcweir 322*cdf0e10cSrcweir XColumnsSupplier xColsSup = (XColumnsSupplier) UnoRuntime.queryInterface( 323*cdf0e10cSrcweir XColumnsSupplier.class,xQDefs.getByName("Query1")); 324*cdf0e10cSrcweir XNameAccess xCols = xColsSup.getColumns(); 325*cdf0e10cSrcweir String aNames [] = xCols.getElementNames(); 326*cdf0e10cSrcweir for(int i=0;i<aNames.length;++i) 327*cdf0e10cSrcweir System.out.println(aNames[i]); 328*cdf0e10cSrcweir } 329*cdf0e10cSrcweir } 330*cdf0e10cSrcweir 331