In JBuilder, a TableDataSet component is used to store data imported from a text file. Once the data is provided to the data set, it can be viewed and modified. To save changes back to the text file, export the data back to the text file.
To import data from a text file, use the TextDataFile component to provide the location of the text file and parameters specific to its structure. Use a StorageDataSet, such as a TableDataSet component, to store the data locally for viewing and editing. Create Column objects so the TableDataSet knows the type of data and the name of the field for each column of data.
Columns of a TableDataSet are defined by adding columns in the Source window, the UI designer, or by loading a text file with a valid .SCHEMA file. This topic discusses the first two options. Importing data using an existing .SCHEMA file is discussed in "Tutorial: creating a database application from a text file". Your text file has a valid .SCHEMA file only if it has previously been exported by JBuilder.
This tutorial shows how to provide data to an application using a TableDataSet component and a comma-delimited text data file. This type of file can be exported from most desktop databases.
For this example, create a text file to import as follows:
1,"A" 2,"B" 3,"C"
To read the data from this text file and create a database application in JBuilder,
Property name | Value |
delimiter | " (double quote) |
separator | , (comma) |
fileName | <path to>ImportTest.txt |
A delimiter in a text file is a character that is used to define the beginning and end of a string field. By default, the delimiter for string data types is a double quotation mark. For this tutorial, no changes are needed.
A separator in a text file is a character that is used for differentiating between column values. By default, the separator character is a tab (/t). For this example, the separator is a comma (,). When using other text files, modify these properties accordingly.
Specify the complete path and file name for the fileName field.
Click the expand glif to the left of the TableDataSet component to expose existing columns. In this case, there are no existing columns, so select <new column> and set the following properties in the Inspector for the first column:
To make the data available to your application,
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.
When you run this application, the data in the text file is loaded into a TableDataSet and displayed in the visual table component to which it is bound. You can now view, edit, add, and delete data from the data set. A TableDataSet component can be used as either a master or a detail table in a master-detail relationship. To save changes back to the text file, you must export the data back.
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.
Warning: Remove filter criteria prior to saving, if you want to save all of the data back to the original 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.
This part of the 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.
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); }
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:
1,"Apple" 2,"B" 3,"C"
JBuilder automatically creates a .SCHEMA file to define the contents of the text file.
[] 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,
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. These steps demonstrate creating an exportDisplayMask for a new column of type DATE.
1,"Apple",11/16/95 2,"B" 3,"C"
[] 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,
The next steps show what happens when you change the date pattern, edit the data, and save the changes again.
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.
1,"Apple",11-16-1995 2,"B" 3,"C"
[] 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
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.