# LuckyNumberDBClient.jws Sample

This topic inludes the source code for the LuckyNumberDBClient.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/lucky_number_db/

## Sample Source Code

`001 package database.lucky_number_db; 002 003 import java.sql.SQLException; 004 import java.util.Iterator; 005 import database.lucky_number_db.LuckyNumberControl; 006 import database.lucky_number_db.LuckyNumberDB; 007 import com.bea.control.JwsContext; 008 009 /** 010 * <p>A sample web service that exercises the CustomerDB.jcx Database control 011 * extension. This sample uses the PLAYERS database table that is created 012 * and populated when WebLogic Workshop is installed.</p> 013 * @common:target-namespace namespace="http://workshop.bea.com/LuckyNumberDBClient" 014 */ 015 public class LuckyNumberDBClient implements com.bea.jws.WebService 016 { 017 /** 018 * @common:context 019 */ 020 JwsContext context; 021 022 /** 023 * @common:control 024 */ 025 private database.lucky_number_db.LuckyNumberControl luckyNumber; 026 027 /** 028 * @common:control 029 */ 030 private LuckyNumberDB luckyNumberDB; 031 032 /** 033 * <p>Starts a conversation.</p> 034 * 035 * <p>The default players have lucky numbers in the range [1,20] inclusive.</p> 036 * 037 * @common:operation 038 * @jws:conversation phase="start" 039 */ 040 public void start() throws SQLException 041 { 042 return; 043 } 044 045 /** 046 * <p>Gets a lucky number from LuckyNumber.jws, then returns a string containing 047 * the number and the list of winners, if any.</p> 048 * 049 * @common:operation 050 * @jws:conversation phase="continue" 051 */ 052 public String drawNumberAndGetWinner() throws Exception 053 { 054 return listWinnersForNumber(luckyNumber.getLuckyNumber()); 055 } 056 057 /** 058 * <p>Returns a list of all players in the database.</p> 059 * 060 * <p>Players's names and lucky numbers are returned in a string, one player per line.</p> 061 * 062 * @common:operation 063 * @jws:conversation phase="continue" 064 */ 065 public String listPlayers() throws Exception 066 { 067 StringBuffer sb = new StringBuffer("\nPlayers:\n"); 068 Iterator iter = luckyNumberDB.getAllPlayers(); 069 070 if (!iter.hasNext()) 071 { 072 sb.append("None"); 073 } 074 while (iter.hasNext()) 075 { 076 PlayerRecord rec = 077 (PlayerRecord)iter.next(); 078 sb.append("Number: "); 079 sb.append(rec.number); 080 sb.append(", Name: "); 081 sb.append(rec.name); 082 sb.append("\n"); 083 } 084 return sb.toString(); 085 } 086 087 /** 088 * <p>Returns a string containing the number and the list of winners, if any.</p> 089 * 090 * @param luckyNumber Winning number for which winners will be returned. 091 * @return A string containing the number and the list of winners, if any. 092 * 093 * @common:operation 094 * @jws:conversation phase="continue" 095 */ 096 public String listWinnersForNumber(int luckyNumber) throws Exception 097 { 098 StringBuffer sb = new StringBuffer("Number: " + luckyNumber + "; Winners: "); 099 Iterator iter = luckyNumberDB.getAllWinners(luckyNumber); 100 if (!iter.hasNext()) 101 { 102 sb.append("None"); 103 } 104 while (iter.hasNext()) 105 { 106 PlayerRecord rec = 107 (PlayerRecord)iter.next(); 108 sb.append(rec.name); 109 if (iter.hasNext()) 110 { 111 sb.append(", "); 112 } 113 } 114 return sb.toString(); 115 } 116 117 /** 118 * <p>Terminates the client conversation.</p> 119 * 120 * <p>This method should be called when the client is finished using the web service.</p> 121 * 122 * @common:operation 123 * @jws:conversation phase="finish" 124 */ 125 public void end() 126 { 127 return; 128 } 129 130 131 }`