1 | package org.greenstone.accessdb;
|
---|
2 |
|
---|
3 | import java.sql.*;
|
---|
4 | import java.io.OutputStreamWriter;
|
---|
5 |
|
---|
6 | public class MSAccessToCSV extends JDBCBase
|
---|
7 | {
|
---|
8 | /*
|
---|
9 |
|
---|
10 | final static String TABLE_NAME = "WAIKATO_INDEPENDENT";
|
---|
11 |
|
---|
12 | public static void printDatabaseMetadata(Connection connection)
|
---|
13 | {
|
---|
14 | try {
|
---|
15 |
|
---|
16 | System.out.println("----");
|
---|
17 | System.out.println("Database Table Names");
|
---|
18 | System.out.println("----");
|
---|
19 | DatabaseMetaData dbmd = connection.getMetaData();
|
---|
20 | String[] types = {"TABLE"};
|
---|
21 | ResultSet rs = dbmd.getTables(null, null, "%", types);
|
---|
22 | while (rs.next()) {
|
---|
23 | System.out.println(rs.getString("TABLE_NAME"));
|
---|
24 | }
|
---|
25 | System.out.println("----");
|
---|
26 | }
|
---|
27 | catch (SQLException e) {
|
---|
28 | e.printStackTrace();
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | public static void printTable(Connection connection, String table_name)
|
---|
33 | {
|
---|
34 | try {
|
---|
35 | Statement st = connection.createStatement();
|
---|
36 | ResultSet rs = st.executeQuery("SELECT * from " + table_name);
|
---|
37 | ResultSetMetaData rsmd = rs.getMetaData();
|
---|
38 |
|
---|
39 | int columnsNumber = rsmd.getColumnCount();
|
---|
40 |
|
---|
41 | // Iterate through the data in the result set and display it.
|
---|
42 | System.out.println("----");
|
---|
43 | System.out.println("Table: " + table_name);
|
---|
44 | System.out.println("----");
|
---|
45 |
|
---|
46 | for(int i = 1; i <= columnsNumber; i++) {
|
---|
47 | System.out.print(rsmd.getColumnLabel(i)+ "|");
|
---|
48 | }
|
---|
49 | System.out.println();
|
---|
50 |
|
---|
51 | while (rs.next()) {
|
---|
52 | for(int i = 1 ; i <= columnsNumber; i++){
|
---|
53 | System.out.print(rs.getString(i));
|
---|
54 | if (i != columnsNumber) { System.out.print(","); }
|
---|
55 | }
|
---|
56 |
|
---|
57 | System.out.println();
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | System.out.println("----");
|
---|
62 |
|
---|
63 | }
|
---|
64 | catch (Exception e) {
|
---|
65 | e.printStackTrace();
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
70 | */
|
---|
71 |
|
---|
72 | public static void msAccessTableToCSVFile(Connection connection, String table_name, String csv_filename)
|
---|
73 | {
|
---|
74 | try {
|
---|
75 | Statement st = connection.createStatement();
|
---|
76 | ResultSet rs = st.executeQuery("SELECT * from " + table_name);
|
---|
77 | ResultSetMetaData rsmd = rs.getMetaData();
|
---|
78 |
|
---|
79 | int columnsNumber = rsmd.getColumnCount();
|
---|
80 |
|
---|
81 | System.out.println("Converting MS Access Database Table '" + table_name + "' to CSV file: " + csv_filename);
|
---|
82 |
|
---|
83 | OutputStreamWriter csv_osw = UtilIO.openTextFile(csv_filename);
|
---|
84 |
|
---|
85 | if (csv_osw != null) {
|
---|
86 |
|
---|
87 | for (int i=1; i<=columnsNumber; i++) {
|
---|
88 | String heading = rsmd.getColumnLabel(i);
|
---|
89 | String heading_safe = heading;
|
---|
90 |
|
---|
91 | if (heading_safe != null) {
|
---|
92 | UtilIO.appendTextToFile(csv_osw,heading_safe);
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (i < columnsNumber) { UtilIO.appendTextToFile(csv_osw,","); }
|
---|
96 |
|
---|
97 | }
|
---|
98 | UtilIO.appendTextToFile(csv_osw,"\n");
|
---|
99 |
|
---|
100 |
|
---|
101 | int line_count = 0;
|
---|
102 |
|
---|
103 | while (rs.next()) {
|
---|
104 | for (int i=1 ; i<=columnsNumber; i++){
|
---|
105 | String val = rs.getString(i);
|
---|
106 |
|
---|
107 | if (val != null) {
|
---|
108 |
|
---|
109 | int val_len = val.length();
|
---|
110 |
|
---|
111 | //if ((val_len>=2) && ((val.charAt(0) == '"') && (val.charAt(val_len-1) == '"'))) {
|
---|
112 | //val = val.substring(1,val_len-2);
|
---|
113 | //}
|
---|
114 |
|
---|
115 | String val_safe = val.replaceAll(",","\\\\,").replaceAll("\"","\\\\\"");
|
---|
116 |
|
---|
117 | UtilIO.appendTextToFile(csv_osw,'"'+val_safe+'"');
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (i < columnsNumber) { UtilIO.appendTextToFile(csv_osw,","); }
|
---|
121 | }
|
---|
122 |
|
---|
123 | UtilIO.appendTextToFile(csv_osw,"\n");
|
---|
124 |
|
---|
125 | line_count++;
|
---|
126 | if ((line_count % 1000) == 0) {
|
---|
127 | System.out.println("Processed lines " + (line_count - 999) + "-" + line_count);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | UtilIO.closeTextFile(csv_osw);
|
---|
133 |
|
---|
134 | }
|
---|
135 | }
|
---|
136 | catch (Exception e) {
|
---|
137 | e.printStackTrace();
|
---|
138 | }
|
---|
139 |
|
---|
140 | }
|
---|
141 |
|
---|
142 | public static void main( String[] args )
|
---|
143 | {
|
---|
144 |
|
---|
145 | /* Note:
|
---|
146 |
|
---|
147 | If needing to load via a absolute filename on Windows, example for this is:
|
---|
148 | "jdbc:ucanaccess://d:/cygwin64/home/<USERNAME>/.../waikato-independent-db/Waikato Independent.accdb"
|
---|
149 | */
|
---|
150 |
|
---|
151 | // Assuming database is relative to directory where script is run from:
|
---|
152 | String inputDatabaseURI = "jdbc:ucanaccess://Waikato Independent.accdb";
|
---|
153 |
|
---|
154 | System.out.println("Opening: '" + inputDatabaseURI + "' ...");
|
---|
155 |
|
---|
156 | try (Connection connection = DriverManager.getConnection(inputDatabaseURI)) {
|
---|
157 | System.out.println("... connnected");
|
---|
158 |
|
---|
159 | printDatabaseMetadata(connection);
|
---|
160 |
|
---|
161 | System.out.println();
|
---|
162 |
|
---|
163 | //printTable(connection, TABLE_NAME);
|
---|
164 |
|
---|
165 | String csv_filename = TABLE_NAME + ".csv";
|
---|
166 |
|
---|
167 | msAccessTableToCSVFile(connection, TABLE_NAME, csv_filename);
|
---|
168 |
|
---|
169 |
|
---|
170 | /*
|
---|
171 | String sql = "INSERT INTO Contacts (Full_Name, Email, Phone) VALUES (?, ?, ?)";
|
---|
172 |
|
---|
173 | PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
---|
174 | preparedStatement.setString(1, "Jim Rohn");
|
---|
175 | preparedStatement.setString(2, "[email protected]");
|
---|
176 | preparedStatement.setString(3, "0919989998");
|
---|
177 |
|
---|
178 | int row = preparedStatement.executeUpdate();
|
---|
179 |
|
---|
180 | if (row > 0) {
|
---|
181 | System.out.println("A row has been inserted successfully.");
|
---|
182 | }
|
---|
183 |
|
---|
184 | sql = "SELECT * FROM Contacts";
|
---|
185 |
|
---|
186 | Statement statement = connection.createStatement();
|
---|
187 | ResultSet result = statement.executeQuery(sql);
|
---|
188 |
|
---|
189 | while (result.next()) {
|
---|
190 | int id = result.getInt("Contact_ID");
|
---|
191 | String fullname = result.getString("Full_Name");
|
---|
192 | String email = result.getString("Email");
|
---|
193 | String phone = result.getString("Phone");
|
---|
194 |
|
---|
195 | System.out.println(id + ", " + fullname + ", " + email + ", " + phone);
|
---|
196 | }
|
---|
197 | */
|
---|
198 |
|
---|
199 | } catch (SQLException ex) {
|
---|
200 | ex.printStackTrace();
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|