source: main/trunk/model-sites-dev/cambridge-museum/collect/waikato-independent/pre-import/MSAccessToCSV/src/main/java/org/greenstone/accessdb/MSAccessToCSV.java@ 34501

Last change on this file since 34501 was 34501, checked in by davidb, 4 years ago

Code tidy up

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1package org.greenstone.accessdb;
2
3import java.sql.*;
4import java.io.OutputStreamWriter;
5
6public class MSAccessToCSV extends JDBCBase
7{
8
9 public static void msAccessTableToCSVFile(Connection connection, String table_name, String csv_filename)
10 {
11 try {
12 Statement st = connection.createStatement();
13 ResultSet rs = st.executeQuery("SELECT * from " + table_name);
14 ResultSetMetaData rsmd = rs.getMetaData();
15
16 int columnsNumber = rsmd.getColumnCount();
17
18 System.out.println("Converting MS Access Database Table '" + table_name + "' to CSV file: " + csv_filename);
19
20 OutputStreamWriter csv_osw = UtilIO.openTextFile(csv_filename);
21
22 if (csv_osw != null) {
23
24 for (int i=1; i<=columnsNumber; i++) {
25 String heading = rsmd.getColumnLabel(i);
26 String heading_safe = heading;
27
28 if (heading_safe != null) {
29 UtilIO.appendTextToFile(csv_osw,heading_safe);
30 }
31
32 if (i < columnsNumber) { UtilIO.appendTextToFile(csv_osw,","); }
33
34 }
35 UtilIO.appendTextToFile(csv_osw,"\n");
36
37
38 int line_count = 0;
39
40 while (rs.next()) {
41 for (int i=1 ; i<=columnsNumber; i++){
42 String val = rs.getString(i);
43
44 if (val != null) {
45
46 int val_len = val.length();
47
48 //if ((val_len>=2) && ((val.charAt(0) == '"') && (val.charAt(val_len-1) == '"'))) {
49 //val = val.substring(1,val_len-2);
50 //}
51
52 String val_safe = val.replaceAll(",","\\\\,").replaceAll("\"","\\\\\"");
53
54 UtilIO.appendTextToFile(csv_osw,'"'+val_safe+'"');
55 }
56
57 if (i < columnsNumber) { UtilIO.appendTextToFile(csv_osw,","); }
58 }
59
60 UtilIO.appendTextToFile(csv_osw,"\n");
61
62 line_count++;
63 if ((line_count % 1000) == 0) {
64 System.out.println("Processed lines " + (line_count - 999) + "-" + line_count);
65 }
66 }
67
68
69 UtilIO.closeTextFile(csv_osw);
70
71 }
72 }
73 catch (Exception e) {
74 e.printStackTrace();
75 }
76
77 }
78
79 public static void main( String[] args )
80 {
81
82 /* Note:
83
84 If needing to load via a absolute filename on Windows, example for this is:
85 "jdbc:ucanaccess://d:/cygwin64/home/<USERNAME>/.../waikato-independent-db/Waikato Independent.accdb"
86 */
87
88 // Assuming database is relative to directory where script is run from:
89 String inputDatabaseURI = "jdbc:ucanaccess://Waikato Independent.accdb";
90
91 System.out.println("Opening: '" + inputDatabaseURI + "' ...");
92
93 try (Connection connection = DriverManager.getConnection(inputDatabaseURI)) {
94 System.out.println("... connnected");
95
96 printDatabaseMetadata(connection);
97
98 System.out.println();
99
100 //printTable(connection, TABLE_NAME);
101
102 String csv_filename = TABLE_NAME + ".csv";
103
104 msAccessTableToCSVFile(connection, TABLE_NAME, csv_filename);
105
106 }
107 catch (SQLException ex) {
108 ex.printStackTrace();
109 }
110 }
111}
112
Note: See TracBrowser for help on using the repository browser.