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