1*34dd1e25SAndrew Rist /************************************************************** 2*34dd1e25SAndrew Rist * 3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*34dd1e25SAndrew Rist * distributed with this work for additional information 6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10*34dd1e25SAndrew Rist * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*34dd1e25SAndrew Rist * 13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an 15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 17*34dd1e25SAndrew Rist * specific language governing permissions and limitations 18*34dd1e25SAndrew Rist * under the License. 19*34dd1e25SAndrew Rist * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew 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 SalesMan 39cdf0e10cSrcweir { 40cdf0e10cSrcweir private XConnection con; 41cdf0e10cSrcweir SalesMan(XConnection connection )42cdf0e10cSrcweir public SalesMan(XConnection connection ) 43cdf0e10cSrcweir { 44cdf0e10cSrcweir con = connection; 45cdf0e10cSrcweir } 46cdf0e10cSrcweir // create the table salesman. createSalesManTable()47cdf0e10cSrcweir public void createSalesManTable() throws com.sun.star.uno.Exception 48cdf0e10cSrcweir { 49cdf0e10cSrcweir String createTableSalesman = "CREATE TABLE SALESMAN " + 50cdf0e10cSrcweir "(SNR INTEGER NOT NULL, "+ 51cdf0e10cSrcweir " FIRSTNAME VARCHAR(50)," + 52cdf0e10cSrcweir " LASTNAME VARCHAR(100)," + 53cdf0e10cSrcweir " STREET VARCHAR(50)," + 54cdf0e10cSrcweir " STATE VARCHAR(50)," + 55cdf0e10cSrcweir " ZIP INTEGER," + 56cdf0e10cSrcweir " BIRTHDATE DATE," + 57cdf0e10cSrcweir " PRIMARY KEY(SNR)" + 58cdf0e10cSrcweir " )"; 59cdf0e10cSrcweir XStatement stmt = con.createStatement(); 60cdf0e10cSrcweir stmt.executeUpdate( createTableSalesman ); 61cdf0e10cSrcweir } 62cdf0e10cSrcweir 63cdf0e10cSrcweir // drop the table salesman dropSalesManTable()64cdf0e10cSrcweir public void dropSalesManTable() throws com.sun.star.uno.Exception 65cdf0e10cSrcweir { 66cdf0e10cSrcweir String createTableSalesman = "DROP TABLE SALESMAN "; 67cdf0e10cSrcweir XStatement stmt = con.createStatement(); 68cdf0e10cSrcweir stmt.executeUpdate( createTableSalesman ); 69cdf0e10cSrcweir } 70cdf0e10cSrcweir 71cdf0e10cSrcweir // insert data into the table salesman insertDataIntoSalesMan()72cdf0e10cSrcweir public void insertDataIntoSalesMan() throws com.sun.star.uno.Exception 73cdf0e10cSrcweir { 74cdf0e10cSrcweir XStatement stmt = con.createStatement(); 75cdf0e10cSrcweir stmt.executeUpdate("INSERT INTO SALESMAN " + 76cdf0e10cSrcweir "VALUES (1, 'Joseph', 'Smith','Bond Street','CA',95460," 77cdf0e10cSrcweir + "'1946-07-02')"); 78cdf0e10cSrcweir stmt.executeUpdate("INSERT INTO SALESMAN " + 79cdf0e10cSrcweir "VALUES (2, 'Frank', 'Jones','Lake Silver','CA',95460," 80cdf0e10cSrcweir + "'1963-12-24')"); 81cdf0e10cSrcweir stmt.executeUpdate("INSERT INTO SALESMAN " + 82cdf0e10cSrcweir "VALUES (3, 'Jane', 'Esperansa','23 Hollywood drive','CA',95460," 83cdf0e10cSrcweir + "'1972-04-01')"); 84cdf0e10cSrcweir stmt.executeUpdate("INSERT INTO SALESMAN " + 85cdf0e10cSrcweir "VALUES (4, 'George', 'Flint','12 Washington street','CA',95460," 86cdf0e10cSrcweir + "'1953-02-13')"); 87cdf0e10cSrcweir stmt.executeUpdate("INSERT INTO SALESMAN " + 88cdf0e10cSrcweir "VALUES (5, 'Bob', 'Meyers','2 Moon way','CA',95460," 89cdf0e10cSrcweir + "'1949-09-07')"); 90cdf0e10cSrcweir } 91cdf0e10cSrcweir 92cdf0e10cSrcweir // update the table sales man with a prepared statement. updateSalesMan()93cdf0e10cSrcweir public void updateSalesMan() throws com.sun.star.uno.Exception 94cdf0e10cSrcweir { 95cdf0e10cSrcweir XPreparedStatement updateStreet = con.prepareStatement( 96cdf0e10cSrcweir "UPDATE SALESMAN SET STREET = ? WHERE SNR = ?"); 97cdf0e10cSrcweir XParameters setPara = (XParameters)UnoRuntime.queryInterface(XParameters.class,updateStreet); 98cdf0e10cSrcweir setPara.setString(1, "34 Main Road"); 99cdf0e10cSrcweir setPara.setInt(2, 1); 100cdf0e10cSrcweir updateStreet.executeUpdate(); 101cdf0e10cSrcweir 102cdf0e10cSrcweir setPara.setString(1, "Marryland"); 103cdf0e10cSrcweir setPara.setInt(2, 4); 104cdf0e10cSrcweir updateStreet.executeUpdate(); 105cdf0e10cSrcweir // changes STREET column of salesman George to Marryland 106cdf0e10cSrcweir setPara.setString(1, "Michigan road"); 107cdf0e10cSrcweir updateStreet.executeUpdate(); 108cdf0e10cSrcweir // changes again STREET column of salesman George to 109cdf0e10cSrcweir // Michigan road 110cdf0e10cSrcweir // parameter 2 stayed 4, and the first parameter was reset 111cdf0e10cSrcweir // to "Michigan road") 112cdf0e10cSrcweir 113cdf0e10cSrcweir setPara.setString(1, "Bond Street"); 114cdf0e10cSrcweir setPara.setInt(2, 3); 115cdf0e10cSrcweir int n = updateStreet.executeUpdate(); 116cdf0e10cSrcweir System.out.println("executeUpdate returns: " + n); 117cdf0e10cSrcweir // n = 1 because one row had a change in it 118cdf0e10cSrcweir } 119cdf0e10cSrcweir 120cdf0e10cSrcweir // retrieve the data of the table salesman retrieveSalesManData()121cdf0e10cSrcweir public void retrieveSalesManData() throws com.sun.star.uno.Exception 122cdf0e10cSrcweir { 123cdf0e10cSrcweir XStatement stmt = con.createStatement(); 124cdf0e10cSrcweir XResultSet rs = stmt.executeQuery("SELECT FIRSTNAME, LASTNAME, BIRTHDATE FROM SALESMAN"); 125cdf0e10cSrcweir XRow row = (XRow)UnoRuntime.queryInterface(XRow.class,rs); 126cdf0e10cSrcweir while ( rs != null && rs.next() ) { 127cdf0e10cSrcweir String fn = row.getString( 1 ); 128cdf0e10cSrcweir String ln = row.getString( 2 ); 129cdf0e10cSrcweir Date dt = row.getDate( 3 ); 130cdf0e10cSrcweir System.out.println(fn + " " + ln + " " + dt.Month + "/" + dt.Day + "/" + dt.Year); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir } 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135