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

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

Changes made as a result of the first test run of the MSAccessTOSQLite main method

File size: 2.7 KB
Line 
1package org.greenstone.accessdb;
2
3import java.util.ArrayList;
4import java.sql.*;
5
6public class JDBCBase
7{
8 // Name of the table inside the MS Access DB
9 final static String TABLE_NAME = "WAIKATO_INDEPENDENT";
10
11 public static ArrayList<String> getColumnLabels(Connection connection, String table_name)
12 {
13 // Read in column labels into ArrayList
14 ArrayList<String> column_labels = null;
15
16 try {
17 Statement st = connection.createStatement();
18 ResultSet rs = st.executeQuery("SELECT * from " + table_name);
19
20 ResultSetMetaData rs_md = rs.getMetaData();
21
22 int columnCount = rs_md.getColumnCount();
23
24
25 column_labels = new ArrayList<String>(columnCount);
26
27 for (int i=1; i<=columnCount; i++) {
28 String heading = rs_md.getColumnLabel(i);
29 column_labels.add(heading); // add even if null, so can keep count
30 if (heading == null) {
31 System.err.println("*** Encountered null column label at column postion: " + i);
32 }
33 }
34 }
35 catch (Exception e) {
36 e.printStackTrace();
37 }
38
39 return column_labels;
40 }
41
42
43 public static void printDatabaseMetadata(Connection connection)
44 {
45 try {
46
47 System.out.println("----");
48 System.out.println("Database Table Names");
49 System.out.println("----");
50 DatabaseMetaData dbmd = connection.getMetaData();
51 String[] types = {"TABLE"};
52 ResultSet rs = dbmd.getTables(null, null, "%", types);
53 while (rs.next()) {
54 System.out.println(rs.getString("TABLE_NAME"));
55 }
56 System.out.println("----");
57 }
58 catch (SQLException e) {
59 e.printStackTrace();
60 }
61 }
62
63 public static void printTable(Connection connection, String table_name)
64 {
65 try {
66 Statement st = connection.createStatement();
67 ResultSet rs = st.executeQuery("SELECT * from " + table_name);
68 ResultSetMetaData rs_md = rs.getMetaData();
69
70 int columnCount = rs_md.getColumnCount();
71
72 // Iterate through the data in the result set and display it.
73 System.out.println("----");
74 System.out.println("Table: " + table_name);
75 System.out.println("----");
76
77 for(int i = 1; i <= columnCount; i++) {
78 System.out.print(rs_md.getColumnLabel(i)+ "|");
79 }
80 System.out.println();
81
82 while (rs.next()) {
83 for(int i = 1 ; i <= columnCount; i++){
84 System.out.print(rs.getString(i));
85 if (i != columnCount) { System.out.print(","); }
86 }
87
88 System.out.println();
89
90 }
91
92 System.out.println("----");
93
94 }
95 catch (Exception e) {
96 e.printStackTrace();
97 }
98
99 }
100
101}
102
Note: See TracBrowser for help on using the repository browser.