The database table that is queried is EMPLOYEE. The premise for this example is that the company is giving all employees a 10% raise. We create a new column named NEW_SALARY and create an expression that multiplies the existing SALARY data by 1.10 and places the resulting value in the NEW_SALARY column. The completed project is available in the /samples/DataExpress/CalculatedColumn subdirectory of your JBuilder installation under the project name CalculatedColumn.jpr.
Open the connection property editor for the Database component by selecting, then double-clicking the connection property ellipsis in the Inspector. Set the connection properties to the JDataStore sample employee table as follows. The Connection URL points to a specific installation location. If you have installed JBuilder to a different directory, point to the correct location for your installation.
Property name | Value |
Driver | com.borland.datastore.jdbc.DataStoreDriver |
URL | jdbc:borland:dslocal:/usr/local/jbuilder/samples/JDataStore/
datastores/employee.jds
The employee.jds database is located under the samples directory of your JBuilder installation, which may be different on your system. This sample has been modified to find the file on your system. |
Username | Enter your name |
Password | not required |
The connection dialog includes a Test Connection button. Click this button to check that the connection properties have been correctly set. Results of the connection attempt are displayed in the status area. When the connection is successful, click OK.
Select the query property of the QueryDataSet component in the Inspector, double-click its ellipsis to open the QueryDescriptor dialog, and set the following properties:
Property name | Value |
Database | database1 |
SQL Statement | SELECT * FROM EMPLOYEE |
Click Test Query to ensure that the query is runnable. When the status area indicates Success, click OK to close the dialog.
To view the data in your application we need to add some UI components and bind them to the data set. To do this,
The JdbNavToolBar will enable you to move quickly through the data set when the application is running, as well as provide a default mechanism for saving changes back to your data source.
Among other information, the status label displays information about the current record or current operation.
Scrolling behavior is not available by default in any Swing component or dbSwing extension, so, to get scrolling behavior, we add the scrollable Swing or dbSwing components to a JScrollPane or a TableScrollPane. TableScrollPane provides special capabilities to JdbTable over JScrollPane. See the dbSwing documentation for more information.
You'll notice that the designer displays live data at this point.
To add the calculated column functionality,
Property name | Value |
calcType | CALC |
caption | NEW_SALARY |
columnName | NEW_SALARY |
dataType | BIGDECIMAL |
currency | true |
No data will be displayed in the calculated column in the table in the designer. The calculations are only visible when the application is running. The data type of BIGDECIMAL is used here because that is the data type of the SALARY column which will be used in the calculation expression. Calculated columns are always read-only.
void queryDataSet1_calcFields(ReadRow changedRow, DataRow calcRow, boolean isPosted) throws DataSetException{ //calculate the new salary calcRow.setBigDecimal("NEW_SALARY", changedRow.getBigDecimal("SALARY").multiply(new BigDecimal(1.1))); }
This method is called for calcFields whenever a field value is saved and whenever a row is posted. This event passes in an input which is the current values in the row (changedRow), an output row for putting any changes you want to make to the row (calcRow), and a boolean (isPosted) that indicates whether the row is posted in the DataSet or not. You may not want to recalculate fields on rows that are not posted yet.
import java.math.BigDecimal;
Change a value in the SALARY column. When the application is running, the values in the calculated column will automatically adjust to changes in any columns referenced in the calculated expression. The NEW_SALARY columns displays the value of (SALARY * 1.10).