# 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/

## Sample Source Code

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