1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
34 *************************************************************************/
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
;
41 public class OpenQuery
{
44 * @param args the command line arguments
46 public static void main(String
[] args
) {
47 OpenQuery openQuery1
= new OpenQuery();
49 openQuery1
.openQuery();
51 catch (java
.lang
.Exception e
){
59 protected void openQuery() throws com
.sun
.star
.uno
.Exception
, java
.lang
.Exception
{
60 XComponentContext xContext
= null;
61 XMultiComponentFactory xMCF
= null;
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();
69 System
.err
.println("ERROR: can't get a component context from a running office ...");
74 // first we create our RowSet object and get its XRowSet interface
75 Object rowSet
= xMCF
.createInstanceWithContext(
76 "com.sun.star.sdb.RowSet", xContext
);
78 com
.sun
.star
.sdbc
.XRowSet xRowSet
= UnoRuntime
.queryInterface(com
.sun
.star
.sdbc
.XRowSet
.class, rowSet
);
80 // set the properties needed to connect to a database
81 XPropertySet xProp
= UnoRuntime
.queryInterface(XPropertySet
.class, xRowSet
);
83 // the DataSourceName can be a data source registered with [PRODUCTNAME], among other possibilities
84 xProp
.setPropertyValue("DataSourceName","Bibliography");
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
));
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");
92 // if your database requires logon, you can use the properties User and Password
93 // xProp.setPropertyValue("User", "JohnDoe");
94 // xProp.setPropertyValue("Password", "mysecret");
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
);
106 // print output header
107 System
.out
.println("Identifier\tAuthor");
108 System
.out
.println("----------\t------");
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
);
117 // XResultSetUpdate for insertRow handling
118 com
.sun
.star
.sdbc
.XResultSetUpdate xResultSetUpdate
= UnoRuntime
.queryInterface(
119 com
.sun
.star
.sdbc
.XResultSetUpdate
.class, xRowSet
);
121 // XRowUpdate for row updates
122 com
.sun
.star
.sdbc
.XRowUpdate xRowUpdate
= UnoRuntime
.queryInterface(
123 com
.sun
.star
.sdbc
.XRowUpdate
.class, xRowSet
);
125 // move to insertRow buffer
126 xResultSetUpdate
.moveToInsertRow();
128 // edit insertRow buffer
129 xRowUpdate
.updateString(xLoc
.findColumn("IDENTIFIER"), "GOF95");
130 xRowUpdate
.updateString(xLoc
.findColumn("AUTHOR"), "Gamma, Helm, Johnson, Vlissides");
132 // write buffer to database
133 xResultSetUpdate
.insertRow();
135 // throw away the row set
136 com
.sun
.star
.lang
.XComponent xComp
= UnoRuntime
.queryInterface(
137 com
.sun
.star
.lang
.XComponent
.class, xRowSet
);
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */