# DatabaseController.jpf Sample

This topic inludes the source code for the DatabaseController.jpf Sample.

## Sample Location

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

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebApp/controls/database_iterator/

## Sample Source Code

`001 package controls.database_iterator; 002 003 import com.bea.wlw.netui.pageflow.*; 004 import java.io.Serializable; 005 import java.sql.ResultSet; 006 import java.sql.SQLException; 007 import java.util.ArrayList; 008 import java.util.Iterator; 009 import java.util.Vector; 010 import javax.sql.RowSet; 011 012 /** 013 * This Page Plow queries a database by calling methods on the 014 * the database control file UsersDBControl.jcx. Each method 015 * on the control sends a SQL query to the database. There are methods for 016 * selecting, inserting, updating, and deleting records. 017 * 018 * This Page Plow provides a user interface for the database control, 019 * invokes the control methods, and displays the results 020 * on the JSP page list_users.jsp. 021 */ 022 023 /** 024 * @jpf:view-properties view-properties:: 025 * <!-- This data is auto-generated. Hand-editing this section is not recommended. --> 026 * <view-properties> 027 * <pageflow-object id="pageflow:/controls/database_iterator/databaseController.jpf"/> 028 * <pageflow-object id="action:begin.do"> 029 * <property name="x" value="60"/> 030 * <property name="y" value="200"/> 031 * </pageflow-object> 032 * <pageflow-object id="action:addUser.do#controls.database_iterator.databaseController.UserForm"> 033 * <property value="200" name="x"/> 034 * <property value="80" name="y"/> 035 * </pageflow-object> 036 * <pageflow-object id="action:deleteUser.do"> 037 * <property name="x" value="380"/> 038 * <property name="y" value="200"/> 039 * </pageflow-object> 040 * <pageflow-object id="action-call:@page:list_users.jsp@#@action:deleteUser.do@"> 041 * <property value="236,290,290,344" name="elbowsX"/> 042 * <property value="192,192,192,192" name="elbowsY"/> 043 * <property value="East_1" name="fromPort"/> 044 * <property value="West_1" name="toPort"/> 045 * </pageflow-object> 046 * <pageflow-object id="action-call:@page:list_users.jsp@#@action:addUser.do#controls.database_iterator.databaseController.UserForm@"> 047 * <property value="200,200,200,200" name="elbowsX"/> 048 * <property value="156,140,140,124" name="elbowsY"/> 049 * <property value="North_1" name="fromPort"/> 050 * <property value="South_1" name="toPort"/> 051 * </pageflow-object> 052 * <pageflow-object id="page:list_users.jsp"> 053 * <property name="x" value="200"/> 054 * <property name="y" value="200"/> 055 * </pageflow-object> 056 * <pageflow-object id="forward:path#success#list_users.jsp#@action:begin.do@"> 057 * <property name="elbowsY" value="192,192,192,192"/> 058 * <property name="toPort" value="West_1"/> 059 * <property name="elbowsX" value="96,130,130,164"/> 060 * <property name="label" value="success"/> 061 * <property name="fromPort" value="East_1"/> 062 * </pageflow-object> 063 * <pageflow-object id="forward:path#success#list_users.jsp#@action:addUser.do#controls.database_iterator.databaseController.UserForm@"> 064 * <property value="189,189,189,189" name="elbowsX"/> 065 * <property value="124,140,140,156" name="elbowsY"/> 066 * <property value="South_0" name="fromPort"/> 067 * <property value="North_0" name="toPort"/> 068 * <property value="success" name="label"/> 069 * </pageflow-object> 070 * <pageflow-object id="forward:path#success#list_users.jsp#@action:deleteUser.do@"> 071 * <property name="elbowsY" value="192,192,192,192"/> 072 * <property name="toPort" value="East_1"/> 073 * <property name="elbowsX" value="344,290,290,236"/> 074 * <property name="label" value="success"/> 075 * <property name="fromPort" value="West_1"/> 076 * </pageflow-object> 077 * <pageflow-object id="control:controls.database_iterator.dbcontrol_wrapper.Wrapper#wrapper"> 078 * <property value="26" name="x"/> 079 * <property value="34" name="y"/> 080 * </pageflow-object> 081 * <pageflow-object id="formbeanprop:controls.database_iterator.databaseController.UserForm#username#java.lang.String"/> 082 * <pageflow-object id="formbeanprop:controls.database_iterator.databaseController.UserForm#password#java.lang.String"/> 083 * <pageflow-object id="formbeanprop:controls.database_iterator.databaseController.UserForm#message#java.lang.String"/> 084 * <pageflow-object id="formbean:controls.database_iterator.databaseController.UserForm"/> 085 * </view-properties> 086 * :: 087 * 088 */ 089 public class databaseController extends PageFlowController 090 { 091 092 /** 093 * @common:control 094 */ 095 private controls.database_iterator.dbcontrol_wrapper.Wrapper wrapper; 096 097 /** 098 * Declare the database control. 099 * You can now call methods on the database control through 100 * the 'users' object. 101 * 102 * @common:control 103 */ 104 //private UsersDBControl users; 105 106 107 /** 108 * This method queries the USERS table with the SQL query: SELECT * FROM USERS 109 */ 110 public ArrayList getAllUsers() throws SQLException 111 { 112 return wrapper.getAllUsers(); 113 } 114 115 /** 116 * When this Page Flow is first invoked, it tries to create and populate the 117 * table USERS. This ensures that the database control does not query 118 * a non-existent table. 119 * 120 * @jpf:action 121 * @jpf:forward name="success" path="list_users.jsp" 122 */ 123 public Forward begin() 124 { 125 try 126 { 127 wrapper.createUsersTable(); 128 wrapper.insertUser( "JohnS", "1234" ); 129 wrapper.insertUser( "SteveH", "5678" ); 130 } 131 catch( Exception e ) 132 { 133 // ignore -- the table might already have been created, 134 // which will cause an exception. 135 } 136 137 return new Forward( "success" ); 138 } 139 140 /** 141 * This method adds a record to the USERS table. 142 * 143 * @jpf:action 144 * @jpf:forward name="success" path="list_users.jsp" 145 */ 146 public Forward addUser( UserForm form ) 147 { 148 if ( form.getUsername().length() == 0 || form.getPassword().length() == 0 ) 149 { 150 form.setMessage( "Please enter both a username and a password." ); 151 return new Forward( "success", form ); 152 } 153 154 wrapper.insertUser( form.getUsername(), form.getPassword() ); 155 return new Forward( "success", new UserForm() ); 156 } 157 158 /** 159 * This method deletes a record from the USERS table. 160 * 161 * @jpf:action 162 * @jpf:forward name="success" path="list_users.jsp" 163 */ 164 public Forward deleteUser() 165 { 166 wrapper.deleteUser( getRequest().getParameter( "userToDelete" ) ); 167 return new Forward( "success", new UserForm() ); 168 } 169 170 public static class UserForm extends FormData 171 { 172 private String _username; 173 private String _password; 174 private String _message; 175 176 public String getUsername() 177 { 178 return _username; 179 } 180 181 public void setUsername( String username ) 182 { 183 _username = username; 184 } 185 186 public String getPassword() 187 { 188 return _password; 189 } 190 191 public void setPassword( String password ) 192 { 193 _password = password; 194 } 195 196 public String getMessage() 197 { 198 return _message; 199 } 200 201 public void setMessage( String message ) 202 { 203 _message = message; 204 } 205 } 206 }`