Database application development is a feature of JBuilder Professional and Enterprise. Distributed application development is a feature of JBuilder Enterprise.
Exporting data, or saving data to a text file, saves all of the data in the current view to the text file, overwriting the existing data. This topic discusses
several ways to export data. You can export data that has been imported from a text file back to that file or to another file. You can export data from a QueryDataSet
or a ProcedureDataSet
to a text file. Or you can resolve data from a TableDataSet
to an existing SQL table.
Exporting data to a text file is handled differently than resolving data to a SQL table. Both QueryDataSet
and TableDataSet
are StorageDataSet
components. When data is provided to the data set, the StorageDataSet
tracks the row status
information (either deleted, inserted, or updated) for all rows. When data is resolved back to a data source like a SQL server, the row status information is used to determine which rows to add to, delete from, or modify in the SQL table. When a row has been successfully resolved, it obtains a new row status of resolved (either RowStatus.UPDATE_RESOLVED
, RowStatus.DELETE_RESOLVED
, or RowStatus.INSERT_RESOLVED
). If the StorageDataSet
is
resolved again, previously resolved rows will be ignored, unless changes have been made subsequent to previous resolving. When data is exported to a text file, all of the data in the current view is written to the text file, and the row status information is not affected.
Data exported to a text file is sensitive to the current sorting and filtering criteria. If sort criteria are specified, the data is saved to the text file in the same order as specified in the sort criteria. If row order is important, remove the sort criteria prior to exporting data. If filter criteria are specified, only the data that meets the filter criteria will be saved. This is useful for saving subsets of data to different files, but could cause data loss if a filtered file is inadvertently saved over an existing data file.
When you export data from a TableDataSet
to a text file, JBuilder creates a .SCHEMA file that defines the columns by name and data type. The next time you import the data into JBuilder, you do not have to define the columns, because this information is already specified in the .SCHEMA file.
Building on the example in "Tutorial: Importing data from a text file", this tutorial demonstrates how to use the UI designer to add a button for saving the data, with any changes, back to the same text file.
contentPane (BorderLayout)
in the content pane and change its layout
property to 'null' in the Inspector.
Select tableScrollPane1
in the component tree. In the UI designer, grab the upper handle and resize the component to allow room to add a button. See the screen shot of the running application further in this tutorial for general placement of components.
JButton
component from the Swing tab to the UI designer. On the Properties tab of the Inspector, set the text
property to Save Changes.
actionPerformed()
method. This changes the focus of the AppBrowser from the Design tab to the Source tab, and displays the stub for the actionPerformed()
method in the source code.
Add the following code to the actionPerformed()
method:
try { tableDataSet1.getDataFile().save(tableDataSet1); System.out.println("Changes saved"); } catch (Exception ex) { System.out.println("Changes NOT saved"); System.err.println("Exception: " + ex); }
Run the application by selecting Run|Run Project.
When you run the application, if it compiles successfully, the application appears in its own window.
Data is displayed in a table, with a Save Changes button. Make and view changes as follows:
Frame
window and change the value in the field from A to Apple. Save the changes back to the text file by clicking the Save Changes button.
1,"Apple" 2,"B" 3,"C"
Close the text file.
JBuilder automatically creates a .SCHEMA file to define the contents of the text file.
View the .SCHEMA file in a text editor. Notice that this file contains information about the name of the fields that have been exported and the type of data that was exported in that field. It looks like this:
[] FILETYPE = VARYING FILEFORMAT = Encoded ENCODING = ISO8859_1 LOCALE = en_US DELIMITER = " SEPARATOR = , FIELD0 = my_number,Variant.SHORT,-1,-1, FIELD1 = my_string,Variant.STRING,-1,-1,
Close the .SCHEMA file.
You can continue to edit, insert, delete, and save data until you close the application, but you must click the Save Changes button to write any changes back to the text file. When you save the changes, the existing file will be overwritten with data from the current view.
By default, JBuilder expects data entry and exports data of date, time, and currency fields according to the locale
property of the column. You can use the exportDisplayMask
property to read or save date, time, and number fields in a different
pattern. Complete the example in "Tutorial: Exporting data from a TableDataSet to a text file", close the running application, then complete the following steps in JBuilder. These steps demonstrate creating an exportDisplayMask
for a new column of type DATE.
Select Frame1.java
in the content pane, then select the Design tab. Expand tableDataSet1
in the component tree by clicking on the expand icon to its left. Select <new column>
, then modify the column's properties in the Inspector as follows:
dataType
to DATE
caption
and columnName
to my_date
Run the application. In the running application window, enter a date in the locale syntax of your computer in the my_date column of the first row. For example, with the locale
property set to English (United States), you
must enter the date in a format of MM/dd/yy, like 11/16/95. Click the Save Changes button to save the changes back to the text file.
View the text file in a text editor. It will now contain the following data:
1,"Apple",11/16/95 2,"B" 3,"C"
Close the text file.
View the .SCHEMA file in a text editor. Notice that the new date field has been added to the list of fields. It looks like this:
[] FILETYPE = VARYING FILEFORMAT = Encoded ENCODING = ISO8859_1 LOCALE = en_US DELIMITER = " SEPARATOR = , FIELD0 = my_number,Variant.SHORT,-1,-1, FIELD1 = my_string,Variant.STRING,-1,-1, FIELD2 = my_date,Variant.DATE,-1,-1,
Close the .SCHEMA file.
The next steps show what happens when you change the date pattern, edit the data, and save the changes again.
Close the running application and the text files and return to the JBuilder UI designer. Select the my_date column and enter the following pattern into the exportDisplayMask
property in the
Inspector: MM-dd-yyyy. The syntax of patterns is defined in "String-based patterns (masks)". This type of pattern will read and save the date field as follows: 11-16-1995.
The application would produce an error now if you tried to run it, because the format of the date field in the text file does not match the format the application is trying to open. Manually edit the text file and remove the value ",11/16/95" from the first row.
Instead of the above step, you could manually enter code that would establish one exportDisplayMask
for importing the data and another exportDisplayMask
for
exporting the data.
Run the application. In the running Frame window, enter a date in the my_date column of the first row using the format of the exportDisplayMask
property, such as 11-16-1995.
Click the Save Changes button to save the changes back to the text file.
View the text file in a text editor. It will now contain the following data:
1,"Apple",11-16-1995 2,"B" 3,"C"
Close the text file.
View the .SCHEMA file in a text editor. Notice that the date field format is displayed as part of the field definition. When the default format is used, this value is blank, as it is in the FIELD0 definition. It looks like this:
[] FILETYPE = VARYING FILEFORMAT = Encoded ENCODING = ISO8859_1 LOCALE = en_US DELIMITER = " SEPARATOR = , FIELD0 = my_number,Variant.SHORT,-1,-1, FIELD1 = my_string,Variant.STRING,-1,-1, FIELD2 = my_date,Variant.DATE,-1,-1,MM-dd-yyyy
Close the .SCHEMA file.
When the text data file is imported next, the data will be imported from the information in the .SCHEMA file. To view data in the table in a different pattern, set the displayMask
property. To modify data in the table using a different pattern, set the editMask
property. These properties affect viewing and editing of the data only; they do not affect the way data is saved. For example, to enter data into a currency field without having to enter the currency symbol each time, use a
displayMask
that uses the currency symbol, and an editMask
that does not contain a currency symbol. You can choose to save the data back to the text file with or without the currency symbol by setting the exportDisplayMask
.
Exporting data from a QueryDataSet
to a text file is the same as exporting data from a TableDataSet
component, as defined in "Tutorial: Exporting data from a TableDataSet to a text file". JBuilder will create a .SCHEMA file that defines each
column, its name, and its data type so that the file can be imported back into JBuilder more easily.
Use a QueryResolver
to resolve changes back to a SQL table. For more information on using the QueryResolver
to save changes to a SQL table, see "Customizing the default resolver logic".
Prior to resolving the changes back to the SQL table, you must set the table name and column names of the SQL table, as shown in the following code snippet. The SQL table and .SCHEMA file
must already exist. The applicable .SCHEMA file of the TableDataSet
must match the configuration of the SQL table. The variant data types of the TableDataSet
columns must map to the JDBC types of server table. By default, all rows will have a status of INSERT.
tabledataset1.setTableName(string); tableDataSet1.SetRowID(columnName);
By default, data is loaded from a TextDataFile
with a status of RowStatus.Loaded
. Calling the saveChanges()
method of a QueryDataSet
or a ProcedureDataSet
will not save changes made to a TextDataFile
because these rows are not yet viewed as being inserted. To enable changes to be saved and enable all rows loaded from the TextDataFile
to have an INSERTED status, set the property TextDataFile.setLoadAsInserted(true)
. Now when the saveChanges()
method of a QueryDataSet
or a ProcedureDataSet
is called, the data will be saved back to the data source.
For more information on using the QueryResolver
to save changes to a SQL table, see "Customizing the default resolver logic".