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 SalesMan
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 	private XConnection con;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	public SalesMan(XConnection connection )
54*cdf0e10cSrcweir 	{
55*cdf0e10cSrcweir 		con = connection;
56*cdf0e10cSrcweir 	}
57*cdf0e10cSrcweir 	// create the table salesman.
58*cdf0e10cSrcweir 	public void createSalesManTable() throws com.sun.star.uno.Exception
59*cdf0e10cSrcweir 	{
60*cdf0e10cSrcweir 		String createTableSalesman = "CREATE TABLE SALESMAN " +
61*cdf0e10cSrcweir 				"(SNR INTEGER NOT NULL, "+
62*cdf0e10cSrcweir 				" FIRSTNAME VARCHAR(50)," +
63*cdf0e10cSrcweir 				" LASTNAME VARCHAR(100)," +
64*cdf0e10cSrcweir 				" STREET VARCHAR(50)," +
65*cdf0e10cSrcweir 				" STATE VARCHAR(50)," +
66*cdf0e10cSrcweir 				" ZIP INTEGER," +
67*cdf0e10cSrcweir 				" BIRTHDATE DATE," +
68*cdf0e10cSrcweir 				" PRIMARY KEY(SNR)" +
69*cdf0e10cSrcweir 				" )";
70*cdf0e10cSrcweir 		XStatement stmt = con.createStatement();
71*cdf0e10cSrcweir 		stmt.executeUpdate( createTableSalesman );
72*cdf0e10cSrcweir 	}
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 	// drop the table salesman
75*cdf0e10cSrcweir 	public void dropSalesManTable() throws com.sun.star.uno.Exception
76*cdf0e10cSrcweir 	{
77*cdf0e10cSrcweir 		String createTableSalesman = "DROP TABLE SALESMAN ";
78*cdf0e10cSrcweir 		XStatement stmt = con.createStatement();
79*cdf0e10cSrcweir 		stmt.executeUpdate( createTableSalesman );
80*cdf0e10cSrcweir 	}
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 	// insert data into the table salesman
83*cdf0e10cSrcweir 	public void insertDataIntoSalesMan() throws com.sun.star.uno.Exception
84*cdf0e10cSrcweir 	{
85*cdf0e10cSrcweir 		XStatement stmt = con.createStatement();
86*cdf0e10cSrcweir 		stmt.executeUpdate("INSERT INTO SALESMAN " +
87*cdf0e10cSrcweir 				"VALUES (1, 'Joseph', 'Smith','Bond Street','CA',95460,"
88*cdf0e10cSrcweir 				+ "'1946-07-02')");
89*cdf0e10cSrcweir 		stmt.executeUpdate("INSERT INTO SALESMAN " +
90*cdf0e10cSrcweir 				"VALUES (2, 'Frank', 'Jones','Lake Silver','CA',95460,"
91*cdf0e10cSrcweir 				+ "'1963-12-24')");
92*cdf0e10cSrcweir 		stmt.executeUpdate("INSERT INTO SALESMAN " +
93*cdf0e10cSrcweir 				"VALUES (3, 'Jane', 'Esperansa','23 Hollywood drive','CA',95460,"
94*cdf0e10cSrcweir 				+ "'1972-04-01')");
95*cdf0e10cSrcweir 		stmt.executeUpdate("INSERT INTO SALESMAN " +
96*cdf0e10cSrcweir 				"VALUES (4, 'George', 'Flint','12 Washington street','CA',95460,"
97*cdf0e10cSrcweir 				+ "'1953-02-13')");
98*cdf0e10cSrcweir 		stmt.executeUpdate("INSERT INTO SALESMAN " +
99*cdf0e10cSrcweir 				"VALUES (5, 'Bob', 'Meyers','2 Moon way','CA',95460,"
100*cdf0e10cSrcweir 				+ "'1949-09-07')");
101*cdf0e10cSrcweir 	}
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	// update the table sales man with a prepared statement.
104*cdf0e10cSrcweir 	public void updateSalesMan() throws com.sun.star.uno.Exception
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		XPreparedStatement updateStreet = con.prepareStatement(
107*cdf0e10cSrcweir 			"UPDATE SALESMAN SET STREET = ? WHERE SNR = ?");
108*cdf0e10cSrcweir 		XParameters setPara = (XParameters)UnoRuntime.queryInterface(XParameters.class,updateStreet);
109*cdf0e10cSrcweir 		setPara.setString(1, "34 Main Road");
110*cdf0e10cSrcweir 		setPara.setInt(2, 1);
111*cdf0e10cSrcweir 		updateStreet.executeUpdate();
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 		setPara.setString(1, "Marryland");
114*cdf0e10cSrcweir 		setPara.setInt(2, 4);
115*cdf0e10cSrcweir 		updateStreet.executeUpdate();
116*cdf0e10cSrcweir 		// changes STREET column of salesman George to Marryland
117*cdf0e10cSrcweir 		setPara.setString(1, "Michigan road");
118*cdf0e10cSrcweir 		updateStreet.executeUpdate();
119*cdf0e10cSrcweir 		// changes again STREET column of salesman George to
120*cdf0e10cSrcweir 		// Michigan road
121*cdf0e10cSrcweir 		// parameter 2 stayed 4, and the first parameter was reset
122*cdf0e10cSrcweir 		// to "Michigan road")
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 		setPara.setString(1, "Bond Street");
125*cdf0e10cSrcweir 		setPara.setInt(2, 3);
126*cdf0e10cSrcweir 		int n = updateStreet.executeUpdate();
127*cdf0e10cSrcweir 		System.out.println("executeUpdate returns: " + n);
128*cdf0e10cSrcweir 		// n = 1 because one row had a change in it
129*cdf0e10cSrcweir 	}
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	// retrieve the data of the table salesman
132*cdf0e10cSrcweir 	public void retrieveSalesManData() throws com.sun.star.uno.Exception
133*cdf0e10cSrcweir 	{
134*cdf0e10cSrcweir 		XStatement stmt = con.createStatement();
135*cdf0e10cSrcweir 		XResultSet rs	= stmt.executeQuery("SELECT FIRSTNAME, LASTNAME, BIRTHDATE FROM SALESMAN");
136*cdf0e10cSrcweir 		XRow row = (XRow)UnoRuntime.queryInterface(XRow.class,rs);
137*cdf0e10cSrcweir 		while ( rs != null && rs.next() ) {
138*cdf0e10cSrcweir 			String fn = row.getString( 1 );
139*cdf0e10cSrcweir 			String ln = row.getString( 2 );
140*cdf0e10cSrcweir 			Date   dt = row.getDate( 3 );
141*cdf0e10cSrcweir 			System.out.println(fn + "    " + ln + "    " + dt.Month + "/" + dt.Day + "/" + dt.Year);
142*cdf0e10cSrcweir 		}
143*cdf0e10cSrcweir 	}
144*cdf0e10cSrcweir }
145*cdf0e10cSrcweir 
146