1 /*************************************************************************
3 * $RCSfile: OpenQuery.java,v $
7 * last change: $Author: kz $ $Date: 2006-02-03 17:14:37 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com
.sun
.star
.bridge
.XUnoUrlResolver
;
42 import com
.sun
.star
.uno
.UnoRuntime
;
43 import com
.sun
.star
.uno
.XComponentContext
;
44 import com
.sun
.star
.lang
.XMultiComponentFactory
;
45 import com
.sun
.star
.beans
.XPropertySet
;
52 * Created on 6. Juli 2002, 10:25
59 public class OpenQuery
{
61 private XComponentContext xContext
= null;
62 private XMultiComponentFactory xMCF
= null;
64 /** Creates a new instance of OpenQuery */
69 * @param args the command line arguments
71 public static void main(String
[] args
) {
72 OpenQuery openQuery1
= new OpenQuery();
74 openQuery1
.openQuery();
76 catch (java
.lang
.Exception e
){
84 protected void openQuery() throws com
.sun
.star
.uno
.Exception
, java
.lang
.Exception
{
86 // get the remote office component context
87 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
88 System
.out
.println("Connected to a running office ...");
89 xMCF
= xContext
.getServiceManager();
92 System
.err
.println("ERROR: can't get a component context from a running office ...");
97 // first we create our RowSet object and get its XRowSet interface
98 Object rowSet
= xMCF
.createInstanceWithContext(
99 "com.sun.star.sdb.RowSet", xContext
);
101 com
.sun
.star
.sdbc
.XRowSet xRowSet
= (com
.sun
.star
.sdbc
.XRowSet
)
102 UnoRuntime
.queryInterface(com
.sun
.star
.sdbc
.XRowSet
.class, rowSet
);
104 // set the properties needed to connect to a database
105 XPropertySet xProp
= (XPropertySet
)UnoRuntime
.queryInterface(XPropertySet
.class, xRowSet
);
107 // the DataSourceName can be a data source registered with [PRODUCTNAME], among other possibilities
108 xProp
.setPropertyValue("DataSourceName","Bibliography");
110 // the CommandType must be TABLE, QUERY or COMMAND, here we use COMMAND
111 xProp
.setPropertyValue("CommandType",new Integer(com
.sun
.star
.sdb
.CommandType
.COMMAND
));
113 // the Command could be a table or query name or a SQL command, depending on the CommandType
114 xProp
.setPropertyValue("Command","SELECT IDENTIFIER, AUTHOR FROM biblio ORDER BY IDENTIFIER");
116 // if your database requires logon, you can use the properties User and Password
117 // xProp.setPropertyValue("User", "JohnDoe");
118 // xProp.setPropertyValue("Password", "mysecret");
122 // prepare the XRow and XColumnLocate interface for column access
123 // XRow gets column values
124 com
.sun
.star
.sdbc
.XRow xRow
= (com
.sun
.star
.sdbc
.XRow
)UnoRuntime
.queryInterface(
125 com
.sun
.star
.sdbc
.XRow
.class, xRowSet
);
126 // XColumnLocate finds columns by name
127 com
.sun
.star
.sdbc
.XColumnLocate xLoc
= (com
.sun
.star
.sdbc
.XColumnLocate
)
128 UnoRuntime
.queryInterface(
129 com
.sun
.star
.sdbc
.XColumnLocate
.class, xRowSet
);
131 // print output header
132 System
.out
.println("Identifier\tAuthor");
133 System
.out
.println("----------\t------");
135 // output result rows
136 while ( xRowSet
!= null && xRowSet
.next() ) {
137 String ident
= xRow
.getString(xLoc
.findColumn("IDENTIFIER"));
138 String author
= xRow
.getString(xLoc
.findColumn("AUTHOR"));
139 System
.out
.println(ident
+ "\t\t" + author
);
142 // XResultSetUpdate for insertRow handling
143 com
.sun
.star
.sdbc
.XResultSetUpdate xResultSetUpdate
= (com
.sun
.star
.sdbc
.XResultSetUpdate
)
144 UnoRuntime
.queryInterface(
145 com
.sun
.star
.sdbc
.XResultSetUpdate
.class, xRowSet
);
147 // XRowUpdate for row updates
148 com
.sun
.star
.sdbc
.XRowUpdate xRowUpdate
= (com
.sun
.star
.sdbc
.XRowUpdate
)
149 UnoRuntime
.queryInterface(
150 com
.sun
.star
.sdbc
.XRowUpdate
.class, xRowSet
);
152 // move to insertRow buffer
153 xResultSetUpdate
.moveToInsertRow();
155 // edit insertRow buffer
156 xRowUpdate
.updateString(xLoc
.findColumn("IDENTIFIER"), "GOF95");
157 xRowUpdate
.updateString(xLoc
.findColumn("AUTHOR"), "Gamma, Helm, Johnson, Vlissides");
159 // write buffer to database
160 xResultSetUpdate
.insertRow();
162 // throw away the row set
163 com
.sun
.star
.lang
.XComponent xComp
= (com
.sun
.star
.lang
.XComponent
)UnoRuntime
.queryInterface(
164 com
.sun
.star
.lang
.XComponent
.class, xRowSet
);