1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
25cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
26cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
27cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
28cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir /*
33cdf0e10cSrcweir  * OpenQuery.java
34cdf0e10cSrcweir  *
35cdf0e10cSrcweir  * Created on 6. Juli 2002, 10:25
36cdf0e10cSrcweir  */
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /**
39cdf0e10cSrcweir  *
40cdf0e10cSrcweir  * @author  dschulten
41cdf0e10cSrcweir  */
42cdf0e10cSrcweir public class OpenQuery {
43cdf0e10cSrcweir 
44cdf0e10cSrcweir     private XComponentContext xContext = null;
45cdf0e10cSrcweir     private XMultiComponentFactory xMCF = null;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     /** Creates a new instance of OpenQuery */
OpenQuery()48cdf0e10cSrcweir     public OpenQuery() {
49cdf0e10cSrcweir     }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     /**
52cdf0e10cSrcweir      * @param args the command line arguments
53cdf0e10cSrcweir      */
main(String[] args)54cdf0e10cSrcweir     public static void main(String[] args) {
55cdf0e10cSrcweir         OpenQuery openQuery1 = new OpenQuery();
56cdf0e10cSrcweir         try {
57cdf0e10cSrcweir             openQuery1.openQuery();
58cdf0e10cSrcweir         }
59cdf0e10cSrcweir         catch (java.lang.Exception e){
60cdf0e10cSrcweir             e.printStackTrace();
61cdf0e10cSrcweir         }
62cdf0e10cSrcweir         finally {
63cdf0e10cSrcweir             System.exit(0);
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir 
openQuery()67cdf0e10cSrcweir     protected void openQuery() throws com.sun.star.uno.Exception, java.lang.Exception {
68cdf0e10cSrcweir         try {
69cdf0e10cSrcweir             // get the remote office component context
70cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
71cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
72cdf0e10cSrcweir             xMCF = xContext.getServiceManager();
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir         catch( Exception e) {
75cdf0e10cSrcweir             System.err.println("ERROR: can't get a component context from a running office ...");
76cdf0e10cSrcweir             e.printStackTrace();
77cdf0e10cSrcweir             System.exit(1);
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir         // first we create our RowSet object and get its XRowSet interface
81cdf0e10cSrcweir         Object rowSet = xMCF.createInstanceWithContext(
82cdf0e10cSrcweir             "com.sun.star.sdb.RowSet", xContext);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         com.sun.star.sdbc.XRowSet xRowSet = (com.sun.star.sdbc.XRowSet)
85cdf0e10cSrcweir             UnoRuntime.queryInterface(com.sun.star.sdbc.XRowSet.class, rowSet);
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         // set the properties needed to connect to a database
88cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xRowSet);
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         // the DataSourceName can be a data source registered with [PRODUCTNAME], among other possibilities
91cdf0e10cSrcweir         xProp.setPropertyValue("DataSourceName","Bibliography");
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         // the CommandType must be TABLE, QUERY or COMMAND, here we use COMMAND
94cdf0e10cSrcweir         xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.COMMAND));
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         // the Command could be a table or query name or a SQL command, depending on the CommandType
97cdf0e10cSrcweir         xProp.setPropertyValue("Command","SELECT IDENTIFIER, AUTHOR FROM biblio ORDER BY IDENTIFIER");
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         // if your database requires logon, you can use the properties User and Password
100cdf0e10cSrcweir         // xProp.setPropertyValue("User", "JohnDoe");
101cdf0e10cSrcweir         // xProp.setPropertyValue("Password", "mysecret");
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         xRowSet.execute();
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         // prepare the XRow and XColumnLocate interface for column access
106cdf0e10cSrcweir         // XRow gets column values
107cdf0e10cSrcweir         com.sun.star.sdbc.XRow xRow = (com.sun.star.sdbc.XRow)UnoRuntime.queryInterface(
108cdf0e10cSrcweir             com.sun.star.sdbc.XRow.class, xRowSet);
109cdf0e10cSrcweir         // XColumnLocate finds columns by name
110cdf0e10cSrcweir         com.sun.star.sdbc.XColumnLocate xLoc = (com.sun.star.sdbc.XColumnLocate)
111cdf0e10cSrcweir             UnoRuntime.queryInterface(
112cdf0e10cSrcweir                 com.sun.star.sdbc.XColumnLocate.class, xRowSet);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         // print output header
115cdf0e10cSrcweir         System.out.println("Identifier\tAuthor");
116cdf0e10cSrcweir         System.out.println("----------\t------");
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         // output result rows
119cdf0e10cSrcweir         while ( xRowSet != null && xRowSet.next() ) {
120cdf0e10cSrcweir             String ident = xRow.getString(xLoc.findColumn("IDENTIFIER"));
121cdf0e10cSrcweir             String author = xRow.getString(xLoc.findColumn("AUTHOR"));
122cdf0e10cSrcweir             System.out.println(ident + "\t\t" + author);
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         // XResultSetUpdate for insertRow handling
126cdf0e10cSrcweir         com.sun.star.sdbc.XResultSetUpdate xResultSetUpdate = (com.sun.star.sdbc.XResultSetUpdate)
127cdf0e10cSrcweir             UnoRuntime.queryInterface(
128cdf0e10cSrcweir                 com.sun.star.sdbc.XResultSetUpdate.class, xRowSet);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         // XRowUpdate for row updates
131cdf0e10cSrcweir         com.sun.star.sdbc.XRowUpdate xRowUpdate = (com.sun.star.sdbc.XRowUpdate)
132cdf0e10cSrcweir             UnoRuntime.queryInterface(
133cdf0e10cSrcweir                 com.sun.star.sdbc.XRowUpdate.class, xRowSet);
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         // move to insertRow buffer
136cdf0e10cSrcweir         xResultSetUpdate.moveToInsertRow();
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         // edit insertRow buffer
139cdf0e10cSrcweir         xRowUpdate.updateString(xLoc.findColumn("IDENTIFIER"), "GOF95");
140cdf0e10cSrcweir         xRowUpdate.updateString(xLoc.findColumn("AUTHOR"), "Gamma, Helm, Johnson, Vlissides");
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         // write buffer to database
143cdf0e10cSrcweir         xResultSetUpdate.insertRow();
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         // throw away the row set
146cdf0e10cSrcweir         com.sun.star.lang.XComponent xComp = (com.sun.star.lang.XComponent)UnoRuntime.queryInterface(
147cdf0e10cSrcweir             com.sun.star.lang.XComponent.class, xRowSet);
148cdf0e10cSrcweir         xComp.dispose();
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir }
152