xref: /trunk/main/odk/examples/DevelopersGuide/Database/Sales.java (revision 351838c4d8726bbb9f5f13e599b27a7f22b4a9bd)
134dd1e25SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
334dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
434dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
534dd1e25SAndrew Rist  * distributed with this work for additional information
634dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
734dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
834dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
934dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1134dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1334dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1434dd1e25SAndrew Rist  * software distributed under the License is distributed on an
1534dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1634dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
1734dd1e25SAndrew Rist  * specific language governing permissions and limitations
1834dd1e25SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2034dd1e25SAndrew Rist  *************************************************************/
2134dd1e25SAndrew Rist 
2234dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import java.io.*;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir //  import com.sun.star.comp.helper.RegistryServiceFactory;
27cdf0e10cSrcweir //  import com.sun.star.comp.servicemanager.ServiceManager;
28cdf0e10cSrcweir //  import com.sun.star.lang.XMultiServiceFactory;
29cdf0e10cSrcweir //  import com.sun.star.lang.XServiceInfo;
30cdf0e10cSrcweir import com.sun.star.lang.XComponent;
31cdf0e10cSrcweir //  import com.sun.star.bridge.XUnoUrlResolver;
32cdf0e10cSrcweir import com.sun.star.uno.*;
33cdf0e10cSrcweir import com.sun.star.util.Date;
34cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
35cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
36cdf0e10cSrcweir import com.sun.star.sdbc.*;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir public class Sales
39cdf0e10cSrcweir {
40cdf0e10cSrcweir     private XConnection con;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir     public Sales(XConnection connection )
43cdf0e10cSrcweir     {
44cdf0e10cSrcweir         con = connection;
45cdf0e10cSrcweir     }
46cdf0e10cSrcweir     // create the table sales.
47cdf0e10cSrcweir     public void createSalesTable() throws com.sun.star.uno.Exception
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         String createTableSales = "CREATE TABLE SALES " +
50cdf0e10cSrcweir                 "(SALENR INTEGER NOT NULL, " +
51cdf0e10cSrcweir                 " COS_NR INTEGER, " +
52cdf0e10cSrcweir                 " SNR INTEGER, " +
53cdf0e10cSrcweir                 " NAME VARCHAR(50)," +
54cdf0e10cSrcweir                 " SALEDATE DATE," +
55cdf0e10cSrcweir                 " PRICE FLOAT(10), " +
56cdf0e10cSrcweir                 " PRIMARY KEY(SALENR)" +
57cdf0e10cSrcweir                 " )";
58cdf0e10cSrcweir         XStatement stmt = con.createStatement();
59cdf0e10cSrcweir         stmt.executeUpdate( createTableSales );
60cdf0e10cSrcweir     }
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     // drop the table sales.
63cdf0e10cSrcweir     public void dropSalesTable() throws com.sun.star.uno.Exception
64cdf0e10cSrcweir     {
65cdf0e10cSrcweir         String createTableSalesman = "DROP TABLE SALES ";
66cdf0e10cSrcweir         XStatement stmt = con.createStatement();
67cdf0e10cSrcweir         stmt.executeUpdate( createTableSalesman );
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     // insert data into the table sales.
71cdf0e10cSrcweir     public void insertDataIntoSales() throws com.sun.star.uno.Exception
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         XStatement stmt = con.createStatement();
74cdf0e10cSrcweir         stmt.executeUpdate("INSERT INTO SALES " +
75cdf0e10cSrcweir                 "VALUES (1, '100', '1','Linux','2001-02-12',15)");
76cdf0e10cSrcweir         stmt.executeUpdate("INSERT INTO SALES " +
77cdf0e10cSrcweir                 "VALUES (2, '101', '2','Beef','2001-10-18',15.78)");
78cdf0e10cSrcweir         stmt.executeUpdate("INSERT INTO SALES " +
79cdf0e10cSrcweir                 "VALUES (3, '104', '4','orange juice','2001-08-09',1.5)");
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     // update the table sales with a prepared statement.
83cdf0e10cSrcweir     public void updateSales() throws com.sun.star.uno.Exception
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         XStatement stmt = con.createStatement();
86cdf0e10cSrcweir         String updateString =   "UPDATE SALES " +
87cdf0e10cSrcweir                                 "SET PRICE = 30 " +
88cdf0e10cSrcweir                                 "WHERE SALENR = 1";
89cdf0e10cSrcweir         stmt.executeUpdate(updateString);
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     // retrieve the data of the table sales.
93cdf0e10cSrcweir     public void retrieveSalesData() throws com.sun.star.uno.Exception
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         XStatement stmt = con.createStatement();
96cdf0e10cSrcweir         String query =  "SELECT NAME, PRICE FROM SALES " +
97cdf0e10cSrcweir                         "WHERE SALENR = 1";
98cdf0e10cSrcweir         XResultSet rs = stmt.executeQuery(query);
99cdf0e10cSrcweir         XRow      row = (XRow)UnoRuntime.queryInterface(XRow.class, rs);
100cdf0e10cSrcweir         while (rs.next()) {
101cdf0e10cSrcweir                 String s = row.getString(1);
102cdf0e10cSrcweir                 float  n = row.getFloat(2);
103cdf0e10cSrcweir                 System.out.println("The current price for " + s + " is: $" + n + ".");
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     // create a scrollable resultset.
108cdf0e10cSrcweir     public void retrieveSalesData2() throws com.sun.star.uno.Exception
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         // example for a programmatic way to do updates. This doesn't work with adabas.
111cdf0e10cSrcweir         XStatement stmt = con.createStatement();
112cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,stmt);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetType", new java.lang.Integer(ResultSetType.SCROLL_INSENSITIVE));
115cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetConcurrency", new java.lang.Integer(ResultSetConcurrency.UPDATABLE));
116cdf0e10cSrcweir 
117cdf0e10cSrcweir         XResultSet srs = stmt.executeQuery("SELECT NAME, PRICE FROM SALES");
118cdf0e10cSrcweir         XRow       row = (XRow)UnoRuntime.queryInterface(XRow.class,srs);
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         srs.afterLast();
121cdf0e10cSrcweir         while (srs.previous()) {
122cdf0e10cSrcweir             String name = row.getString(1);
123cdf0e10cSrcweir             float price = row.getFloat(2);
124cdf0e10cSrcweir             System.out.println(name + "     " + price);
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         srs.last();
128cdf0e10cSrcweir         XRowUpdate updateRow = (XRowUpdate)UnoRuntime.queryInterface(XRowUpdate.class,srs);
129cdf0e10cSrcweir         updateRow.updateFloat(2, (float)0.69);
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         XResultSetUpdate updateRs = ( XResultSetUpdate )UnoRuntime.queryInterface(
132cdf0e10cSrcweir             XResultSetUpdate.class,srs);
133cdf0e10cSrcweir         updateRs.updateRow(); // this call updates the data in DBMS
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         srs.last();
136cdf0e10cSrcweir         updateRow.updateFloat(2, (float)0.99);
137cdf0e10cSrcweir         updateRs.cancelRowUpdates();
138cdf0e10cSrcweir         updateRow.updateFloat(2, (float)0.79);
139cdf0e10cSrcweir         updateRs.updateRow();
140cdf0e10cSrcweir     }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     // inserts a row programmatically.
143cdf0e10cSrcweir     public void insertRow() throws com.sun.star.uno.Exception
144cdf0e10cSrcweir     {
145cdf0e10cSrcweir         // example for a programmatic way to do updates. This doesn't work with adabas.
146cdf0e10cSrcweir         XStatement stmt = con.createStatement();
147cdf0e10cSrcweir //      stmt.executeUpdate("INSERT INTO SALES " +
148cdf0e10cSrcweir //                   "VALUES (4, 102, 5, 'FTOP Darjeeling tea', '2002-01-02',150)");
149cdf0e10cSrcweir //
150cdf0e10cSrcweir //      stmt = con.createStatement();
151cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,stmt);
152cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetType", new java.lang.Integer(ResultSetType.SCROLL_INSENSITIVE));
153cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetConcurrency", new java.lang.Integer(ResultSetConcurrency.UPDATABLE));
154cdf0e10cSrcweir         XResultSet rs = stmt.executeQuery("SELECT * FROM SALES");
155cdf0e10cSrcweir         XRow       row = (XRow)UnoRuntime.queryInterface(XRow.class,rs);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         // insert a new row
158cdf0e10cSrcweir         XRowUpdate updateRow = (XRowUpdate)UnoRuntime.queryInterface(XRowUpdate.class,rs);
159cdf0e10cSrcweir         XResultSetUpdate updateRs = ( XResultSetUpdate )UnoRuntime. queryInterface(XResultSetUpdate.class,rs);
160cdf0e10cSrcweir         updateRs.moveToInsertRow();
161cdf0e10cSrcweir         updateRow.updateInt(1, 4);
162cdf0e10cSrcweir         updateRow.updateInt(2, 102);
163cdf0e10cSrcweir         updateRow.updateInt(3, 5);
164cdf0e10cSrcweir         updateRow.updateString(4, "FTOP Darjeeling tea");
165cdf0e10cSrcweir         updateRow.updateDate(5, new Date((short)1,(short)2,(short)2002));
166cdf0e10cSrcweir         updateRow.updateFloat(6, 150);
167cdf0e10cSrcweir         updateRs.insertRow();
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     // deletes a row programmatically.
171cdf0e10cSrcweir     public void deleteRow() throws com.sun.star.uno.Exception
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir         // example for a programmatic way to do updates. This doesn't work with adabas.
174cdf0e10cSrcweir         XStatement stmt = con.createStatement();
175cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,stmt);
176cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetType", new java.lang.Integer(ResultSetType.SCROLL_INSENSITIVE));
177cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetConcurrency", new java.lang.Integer(ResultSetConcurrency.UPDATABLE));
178cdf0e10cSrcweir         XResultSet rs = stmt.executeQuery("SELECT * FROM SALES");
179cdf0e10cSrcweir         XRow       row = (XRow)UnoRuntime.queryInterface(XRow.class,rs);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir         XResultSetUpdate updateRs = ( XResultSetUpdate )UnoRuntime. queryInterface(XResultSetUpdate.class,rs);
182cdf0e10cSrcweir         // move to the inserted row
183cdf0e10cSrcweir         rs.absolute(4);
184cdf0e10cSrcweir         updateRs.deleteRow();
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     // refresh a row
188cdf0e10cSrcweir     public void refreshRow() throws com.sun.star.uno.Exception
189cdf0e10cSrcweir     {
190cdf0e10cSrcweir         // example for a programmatic way to do updates. This doesn't work with adabas.
191cdf0e10cSrcweir         // first we need the 4 row
192cdf0e10cSrcweir         insertRow();
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         XStatement stmt = con.createStatement();
195cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,stmt);
196cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetType", new java.lang.Integer(ResultSetType.SCROLL_INSENSITIVE));
197cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetConcurrency", new java.lang.Integer(ResultSetConcurrency.READ_ONLY));
198cdf0e10cSrcweir         XResultSet rs = stmt.executeQuery("SELECT NAME, PRICE FROM SALES");
199cdf0e10cSrcweir         XRow       row = (XRow)UnoRuntime.queryInterface(XRow.class, rs);
200cdf0e10cSrcweir         rs.absolute(4);
201cdf0e10cSrcweir         float price1 = row.getFloat(2);
202cdf0e10cSrcweir 
203*351838c4SJohn Bampton         // modify the 4 row
204cdf0e10cSrcweir         XRowUpdate updateRow = (XRowUpdate)UnoRuntime.queryInterface(XRowUpdate.class,rs);
205cdf0e10cSrcweir         XResultSetUpdate updateRs = ( XResultSetUpdate )UnoRuntime. queryInterface(XResultSetUpdate.class,rs);
206cdf0e10cSrcweir         updateRow.updateFloat(2, 150);
207cdf0e10cSrcweir         updateRs.updateRow();
208cdf0e10cSrcweir         // repositioning
209cdf0e10cSrcweir         rs.absolute(4);
210cdf0e10cSrcweir         rs.refreshRow();
211cdf0e10cSrcweir         float price2 = row.getFloat(2);
212cdf0e10cSrcweir         if (price2 != price1) {
213cdf0e10cSrcweir             System.out.println("Prices are different.");
214cdf0e10cSrcweir         }
215cdf0e10cSrcweir         else
216cdf0e10cSrcweir             System.out.println("Prices are equal.");
217cdf0e10cSrcweir         deleteRow();
218cdf0e10cSrcweir     }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     // displays the column names
221cdf0e10cSrcweir     public void displayColumnNames() throws com.sun.star.uno.Exception
222cdf0e10cSrcweir     {
223cdf0e10cSrcweir         XStatement stmt = con.createStatement();
224cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,stmt);
225cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetType", new java.lang.Integer(ResultSetType.SCROLL_INSENSITIVE));
226cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetConcurrency", new java.lang.Integer(ResultSetConcurrency.READ_ONLY));
227cdf0e10cSrcweir         XResultSet rs = stmt.executeQuery("SELECT NAME, PRICE FROM SALES");
228cdf0e10cSrcweir         XResultSetMetaDataSupplier xRsMetaSup = (XResultSetMetaDataSupplier)
229cdf0e10cSrcweir             UnoRuntime.queryInterface(XResultSetMetaDataSupplier.class,rs);
230cdf0e10cSrcweir         XResultSetMetaData xRsMetaData =  xRsMetaSup.getMetaData();
231cdf0e10cSrcweir         int nColumnCount =  xRsMetaData.getColumnCount();
232cdf0e10cSrcweir         for(int i=1 ; i <= nColumnCount ; ++i)
233cdf0e10cSrcweir         {
234cdf0e10cSrcweir            System.out.println("Name: " + xRsMetaData.getColumnName(i) + " Type: " +
235cdf0e10cSrcweir                               xRsMetaData.getColumnType(i));
236cdf0e10cSrcweir         }
237cdf0e10cSrcweir     }
238cdf0e10cSrcweir }
239