# CustomerDBClient.jws Sample

This topic inludes the source code for the CustomerDBClient.jws Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebServices/database/customer_db/

## Sample Source Code

`001 package database.customer_db; 002 003 import java.sql.SQLException; 004 import java.sql.ResultSet; 005 006 /** 007 * <p>A sample web service that exercises the CustomerDB.jcx Database control 008 * extension. This sample uses the CUSTOMER database table that is created 009 * and populated when WebLogic Workshop is installed.</p> 010 * 011 * @common:xmlns namespace="http://openuri.org/bea/samples/workshop/database" prefix="ns0" 012 * @common:target-namespace namespace="http://workshop.bea.com/CustomerDBClient" 013 */ 014 public class CustomerDBClient implements com.bea.jws.WebService 015 { 016 /** 017 * <p>This is a Database control. It is defined in 018 * CustomerDB.jcx. The @common:control tag 019 * causes display as a control in the IDE's 020 * Design View.</p> 021 * 022 * @common:control 023 */ 024 private CustomerDB customerDB; 025 026 /** 027 * @common:operation 028 */ 029 public String badSQL() 030 { 031 try 032 { 033 customerDB.badSQL(); 034 } 035 catch (java.sql.SQLException e) 036 { 037 return "Failed: " + e.getMessage(); 038 } 039 return "Created"; 040 041 } 042 043 /** 044 * <p>Use a database control method which inserts a row from individual arguments.</p> 045 * 046 * @return The number of rows inserted. 047 * 048 * @param custid The key of the customer record. 049 * @param name The new name for the customer record. 050 * @param address The new street address for the customer record. 051 * @param city The new city for the customer record. 052 * @param state The new 2 letter state code for the customer record. 053 * @param zip The new 9 digit zip code for the customer record. 054 * @param area The new 3 digit area code for the customer record. 055 * @param phone The new 7 digit phone number for the customer record. 056 * 057 * @common:operation 058 */ 059 public int insertCustomer(int custid, String name, String address, String city, String state, String zip, String area, String phone) 060 { 061 int numInserted = customerDB.insertCustomer(custid, name, address, city, state, zip, area, phone); 062 return numInserted; 063 } 064 065 /** 066 * @common:operation 067 */ 068 public String [] getAllCustomerNamesArrayFromResultSet() throws Exception 069 { 070 java.sql.ResultSet resultSet; 071 String [] custNames; 072 int numCustomers = customerDB.getAllCustomerNames().length; 073 int i = 0; 074 075 if( numCustomers > 0 ) 076 { 077 custNames = new String [numCustomers]; 078 resultSet = customerDB.findAllCustomersResultSet(); 079 while (resultSet.next()) 080 { 081 custNames[i++] = new String(resultSet.getString("name")); 082 } 083 } 084 else 085 { 086 custNames = new String [1]; 087 custNames[0] = "no records found"; 088 } 089 return custNames; 090 } 091 092 093 /** 094 * @common:operation 095 */ 096 public String[] getAllCustomerNamesArray() 097 { 098 return customerDB.getAllCustomerNames(); 099 } 100 101 102 /** 103 * <p>Use a database control method which returns all records, 104 * using an Iterator internally.</p> 105 * 106 * @return Multiple customer records delimited by ". ". 107 * 108 * @common:operation 109 */ 110 public String getAllCustomerNamesString() 111 { 112 java.util.Iterator iter = null; 113 StringBuffer sb = new StringBuffer(); 114 115 iter = customerDB.findAllCustomersIterator(); 116 if( iter.hasNext() ) 117 { 118 while (iter.hasNext()) 119 { 120 Customer c = (Customer)iter.next(); 121 sb.append( c.getCustid() ).append(':').append( c.getName() ).append(". "); 122 } 123 } 124 else 125 { 126 sb.append("No records in table"); 127 } 128 return sb.toString(); 129 } 130 131 132 /** 133 * <p>Use a database control method which returns all records, 134 * using an array internally. Return that array directly 135 * to the caller using the default XML mapping.</p> 136 * 137 * @common:operation 138 */ 139 public Customer [] getAllCustomerRecordsArray() 140 { 141 Customer [] custArray; 142 custArray = customerDB.findAllCustomersArray(); 143 return custArray; 144 } 145 146 /** 147 * <p>Use a database control method to retrieve a customer record.</p> 148 * 149 * @return The customer's name. Return a message saying the customer 150 * is not found, if the customer is not in the table. 151 * 152 * @param custid The key of the customer record. 153 * 154 * @common:operation 155 */ 156 public String getCustomerName(int custid) 157 { 158 Customer cust = null; 159 cust = customerDB.findCustomerByID(custid); 160 if( cust != null ) 161 { 162 return cust.getName(); 163 } 164 else 165 { 166 return("Customer not found"); 167 } 168 169 } 170 171 172 /** 173 * <p>Use a database control method to get the customer's street address.</p> 174 * 175 * <p>In this example, the customer record is returned in a HashMap, and then the 176 * address is found by searching the HashMap with an UPPERCASE column name. 177 * Database column names are always accessed in UPPERCASE in a HashMap.</p> 178 * 179 * @return The customer's street address. Return a message saying the customer 180 * is not found, if the customer is not in the table. 181 * 182 * @param custid The key of the customer record. 183 * 184 * @common:operation 185 */ 186 public String getCustomerStreetAddress(int custid) 187 { 188 java.util.HashMap custHash = null; 189 custHash = customerDB.findCustomerHashByID(custid); 190 if (custHash != null) 191 { 192 return (String)custHash.get("ADDRESS"); 193 } 194 else 195 { 196 return ("Customer not found"); 197 } 198 } 199 200 /** 201 * 202 * <p>Use a database control method to get the customer's full address.</p> 203 * 204 * <p>In this example, the customer record is returned in a Customer 205 * object. The members of interest are then returned to the caller 206 * in a custom XML map.</p> 207 * 208 * @return The city portion of the customer's record. 209 * 210 * @param custid The key of the customer record. 211 * 212 * @common:operation 213 * @jws:return-xml schema-element="ns0:full-address" xquery:: 214 * declare namespace ns0 = "http://workshop.bea.com/CustomerDBClient" 215 * declare namespace ns1 = "http://openuri.org/bea/samples/workshop/database" 216 * 217 * <ns1:full-address> 218 * <ns1:address>{ data($input/ns0:getCustomerFullAddressResult/ns0:Address) }</ns1:address> 219 * <ns1:city>{ data($input/ns0:getCustomerFullAddressResult/ns0:City) }</ns1:city> 220 * <ns1:state>{ data($input/ns0:getCustomerFullAddressResult/ns0:State) }</ns1:state> 221 * <ns1:zip>{ data($input/ns0:getCustomerFullAddressResult/ns0:Zip) }</ns1:zip> 222 * </ns1:full-address> 223 * :: 224 */ 225 public Customer getCustomerFullAddress(int custid) 226 { 227 return customerDB.findCustomerByID(custid); 228 } 229 230 231 /** 232 * <p>Use a database control method which inserts a row from an object.</p> 233 * 234 * @return The number of rows inserted. 235 * 236 * @param custid The key of the customer record. 237 * @param name The new name for the customer record. 238 * @param address The new street address for the customer record. 239 * @param city The new city for the customer record. 240 * @param state The new 2 letter state code for the customer record. 241 * @param zip The new 9 digit zip code for the customer record. 242 * @param area The new 3 digit area code for the customer record. 243 * @param phone The new 7 digit phone number for the customer record. 244 * 245 * @common:operation 246 */ 247 public int insertCustomerObj(int custid, String name, String address, String city, String state, String zip, String area, String phone) 248 { 249 Customer custObj = new Customer( custid, name, address, city, state, zip, 250 area, phone); 251 int numInserted = customerDB.insertCustomerObject(custObj); 252 return numInserted; 253 } 254 255 256 /** 257 * <p>Use a database control method which updates a row.</p> 258 * 259 * @param custid The key of the customer record. 260 * @param address The new address to be stored in the customer record. 261 * 262 * @common:operation 263 */ 264 public void updateAddress(int custid, String address) 265 throws Exception 266 { 267 int numUpdated = customerDB.changeAddress(custid, address); 268 if (numUpdated != 1) 269 { 270 throw new Exception("Error updating address for customer " + custid + "."); 271 } 272 } 273 274 275 /** 276 * <p>Use a database control method that deletes a row.</p> 277 * 278 * @return The number of rows deleted. 279 * 280 * @param custid The key of the customer record to delete. 281 * 282 * @common:operation 283 */ 284 public int deleteCustomer(int custid) 285 { 286 int numDeleted = customerDB.deleteCustomer(custid); 287 return numDeleted; 288 } 289 290 291 /** 292 * <p>Demonstrates how to call an internal database function.</p> 293 * 294 * <p>Pass an array of integers when invoking the method. The 295 * database will return an records with custid's within the 296 * array of integers. For example, pass the array [1,2] or [2,3].</p> 297 * 298 * @return Customer[] object 299 * 300 * @param an array of integers, corresponding to custid's 301 * 302 * @common:operation 303 */ 304 public database.customer_db.Customer[] callInternalFunction(java.lang.Integer[] customerIDs) 305 { 306 return customerDB.callInternalFunction(customerIDs); 307 } 308 309 }`