1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /************************************************************************* 3 * 4 * The Contents of this file are made available subject to the terms of 5 * the BSD license. 6 * 7 * Copyright 2000, 2010 Oracle and/or its affiliates. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 *************************************************************************/ 35 36 import com.sun.star.uno.UnoRuntime; 37 import com.sun.star.uno.XComponentContext; 38 import com.sun.star.lang.XMultiComponentFactory; 39 import com.sun.star.beans.XPropertySet; 40 41 public class OpenQuery { 42 43 /** 44 * @param args the command line arguments 45 */ main(String[] args)46 public static void main(String[] args) { 47 OpenQuery openQuery1 = new OpenQuery(); 48 try { 49 openQuery1.openQuery(); 50 } 51 catch (java.lang.Exception e){ 52 e.printStackTrace(); 53 } 54 finally { 55 System.exit(0); 56 } 57 } 58 openQuery()59 protected void openQuery() throws com.sun.star.uno.Exception, java.lang.Exception { 60 XComponentContext xContext = null; 61 XMultiComponentFactory xMCF = null; 62 try { 63 // get the remote office component context 64 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 65 System.out.println("Connected to a running office ..."); 66 xMCF = xContext.getServiceManager(); 67 } 68 catch( Exception e) { 69 System.err.println("ERROR: can't get a component context from a running office ..."); 70 e.printStackTrace(); 71 System.exit(1); 72 } 73 74 // first we create our RowSet object and get its XRowSet interface 75 Object rowSet = xMCF.createInstanceWithContext( 76 "com.sun.star.sdb.RowSet", xContext); 77 78 com.sun.star.sdbc.XRowSet xRowSet = UnoRuntime.queryInterface(com.sun.star.sdbc.XRowSet.class, rowSet); 79 80 // set the properties needed to connect to a database 81 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class, xRowSet); 82 83 // the DataSourceName can be a data source registered with [PRODUCTNAME], among other possibilities 84 xProp.setPropertyValue("DataSourceName","Bibliography"); 85 86 // the CommandType must be TABLE, QUERY or COMMAND, here we use COMMAND 87 xProp.setPropertyValue("CommandType",Integer.valueOf(com.sun.star.sdb.CommandType.COMMAND)); 88 89 // the Command could be a table or query name or a SQL command, depending on the CommandType 90 xProp.setPropertyValue("Command","SELECT IDENTIFIER, AUTHOR FROM biblio ORDER BY IDENTIFIER"); 91 92 // if your database requires logon, you can use the properties User and Password 93 // xProp.setPropertyValue("User", "JohnDoe"); 94 // xProp.setPropertyValue("Password", "mysecret"); 95 96 xRowSet.execute(); 97 98 // prepare the XRow and XColumnLocate interface for column access 99 // XRow gets column values 100 com.sun.star.sdbc.XRow xRow = UnoRuntime.queryInterface( 101 com.sun.star.sdbc.XRow.class, xRowSet); 102 // XColumnLocate finds columns by name 103 com.sun.star.sdbc.XColumnLocate xLoc = UnoRuntime.queryInterface( 104 com.sun.star.sdbc.XColumnLocate.class, xRowSet); 105 106 // print output header 107 System.out.println("Identifier\tAuthor"); 108 System.out.println("----------\t------"); 109 110 // output result rows 111 while ( xRowSet.next() ) { 112 String ident = xRow.getString(xLoc.findColumn("IDENTIFIER")); 113 String author = xRow.getString(xLoc.findColumn("AUTHOR")); 114 System.out.println(ident + "\t\t" + author); 115 } 116 117 // XResultSetUpdate for insertRow handling 118 com.sun.star.sdbc.XResultSetUpdate xResultSetUpdate = UnoRuntime.queryInterface( 119 com.sun.star.sdbc.XResultSetUpdate.class, xRowSet); 120 121 // XRowUpdate for row updates 122 com.sun.star.sdbc.XRowUpdate xRowUpdate = UnoRuntime.queryInterface( 123 com.sun.star.sdbc.XRowUpdate.class, xRowSet); 124 125 // move to insertRow buffer 126 xResultSetUpdate.moveToInsertRow(); 127 128 // edit insertRow buffer 129 xRowUpdate.updateString(xLoc.findColumn("IDENTIFIER"), "GOF95"); 130 xRowUpdate.updateString(xLoc.findColumn("AUTHOR"), "Gamma, Helm, Johnson, Vlissides"); 131 132 // write buffer to database 133 xResultSetUpdate.insertRow(); 134 135 // throw away the row set 136 com.sun.star.lang.XComponent xComp = UnoRuntime.queryInterface( 137 com.sun.star.lang.XComponent.class, xRowSet); 138 xComp.dispose(); 139 } 140 141 } 142 143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 144