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