xref: /trunk/main/odk/examples/DevelopersGuide/Database/Sales.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import java.io.*;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir //  import com.sun.star.comp.helper.RegistryServiceFactory;
38*cdf0e10cSrcweir //  import com.sun.star.comp.servicemanager.ServiceManager;
39*cdf0e10cSrcweir //  import com.sun.star.lang.XMultiServiceFactory;
40*cdf0e10cSrcweir //  import com.sun.star.lang.XServiceInfo;
41*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
42*cdf0e10cSrcweir //  import com.sun.star.bridge.XUnoUrlResolver;
43*cdf0e10cSrcweir import com.sun.star.uno.*;
44*cdf0e10cSrcweir import com.sun.star.util.Date;
45*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
46*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
47*cdf0e10cSrcweir import com.sun.star.sdbc.*;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir public class Sales
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir     private XConnection con;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir     public Sales(XConnection connection )
54*cdf0e10cSrcweir     {
55*cdf0e10cSrcweir         con = connection;
56*cdf0e10cSrcweir     }
57*cdf0e10cSrcweir     // create the table sales.
58*cdf0e10cSrcweir     public void createSalesTable() throws com.sun.star.uno.Exception
59*cdf0e10cSrcweir     {
60*cdf0e10cSrcweir         String createTableSales = "CREATE TABLE SALES " +
61*cdf0e10cSrcweir                 "(SALENR INTEGER NOT NULL, " +
62*cdf0e10cSrcweir                 " COS_NR INTEGER, " +
63*cdf0e10cSrcweir                 " SNR INTEGER, " +
64*cdf0e10cSrcweir                 " NAME VARCHAR(50)," +
65*cdf0e10cSrcweir                 " SALEDATE DATE," +
66*cdf0e10cSrcweir                 " PRICE FLOAT(10), " +
67*cdf0e10cSrcweir                 " PRIMARY KEY(SALENR)" +
68*cdf0e10cSrcweir                 " )";
69*cdf0e10cSrcweir         XStatement stmt = con.createStatement();
70*cdf0e10cSrcweir         stmt.executeUpdate( createTableSales );
71*cdf0e10cSrcweir     }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     // drop the table sales.
74*cdf0e10cSrcweir     public void dropSalesTable() throws com.sun.star.uno.Exception
75*cdf0e10cSrcweir     {
76*cdf0e10cSrcweir         String createTableSalesman = "DROP TABLE SALES ";
77*cdf0e10cSrcweir         XStatement stmt = con.createStatement();
78*cdf0e10cSrcweir         stmt.executeUpdate( createTableSalesman );
79*cdf0e10cSrcweir     }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     // insert data into the table sales.
82*cdf0e10cSrcweir     public void insertDataIntoSales() throws com.sun.star.uno.Exception
83*cdf0e10cSrcweir     {
84*cdf0e10cSrcweir         XStatement stmt = con.createStatement();
85*cdf0e10cSrcweir         stmt.executeUpdate("INSERT INTO SALES " +
86*cdf0e10cSrcweir                 "VALUES (1, '100', '1','Linux','2001-02-12',15)");
87*cdf0e10cSrcweir         stmt.executeUpdate("INSERT INTO SALES " +
88*cdf0e10cSrcweir                 "VALUES (2, '101', '2','Beef','2001-10-18',15.78)");
89*cdf0e10cSrcweir         stmt.executeUpdate("INSERT INTO SALES " +
90*cdf0e10cSrcweir                 "VALUES (3, '104', '4','orange juice','2001-08-09',1.5)");
91*cdf0e10cSrcweir     }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     // update the table sales with a prepared statement.
94*cdf0e10cSrcweir     public void updateSales() throws com.sun.star.uno.Exception
95*cdf0e10cSrcweir     {
96*cdf0e10cSrcweir         XStatement stmt = con.createStatement();
97*cdf0e10cSrcweir         String updateString =   "UPDATE SALES " +
98*cdf0e10cSrcweir                                 "SET PRICE = 30 " +
99*cdf0e10cSrcweir                                 "WHERE SALENR = 1";
100*cdf0e10cSrcweir         stmt.executeUpdate(updateString);
101*cdf0e10cSrcweir     }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     // retrieve the data of the table sales.
104*cdf0e10cSrcweir     public void retrieveSalesData() throws com.sun.star.uno.Exception
105*cdf0e10cSrcweir     {
106*cdf0e10cSrcweir         XStatement stmt = con.createStatement();
107*cdf0e10cSrcweir         String query =  "SELECT NAME, PRICE FROM SALES " +
108*cdf0e10cSrcweir                         "WHERE SALENR = 1";
109*cdf0e10cSrcweir         XResultSet rs = stmt.executeQuery(query);
110*cdf0e10cSrcweir         XRow      row = (XRow)UnoRuntime.queryInterface(XRow.class, rs);
111*cdf0e10cSrcweir         while (rs.next()) {
112*cdf0e10cSrcweir                 String s = row.getString(1);
113*cdf0e10cSrcweir                 float  n = row.getFloat(2);
114*cdf0e10cSrcweir                 System.out.println("The current price for " + s + " is: $" + n + ".");
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir     }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     // create a scrollable resultset.
119*cdf0e10cSrcweir     public void retrieveSalesData2() throws com.sun.star.uno.Exception
120*cdf0e10cSrcweir     {
121*cdf0e10cSrcweir         // example for a programmatic way to do updates. This doesn't work with adabas.
122*cdf0e10cSrcweir         XStatement stmt = con.createStatement();
123*cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,stmt);
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetType", new java.lang.Integer(ResultSetType.SCROLL_INSENSITIVE));
126*cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetConcurrency", new java.lang.Integer(ResultSetConcurrency.UPDATABLE));
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         XResultSet srs = stmt.executeQuery("SELECT NAME, PRICE FROM SALES");
129*cdf0e10cSrcweir         XRow       row = (XRow)UnoRuntime.queryInterface(XRow.class,srs);
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         srs.afterLast();
132*cdf0e10cSrcweir         while (srs.previous()) {
133*cdf0e10cSrcweir             String name = row.getString(1);
134*cdf0e10cSrcweir             float price = row.getFloat(2);
135*cdf0e10cSrcweir             System.out.println(name + "     " + price);
136*cdf0e10cSrcweir         }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         srs.last();
139*cdf0e10cSrcweir         XRowUpdate updateRow = (XRowUpdate)UnoRuntime.queryInterface(XRowUpdate.class,srs);
140*cdf0e10cSrcweir         updateRow.updateFloat(2, (float)0.69);
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir         XResultSetUpdate updateRs = ( XResultSetUpdate )UnoRuntime.queryInterface(
143*cdf0e10cSrcweir             XResultSetUpdate.class,srs);
144*cdf0e10cSrcweir         updateRs.updateRow(); // this call updates the data in DBMS
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         srs.last();
147*cdf0e10cSrcweir         updateRow.updateFloat(2, (float)0.99);
148*cdf0e10cSrcweir         updateRs.cancelRowUpdates();
149*cdf0e10cSrcweir         updateRow.updateFloat(2, (float)0.79);
150*cdf0e10cSrcweir         updateRs.updateRow();
151*cdf0e10cSrcweir     }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     // inserts a row programmatically.
154*cdf0e10cSrcweir     public void insertRow() throws com.sun.star.uno.Exception
155*cdf0e10cSrcweir     {
156*cdf0e10cSrcweir         // example for a programmatic way to do updates. This doesn't work with adabas.
157*cdf0e10cSrcweir         XStatement stmt = con.createStatement();
158*cdf0e10cSrcweir //      stmt.executeUpdate("INSERT INTO SALES " +
159*cdf0e10cSrcweir //                   "VALUES (4, 102, 5, 'FTOP Darjeeling tea', '2002-01-02',150)");
160*cdf0e10cSrcweir //
161*cdf0e10cSrcweir //      stmt = con.createStatement();
162*cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,stmt);
163*cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetType", new java.lang.Integer(ResultSetType.SCROLL_INSENSITIVE));
164*cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetConcurrency", new java.lang.Integer(ResultSetConcurrency.UPDATABLE));
165*cdf0e10cSrcweir         XResultSet rs = stmt.executeQuery("SELECT * FROM SALES");
166*cdf0e10cSrcweir         XRow       row = (XRow)UnoRuntime.queryInterface(XRow.class,rs);
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         // insert a new row
169*cdf0e10cSrcweir         XRowUpdate updateRow = (XRowUpdate)UnoRuntime.queryInterface(XRowUpdate.class,rs);
170*cdf0e10cSrcweir         XResultSetUpdate updateRs = ( XResultSetUpdate )UnoRuntime. queryInterface(XResultSetUpdate.class,rs);
171*cdf0e10cSrcweir         updateRs.moveToInsertRow();
172*cdf0e10cSrcweir         updateRow.updateInt(1, 4);
173*cdf0e10cSrcweir         updateRow.updateInt(2, 102);
174*cdf0e10cSrcweir         updateRow.updateInt(3, 5);
175*cdf0e10cSrcweir         updateRow.updateString(4, "FTOP Darjeeling tea");
176*cdf0e10cSrcweir         updateRow.updateDate(5, new Date((short)1,(short)2,(short)2002));
177*cdf0e10cSrcweir         updateRow.updateFloat(6, 150);
178*cdf0e10cSrcweir         updateRs.insertRow();
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     // deletes a row programmatically.
182*cdf0e10cSrcweir     public void deleteRow() throws com.sun.star.uno.Exception
183*cdf0e10cSrcweir     {
184*cdf0e10cSrcweir         // example for a programmatic way to do updates. This doesn't work with adabas.
185*cdf0e10cSrcweir         XStatement stmt = con.createStatement();
186*cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,stmt);
187*cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetType", new java.lang.Integer(ResultSetType.SCROLL_INSENSITIVE));
188*cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetConcurrency", new java.lang.Integer(ResultSetConcurrency.UPDATABLE));
189*cdf0e10cSrcweir         XResultSet rs = stmt.executeQuery("SELECT * FROM SALES");
190*cdf0e10cSrcweir         XRow       row = (XRow)UnoRuntime.queryInterface(XRow.class,rs);
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir         XResultSetUpdate updateRs = ( XResultSetUpdate )UnoRuntime. queryInterface(XResultSetUpdate.class,rs);
193*cdf0e10cSrcweir         // move to the inserted row
194*cdf0e10cSrcweir         rs.absolute(4);
195*cdf0e10cSrcweir         updateRs.deleteRow();
196*cdf0e10cSrcweir     }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     // refresh a row
199*cdf0e10cSrcweir     public void refreshRow() throws com.sun.star.uno.Exception
200*cdf0e10cSrcweir     {
201*cdf0e10cSrcweir         // example for a programmatic way to do updates. This doesn't work with adabas.
202*cdf0e10cSrcweir         // first we need the 4 row
203*cdf0e10cSrcweir         insertRow();
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir         XStatement stmt = con.createStatement();
206*cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,stmt);
207*cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetType", new java.lang.Integer(ResultSetType.SCROLL_INSENSITIVE));
208*cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetConcurrency", new java.lang.Integer(ResultSetConcurrency.READ_ONLY));
209*cdf0e10cSrcweir         XResultSet rs = stmt.executeQuery("SELECT NAME, PRICE FROM SALES");
210*cdf0e10cSrcweir         XRow       row = (XRow)UnoRuntime.queryInterface(XRow.class, rs);
211*cdf0e10cSrcweir         rs.absolute(4);
212*cdf0e10cSrcweir         float price1 = row.getFloat(2);
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir         // modifiy the 4 row
215*cdf0e10cSrcweir         XRowUpdate updateRow = (XRowUpdate)UnoRuntime.queryInterface(XRowUpdate.class,rs);
216*cdf0e10cSrcweir         XResultSetUpdate updateRs = ( XResultSetUpdate )UnoRuntime. queryInterface(XResultSetUpdate.class,rs);
217*cdf0e10cSrcweir         updateRow.updateFloat(2, 150);
218*cdf0e10cSrcweir         updateRs.updateRow();
219*cdf0e10cSrcweir         // repositioning
220*cdf0e10cSrcweir         rs.absolute(4);
221*cdf0e10cSrcweir         rs.refreshRow();
222*cdf0e10cSrcweir         float price2 = row.getFloat(2);
223*cdf0e10cSrcweir         if (price2 != price1) {
224*cdf0e10cSrcweir             System.out.println("Prices are different.");
225*cdf0e10cSrcweir         }
226*cdf0e10cSrcweir         else
227*cdf0e10cSrcweir             System.out.println("Prices are equal.");
228*cdf0e10cSrcweir         deleteRow();
229*cdf0e10cSrcweir     }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir     // displays the column names
232*cdf0e10cSrcweir     public void displayColumnNames() throws com.sun.star.uno.Exception
233*cdf0e10cSrcweir     {
234*cdf0e10cSrcweir         XStatement stmt = con.createStatement();
235*cdf0e10cSrcweir         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,stmt);
236*cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetType", new java.lang.Integer(ResultSetType.SCROLL_INSENSITIVE));
237*cdf0e10cSrcweir         xProp.setPropertyValue("ResultSetConcurrency", new java.lang.Integer(ResultSetConcurrency.READ_ONLY));
238*cdf0e10cSrcweir         XResultSet rs = stmt.executeQuery("SELECT NAME, PRICE FROM SALES");
239*cdf0e10cSrcweir         XResultSetMetaDataSupplier xRsMetaSup = (XResultSetMetaDataSupplier)
240*cdf0e10cSrcweir             UnoRuntime.queryInterface(XResultSetMetaDataSupplier.class,rs);
241*cdf0e10cSrcweir         XResultSetMetaData xRsMetaData =  xRsMetaSup.getMetaData();
242*cdf0e10cSrcweir         int nColumnCount =  xRsMetaData.getColumnCount();
243*cdf0e10cSrcweir         for(int i=1 ; i <= nColumnCount ; ++i)
244*cdf0e10cSrcweir         {
245*cdf0e10cSrcweir            System.out.println("Name: " + xRsMetaData.getColumnName(i) + " Type: " +
246*cdf0e10cSrcweir                               xRsMetaData.getColumnType(i));
247*cdf0e10cSrcweir         }
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir }
250*cdf0e10cSrcweir 
251