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.
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 grid, 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 = 8859_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. 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.
1,"Apple",11/16/95 2,"B" 3,"C"
[] FILETYPE = VARYING FILEFORMAT = Encoded ENCODING = 8859_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,
Next, 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 = 8859_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 grid in a different pattern, set the displayMask property. To modify data in the grid 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);