01: /*
02: * Copyright (C) CollabraSpace Inc. All rights reserved.
03: */
04: package adhoc;
05:
06: import java.sql.Connection;
07: import java.sql.DatabaseMetaData;
08: import java.sql.DriverManager;
09: import java.sql.ResultSet;
10: import java.sql.SQLException;
11:
12: public class DB2DatabaseMetaDataTest {
13:
14: String jdbcDriver = "com.ibm.db2.jcc.DB2Driver";
15:
16: String jdbcUrl = "jdbc:db2://localhost:50000/sample";
17:
18: String user = "dbcopy";
19:
20: String pass = "password";
21:
22: Connection con = null;
23:
24: String schema = "DBCOPYDEST";
25: String table = "BIGINT_TYPE_TABLE";
26:
27: int[] bestRowConstants = new int[] {
28: DatabaseMetaData.bestRowNotPseudo,
29: DatabaseMetaData.bestRowPseudo,
30: DatabaseMetaData.bestRowSession,
31: DatabaseMetaData.bestRowTemporary,
32: DatabaseMetaData.bestRowTransaction,
33: DatabaseMetaData.bestRowUnknown, };
34:
35: public DB2DatabaseMetaDataTest() throws Exception {
36: init();
37: }
38:
39: public void init() throws Exception {
40: Class.forName(jdbcDriver);
41: con = DriverManager.getConnection(jdbcUrl, user, pass);
42: }
43:
44: /**
45: * This fails with a Connection failure - java.io.EOFException
46: * @throws SQLException
47: */
48: public void doTest() throws SQLException {
49: for (int i = 0; i < bestRowConstants.length; i++) {
50: getBestRowIdentifier(null, i);
51: getBestRowIdentifier("null", i);
52: getBestRowIdentifier("%", i);
53: getBestRowIdentifier(con.getCatalog(), i);
54: }
55: }
56:
57: private void getBestRowIdentifier(String catalog, int constant)
58: throws SQLException {
59: System.out.println("Testing catalog=" + catalog + " constant="
60: + constant);
61: DatabaseMetaData dmd = con.getMetaData();
62: ResultSet rs = dmd.getBestRowIdentifier(catalog, schema, table,
63: constant, true);
64: boolean results = false;
65: while (rs.next()) {
66: results = true;
67: String columnName = rs.getString("COLUMN_NAME");
68: System.out.println("Column=" + columnName);
69: }
70: if (!results) {
71: System.out.println("Found no columns that identify row");
72: }
73: rs.close();
74: }
75:
76: public void shutdown() throws SQLException {
77: con.close();
78: }
79:
80: /**
81: * @param args
82: */
83: public static void main(String[] args) throws Exception {
84: // TODO Auto-generated method stub
85: DB2DatabaseMetaDataTest test = new DB2DatabaseMetaDataTest();
86:
87: test.doTest();
88:
89: test.shutdown();
90: }
91:
92: }
|