KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > load > Export


1 /*
2
3    Derby - Class org.apache.derby.impl.load.Export
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.load;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.*;
29
30 /**
31  * This class provides ways to export data from
32  * a table or a view into a file. Export functions provided
33  * in this class are called through Systement Procedures.
34  */

35 public class Export extends ExportAbstract{
36
37     private String JavaDoc outputFileName;
38
39
40     private void doExport() throws SQLException JavaDoc
41     {
42         try {
43             if (entityName == null && selectStatement == null)
44                 throw LoadError.entityNameMissing();
45             
46             if (outputFileName == null)
47                 throw LoadError.dataFileNull();
48             try {
49                 doAllTheWork();
50             } catch (IOException JavaDoc iex) {
51                 //in case of ioexception, catch it and throw it as our own exception
52
throw LoadError.errorWritingData();
53             }
54         } catch (Exception JavaDoc ex) {
55             throw LoadError.unexpectedError(ex);
56         }
57
58     }
59     
60     private Export(Connection JavaDoc con, String JavaDoc schemaName ,
61                        String JavaDoc tableName, String JavaDoc selectStatement ,
62                        String JavaDoc outputFileName, String JavaDoc characterDelimeter,
63                        String JavaDoc columnDelimeter, String JavaDoc codeset)
64         throws SQLException JavaDoc{
65         this.con = con;
66         this.schemaName = schemaName;
67         this.entityName = tableName;
68         this.selectStatement = selectStatement;
69         this.outputFileName = outputFileName;
70         try{
71             controlFileReader = new ControlInfo();
72             controlFileReader.setControlProperties(characterDelimeter,
73                                                    columnDelimeter, codeset);
74         }catch(Exception JavaDoc ex)
75         {
76             throw LoadError.unexpectedError(ex);
77         }
78     }
79
80     /**
81      * SYSCS_EXPORT_TABLE system Procedure from ij or from a Java application
82      * invokes this method to perform export of a table data to a file.
83      * @param con The Cloudscape database connection URL for the database containing the table
84      * @param schemaName schema name of the table data is being exported from
85      * @param tableName Name of the Table from which data has to be exported.
86      * @param outputFileName Name of the file to which data has to be exported.
87      * @param columnDelimeter Delimiter that seperates columns in the output file
88      * @param characterDelimeter Delimiter that is used to quoate non-numeric types
89      * @param codeset Codeset that should be used to write the data to the file
90      * @exception SQL Exception on errors
91      */

92
93     public static void exportTable(Connection JavaDoc con, String JavaDoc schemaName,
94                               String JavaDoc tableName, String JavaDoc outputFileName,
95                               String JavaDoc columnDelimeter, String JavaDoc characterDelimeter,
96                               String JavaDoc codeset)
97         throws SQLException JavaDoc {
98         
99         Export fex = new Export(con, schemaName, tableName, null,
100                                         outputFileName, characterDelimeter,
101                                         columnDelimeter, codeset);
102
103         fex.doExport();
104     }
105
106     
107     /**
108      * SYSCS_EXPORT_QUERY system Procedure from ij or from a Java application
109      * invokes this method to perform export of the data retrieved by select statement to a file.
110      * @param con The Cloudscape database connection URL for the database containing the table
111      * @param selectStatement select query that is used to export the data
112      * @param outputFileName Name of the file to which data has to be exported.
113      * @param columnDelimeter Delimiter that seperates columns in the output file
114      * @param characterDelimeter Delimiter that is used to quiote non-numeric types
115      * @param codeset Codeset that should be used to write the data to the file
116      * @exception SQL Exception on errors
117      */

118     public static void exportQuery(Connection JavaDoc con, String JavaDoc selectStatement,
119                               String JavaDoc outputFileName, String JavaDoc columnDelimeter,
120                               String JavaDoc characterDelimeter, String JavaDoc codeset)
121         throws SQLException JavaDoc {
122         
123         Export fex = new Export(con, null, null, selectStatement,
124                                         outputFileName,characterDelimeter,
125                                         columnDelimeter, codeset);
126         fex.doExport();
127     }
128
129
130     /**
131      * For internal use only
132      * @exception Exception if there is an error
133      */

134     //returns the control file reader corresponding to the control file passed
135
protected ExportWriteDataAbstract getExportWriteData() throws Exception JavaDoc {
136         return new ExportWriteData(outputFileName, controlFileReader);
137     }
138 }
139
140
141
142
143
144
145
Popular Tags