[34500] | 1 | package org.greenstone.accessdb;
|
---|
[34502] | 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
[34500] | 4 | import java.sql.*;
|
---|
| 5 |
|
---|
| 6 | public class JDBCBase
|
---|
| 7 | {
|
---|
[34502] | 8 | // Name of the table inside the MS Access DB
|
---|
[34500] | 9 | final static String TABLE_NAME = "WAIKATO_INDEPENDENT";
|
---|
[34502] | 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 | }
|
---|
[34500] | 41 |
|
---|
[34502] | 42 |
|
---|
[34500] | 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);
|
---|
[34502] | 68 | ResultSetMetaData rs_md = rs.getMetaData();
|
---|
[34500] | 69 |
|
---|
[34502] | 70 | int columnCount = rs_md.getColumnCount();
|
---|
[34500] | 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 |
|
---|
[34502] | 77 | for(int i = 1; i <= columnCount; i++) {
|
---|
| 78 | System.out.print(rs_md.getColumnLabel(i)+ "|");
|
---|
[34500] | 79 | }
|
---|
| 80 | System.out.println();
|
---|
| 81 |
|
---|
| 82 | while (rs.next()) {
|
---|
[34502] | 83 | for(int i = 1 ; i <= columnCount; i++){
|
---|
[34500] | 84 | System.out.print(rs.getString(i));
|
---|
[34502] | 85 | if (i != columnCount) { System.out.print(","); }
|
---|
[34500] | 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 |
|
---|