root / trunk / org.gvsig.postgresql / org.gvsig.postgresql.provider / src / test / java / org / gvsig / fmap / dal / store / postgresql / DatabaseMetadataChecks.java @ 5
History | View | Annotate | Download (2.64 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package org.gvsig.fmap.dal.store.postgresql; |
5 |
|
6 |
import java.sql.Connection; |
7 |
import java.sql.DatabaseMetaData; |
8 |
import java.sql.DriverManager; |
9 |
import java.sql.ResultSet; |
10 |
import java.sql.ResultSetMetaData; |
11 |
import java.sql.SQLException; |
12 |
|
13 |
/**
|
14 |
* @author cordin
|
15 |
*
|
16 |
*/
|
17 |
public class DatabaseMetadataChecks { |
18 |
|
19 |
// Columns as specified by the DatabaseMetadata.getTables javadoc
|
20 |
private static final String[] COLUMNS = new String[] { |
21 |
// Metadata resultset column names
|
22 |
"TABLE_CAT", // String => table catalog (may be null) |
23 |
"TABLE_SCHEM", // String => table schema (may be null) |
24 |
"TABLE_NAME", // String => table name |
25 |
"TABLE_TYPE", // String => table type. Typical types are "TABLE", |
26 |
// "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY",
|
27 |
// "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
|
28 |
"REMARKS", // String => explanatory comment on the table |
29 |
"TYPE_CAT", // String => the types catalog (may be null) |
30 |
"TYPE_SCHEM", // String => the types schema (may be null) |
31 |
"TYPE_NAME", // String => type name (may be null) |
32 |
"SELF_REFERENCING_COL_NAME", // String => name of the designated |
33 |
// "identifier" column of a typed
|
34 |
// table (may be null)
|
35 |
"REF_GENERATION", // String => specifies how values in |
36 |
// SELF_REFERENCING_COL_NAME are created. Values
|
37 |
// are "SYSTEM", "USER", "DERIVED". (may be
|
38 |
// null)
|
39 |
}; |
40 |
|
41 |
public void showTables() throws Exception { |
42 |
|
43 |
Connection conn = DriverManager.getConnection( |
44 |
"jdbc:postgresql://localhost/gvsigDB", "gvsig", "gvsig"); |
45 |
|
46 |
try {
|
47 |
// Statement st = conn.createStatement();
|
48 |
// String sql = "select tablename from pg_tables";
|
49 |
// ResultSet result = st.executeQuery(sql);
|
50 |
|
51 |
DatabaseMetaData metadata = conn.getMetaData();
|
52 |
ResultSet result = metadata.getTables(null, null, null, |
53 |
new String[] { "TABLE", "VIEW" }); |
54 |
ResultSetMetaData resultMD = result.getMetaData();
|
55 |
String[] columns = new String[resultMD.getColumnCount()]; |
56 |
for (int i = 0; i < columns.length; i++) { |
57 |
columns[i] = resultMD.getColumnName(i + 1);
|
58 |
} |
59 |
|
60 |
for (int i = 0; i < columns.length; i++) { |
61 |
System.out.print(columns[i]);
|
62 |
System.out.print(";"); |
63 |
} |
64 |
System.out.println();
|
65 |
while (result.next()) {
|
66 |
|
67 |
for (int i = 0; i < columns.length; i++) { |
68 |
System.out.print(result.getString(columns[i]));
|
69 |
System.out.print(";"); |
70 |
} |
71 |
System.out.println();
|
72 |
} |
73 |
|
74 |
} finally {
|
75 |
try {
|
76 |
conn.close(); |
77 |
} catch (SQLException e) { |
78 |
e.printStackTrace(); |
79 |
} |
80 |
} |
81 |
|
82 |
} |
83 |
|
84 |
/**
|
85 |
* @param args
|
86 |
*/
|
87 |
public static void main(String[] args) { |
88 |
try {
|
89 |
new DatabaseMetadataChecks().showTables();
|
90 |
} catch (Exception e) { |
91 |
e.printStackTrace(); |
92 |
System.exit(-1); |
93 |
} |
94 |
System.exit(0); |
95 |
} |
96 |
|
97 |
} |