1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com
.sun
.star
.uno
.*;
37 import com
.sun
.star
.util
.Date
;
38 import com
.sun
.star
.sdbc
.*;
42 private final XConnection con
;
44 public SalesMan(XConnection connection
)
48 // create the table salesman.
49 public void createSalesManTable() throws com
.sun
.star
.uno
.Exception
51 String createTableSalesman
= "CREATE TABLE SALESMAN " +
52 "(SNR INTEGER NOT NULL, "+
53 " FIRSTNAME VARCHAR(50)," +
54 " LASTNAME VARCHAR(100)," +
55 " STREET VARCHAR(50)," +
56 " STATE VARCHAR(50)," +
61 XStatement stmt
= con
.createStatement();
62 stmt
.executeUpdate( createTableSalesman
);
65 // drop the table salesman
66 public void dropSalesManTable() throws com
.sun
.star
.uno
.Exception
68 String createTableSalesman
= "DROP TABLE SALESMAN ";
69 XStatement stmt
= con
.createStatement();
70 stmt
.executeUpdate( createTableSalesman
);
73 // insert data into the table salesman
74 public void insertDataIntoSalesMan() throws com
.sun
.star
.uno
.Exception
76 XStatement stmt
= con
.createStatement();
77 stmt
.executeUpdate("INSERT INTO SALESMAN " +
78 "VALUES (1, 'Joseph', 'Smith','Bond Street','CA',95460,"
80 stmt
.executeUpdate("INSERT INTO SALESMAN " +
81 "VALUES (2, 'Frank', 'Jones','Lake Silver','CA',95460,"
83 stmt
.executeUpdate("INSERT INTO SALESMAN " +
84 "VALUES (3, 'Jane', 'Esperansa','23 Hollywood drive','CA',95460,"
86 stmt
.executeUpdate("INSERT INTO SALESMAN " +
87 "VALUES (4, 'George', 'Flint','12 Washington street','CA',95460,"
89 stmt
.executeUpdate("INSERT INTO SALESMAN " +
90 "VALUES (5, 'Bob', 'Meyers','2 Moon way','CA',95460,"
94 // update the table sales man with a prepared statement.
95 public void updateSalesMan() throws com
.sun
.star
.uno
.Exception
97 XPreparedStatement updateStreet
= con
.prepareStatement(
98 "UPDATE SALESMAN SET STREET = ? WHERE SNR = ?");
99 XParameters setPara
= UnoRuntime
.queryInterface(XParameters
.class,updateStreet
);
100 setPara
.setString(1, "34 Main Road");
101 setPara
.setInt(2, 1);
102 updateStreet
.executeUpdate();
104 setPara
.setString(1, "Marryland");
105 setPara
.setInt(2, 4);
106 updateStreet
.executeUpdate();
107 // changes STREET column of salesman George to Marryland
108 setPara
.setString(1, "Michigan road");
109 updateStreet
.executeUpdate();
110 // changes again STREET column of salesman George to
112 // parameter 2 stayed 4, and the first parameter was reset
113 // to "Michigan road")
115 setPara
.setString(1, "Bond Street");
116 setPara
.setInt(2, 3);
117 int n
= updateStreet
.executeUpdate();
118 System
.out
.println("executeUpdate returns: " + n
);
119 // n = 1 because one row had a change in it
122 // retrieve the data of the table salesman
123 public void retrieveSalesManData() throws com
.sun
.star
.uno
.Exception
125 XStatement stmt
= con
.createStatement();
126 XResultSet rs
= stmt
.executeQuery("SELECT FIRSTNAME, LASTNAME, BIRTHDATE FROM SALESMAN");
127 XRow row
= UnoRuntime
.queryInterface(XRow
.class,rs
);
128 while ( rs
!= null && rs
.next() ) {
129 String fn
= row
.getString( 1 );
130 String ln
= row
.getString( 2 );
131 Date dt
= row
.getDate( 3 );
132 System
.out
.println(fn
+ " " + ln
+ " " + dt
.Month
+ "/" + dt
.Day
+ "/" + dt
.Year
);
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */