1 /*************************************************************************
3 * $RCSfile: Sales.java,v $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:17:40 $
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 *************************************************************************/
43 // import com.sun.star.comp.helper.RegistryServiceFactory;
44 // import com.sun.star.comp.servicemanager.ServiceManager;
45 // import com.sun.star.lang.XMultiServiceFactory;
46 // import com.sun.star.lang.XServiceInfo;
47 import com
.sun
.star
.lang
.XComponent
;
48 // import com.sun.star.bridge.XUnoUrlResolver;
49 import com
.sun
.star
.uno
.*;
50 import com
.sun
.star
.util
.Date
;
51 import com
.sun
.star
.beans
.XPropertySet
;
52 import com
.sun
.star
.container
.XNameAccess
;
53 import com
.sun
.star
.sdbc
.*;
57 private XConnection con
;
59 public Sales(XConnection connection
)
63 // create the table sales.
64 public void createSalesTable() throws com
.sun
.star
.uno
.Exception
66 String createTableSales
= "CREATE TABLE SALES " +
67 "(SALENR INTEGER NOT NULL, " +
70 " NAME VARCHAR(50)," +
72 " PRICE FLOAT(10), " +
73 " PRIMARY KEY(SALENR)" +
75 XStatement stmt
= con
.createStatement();
76 stmt
.executeUpdate( createTableSales
);
79 // drop the table sales.
80 public void dropSalesTable() throws com
.sun
.star
.uno
.Exception
82 String createTableSalesman
= "DROP TABLE SALES ";
83 XStatement stmt
= con
.createStatement();
84 stmt
.executeUpdate( createTableSalesman
);
87 // insert data into the table sales.
88 public void insertDataIntoSales() throws com
.sun
.star
.uno
.Exception
90 XStatement stmt
= con
.createStatement();
91 stmt
.executeUpdate("INSERT INTO SALES " +
92 "VALUES (1, '100', '1','Linux','2001-02-12',15)");
93 stmt
.executeUpdate("INSERT INTO SALES " +
94 "VALUES (2, '101', '2','Beef','2001-10-18',15.78)");
95 stmt
.executeUpdate("INSERT INTO SALES " +
96 "VALUES (3, '104', '4','orange juice','2001-08-09',1.5)");
99 // update the table sales with a prepared statement.
100 public void updateSales() throws com
.sun
.star
.uno
.Exception
102 XStatement stmt
= con
.createStatement();
103 String updateString
= "UPDATE SALES " +
106 stmt
.executeUpdate(updateString
);
109 // retrieve the data of the table sales.
110 public void retrieveSalesData() throws com
.sun
.star
.uno
.Exception
112 XStatement stmt
= con
.createStatement();
113 String query
= "SELECT NAME, PRICE FROM SALES " +
115 XResultSet rs
= stmt
.executeQuery(query
);
116 XRow row
= (XRow
)UnoRuntime
.queryInterface(XRow
.class, rs
);
118 String s
= row
.getString(1);
119 float n
= row
.getFloat(2);
120 System
.out
.println("The current price for " + s
+ " is: $" + n
+ ".");
124 // create a scrollable resultset.
125 public void retrieveSalesData2() throws com
.sun
.star
.uno
.Exception
127 // example for a programmatic way to do updates. This doesn't work with adabas.
128 XStatement stmt
= con
.createStatement();
129 XPropertySet xProp
= (XPropertySet
)UnoRuntime
.queryInterface(XPropertySet
.class,stmt
);
131 xProp
.setPropertyValue("ResultSetType", new java
.lang
.Integer(ResultSetType
.SCROLL_INSENSITIVE
));
132 xProp
.setPropertyValue("ResultSetConcurrency", new java
.lang
.Integer(ResultSetConcurrency
.UPDATABLE
));
134 XResultSet srs
= stmt
.executeQuery("SELECT NAME, PRICE FROM SALES");
135 XRow row
= (XRow
)UnoRuntime
.queryInterface(XRow
.class,srs
);
138 while (srs
.previous()) {
139 String name
= row
.getString(1);
140 float price
= row
.getFloat(2);
141 System
.out
.println(name
+ " " + price
);
145 XRowUpdate updateRow
= (XRowUpdate
)UnoRuntime
.queryInterface(XRowUpdate
.class,srs
);
146 updateRow
.updateFloat(2, (float)0.69);
148 XResultSetUpdate updateRs
= ( XResultSetUpdate
)UnoRuntime
.queryInterface(
149 XResultSetUpdate
.class,srs
);
150 updateRs
.updateRow(); // this call updates the data in DBMS
153 updateRow
.updateFloat(2, (float)0.99);
154 updateRs
.cancelRowUpdates();
155 updateRow
.updateFloat(2, (float)0.79);
156 updateRs
.updateRow();
159 // inserts a row programmatically.
160 public void insertRow() throws com
.sun
.star
.uno
.Exception
162 // example for a programmatic way to do updates. This doesn't work with adabas.
163 XStatement stmt
= con
.createStatement();
164 // stmt.executeUpdate("INSERT INTO SALES " +
165 // "VALUES (4, 102, 5, 'FTOP Darjeeling tea', '2002-01-02',150)");
167 // stmt = con.createStatement();
168 XPropertySet xProp
= (XPropertySet
)UnoRuntime
.queryInterface(XPropertySet
.class,stmt
);
169 xProp
.setPropertyValue("ResultSetType", new java
.lang
.Integer(ResultSetType
.SCROLL_INSENSITIVE
));
170 xProp
.setPropertyValue("ResultSetConcurrency", new java
.lang
.Integer(ResultSetConcurrency
.UPDATABLE
));
171 XResultSet rs
= stmt
.executeQuery("SELECT * FROM SALES");
172 XRow row
= (XRow
)UnoRuntime
.queryInterface(XRow
.class,rs
);
175 XRowUpdate updateRow
= (XRowUpdate
)UnoRuntime
.queryInterface(XRowUpdate
.class,rs
);
176 XResultSetUpdate updateRs
= ( XResultSetUpdate
)UnoRuntime
. queryInterface(XResultSetUpdate
.class,rs
);
177 updateRs
.moveToInsertRow();
178 updateRow
.updateInt(1, 4);
179 updateRow
.updateInt(2, 102);
180 updateRow
.updateInt(3, 5);
181 updateRow
.updateString(4, "FTOP Darjeeling tea");
182 updateRow
.updateDate(5, new Date((short)1,(short)2,(short)2002));
183 updateRow
.updateFloat(6, 150);
184 updateRs
.insertRow();
187 // deletes a row programmatically.
188 public void deleteRow() throws com
.sun
.star
.uno
.Exception
190 // example for a programmatic way to do updates. This doesn't work with adabas.
191 XStatement stmt
= con
.createStatement();
192 XPropertySet xProp
= (XPropertySet
)UnoRuntime
.queryInterface(XPropertySet
.class,stmt
);
193 xProp
.setPropertyValue("ResultSetType", new java
.lang
.Integer(ResultSetType
.SCROLL_INSENSITIVE
));
194 xProp
.setPropertyValue("ResultSetConcurrency", new java
.lang
.Integer(ResultSetConcurrency
.UPDATABLE
));
195 XResultSet rs
= stmt
.executeQuery("SELECT * FROM SALES");
196 XRow row
= (XRow
)UnoRuntime
.queryInterface(XRow
.class,rs
);
198 XResultSetUpdate updateRs
= ( XResultSetUpdate
)UnoRuntime
. queryInterface(XResultSetUpdate
.class,rs
);
199 // move to the inserted row
201 updateRs
.deleteRow();
205 public void refreshRow() throws com
.sun
.star
.uno
.Exception
207 // example for a programmatic way to do updates. This doesn't work with adabas.
208 // first we need the 4 row
211 XStatement stmt
= con
.createStatement();
212 XPropertySet xProp
= (XPropertySet
)UnoRuntime
.queryInterface(XPropertySet
.class,stmt
);
213 xProp
.setPropertyValue("ResultSetType", new java
.lang
.Integer(ResultSetType
.SCROLL_INSENSITIVE
));
214 xProp
.setPropertyValue("ResultSetConcurrency", new java
.lang
.Integer(ResultSetConcurrency
.READ_ONLY
));
215 XResultSet rs
= stmt
.executeQuery("SELECT NAME, PRICE FROM SALES");
216 XRow row
= (XRow
)UnoRuntime
.queryInterface(XRow
.class, rs
);
218 float price1
= row
.getFloat(2);
221 XRowUpdate updateRow
= (XRowUpdate
)UnoRuntime
.queryInterface(XRowUpdate
.class,rs
);
222 XResultSetUpdate updateRs
= ( XResultSetUpdate
)UnoRuntime
. queryInterface(XResultSetUpdate
.class,rs
);
223 updateRow
.updateFloat(2, 150);
224 updateRs
.updateRow();
228 float price2
= row
.getFloat(2);
229 if (price2
!= price1
) {
230 System
.out
.println("Prices are different.");
233 System
.out
.println("Prices are equal.");
237 // displays the column names
238 public void displayColumnNames() throws com
.sun
.star
.uno
.Exception
240 XStatement stmt
= con
.createStatement();
241 XPropertySet xProp
= (XPropertySet
)UnoRuntime
.queryInterface(XPropertySet
.class,stmt
);
242 xProp
.setPropertyValue("ResultSetType", new java
.lang
.Integer(ResultSetType
.SCROLL_INSENSITIVE
));
243 xProp
.setPropertyValue("ResultSetConcurrency", new java
.lang
.Integer(ResultSetConcurrency
.READ_ONLY
));
244 XResultSet rs
= stmt
.executeQuery("SELECT NAME, PRICE FROM SALES");
245 XResultSetMetaDataSupplier xRsMetaSup
= (XResultSetMetaDataSupplier
)
246 UnoRuntime
.queryInterface(XResultSetMetaDataSupplier
.class,rs
);
247 XResultSetMetaData xRsMetaData
= xRsMetaSup
.getMetaData();
248 int nColumnCount
= xRsMetaData
.getColumnCount();
249 for(int i
=1 ; i
<= nColumnCount
; ++i
)
251 System
.out
.println("Name: " + xRsMetaData
.getColumnName(i
) + " Type: " +
252 xRsMetaData
.getColumnType(i
));