1 | package org.greenstone.accessdb;
|
---|
2 |
|
---|
3 | import java.sql.*;
|
---|
4 |
|
---|
5 | public class JDBCBase
|
---|
6 | {
|
---|
7 |
|
---|
8 | final static String TABLE_NAME = "WAIKATO_INDEPENDENT";
|
---|
9 |
|
---|
10 | public static void printDatabaseMetadata(Connection connection)
|
---|
11 | {
|
---|
12 | try {
|
---|
13 |
|
---|
14 | System.out.println("----");
|
---|
15 | System.out.println("Database Table Names");
|
---|
16 | System.out.println("----");
|
---|
17 | DatabaseMetaData dbmd = connection.getMetaData();
|
---|
18 | String[] types = {"TABLE"};
|
---|
19 | ResultSet rs = dbmd.getTables(null, null, "%", types);
|
---|
20 | while (rs.next()) {
|
---|
21 | System.out.println(rs.getString("TABLE_NAME"));
|
---|
22 | }
|
---|
23 | System.out.println("----");
|
---|
24 | }
|
---|
25 | catch (SQLException e) {
|
---|
26 | e.printStackTrace();
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | public static void printTable(Connection connection, String table_name)
|
---|
31 | {
|
---|
32 | try {
|
---|
33 | Statement st = connection.createStatement();
|
---|
34 | ResultSet rs = st.executeQuery("SELECT * from " + table_name);
|
---|
35 | ResultSetMetaData rsmd = rs.getMetaData();
|
---|
36 |
|
---|
37 | int columnsNumber = rsmd.getColumnCount();
|
---|
38 |
|
---|
39 | // Iterate through the data in the result set and display it.
|
---|
40 | System.out.println("----");
|
---|
41 | System.out.println("Table: " + table_name);
|
---|
42 | System.out.println("----");
|
---|
43 |
|
---|
44 | for(int i = 1; i <= columnsNumber; i++) {
|
---|
45 | System.out.print(rsmd.getColumnLabel(i)+ "|");
|
---|
46 | }
|
---|
47 | System.out.println();
|
---|
48 |
|
---|
49 | while (rs.next()) {
|
---|
50 | for(int i = 1 ; i <= columnsNumber; i++){
|
---|
51 | System.out.print(rs.getString(i));
|
---|
52 | if (i != columnsNumber) { System.out.print(","); }
|
---|
53 | }
|
---|
54 |
|
---|
55 | System.out.println();
|
---|
56 |
|
---|
57 | }
|
---|
58 |
|
---|
59 | System.out.println("----");
|
---|
60 |
|
---|
61 | }
|
---|
62 | catch (Exception e) {
|
---|
63 | e.printStackTrace();
|
---|
64 | }
|
---|
65 |
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|