01: package test;
02:
03: import java.sql.Connection;
04: import java.sql.DatabaseMetaData;
05: import java.sql.DriverManager;
06: import java.sql.ResultSet;
07: import java.sql.SQLException;
08: import java.sql.Statement;
09:
10: /*
11: * Copyright (C) 2007 Rob Manning
12: * manningr@users.sourceforge.net
13: *
14: * This library is free software; you can redistribute it and/or
15: * modify it under the terms of the GNU Lesser General Public
16: * License as published by the Free Software Foundation; either
17: * version 2.1 of the License, or (at your option) any later version.
18: *
19: * This library is distributed in the hope that it will be useful,
20: * but WITHOUT ANY WARRANTY; without even the implied warranty of
21: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22: * Lesser General Public License for more details.
23: *
24: * You should have received a copy of the GNU Lesser General Public
25: * License along with this library; if not, write to the Free Software
26: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27: */
28:
29: public class H2IdentifierQuoteTest {
30:
31: private static final String tableName = "foo\"\"bar";
32:
33: private static final String dropTable = "drop table \"" + tableName
34: + "\"";
35: private static final String createTable = "CREATE TABLE "
36: + tableName + " (someid int)";
37:
38: private static void execute(Connection con, String sql,
39: boolean printError) {
40: Statement stmt = null;
41: try {
42: stmt = con.createStatement();
43: System.out.println("Executing sql: " + sql);
44: stmt.execute(sql);
45: } catch (SQLException e) {
46: if (printError) {
47: e.printStackTrace();
48: }
49: } finally {
50: if (stmt != null)
51: try {
52: stmt.close();
53: } catch (SQLException e) {
54: }
55: }
56: }
57:
58: private static void test(Connection con) throws Exception {
59: execute(con, dropTable, false);
60: execute(con, createTable, true);
61:
62: ResultSet rs = null;
63: DatabaseMetaData md = con.getMetaData();
64: rs = md.getTables(null, null, "foo%", new String[] { "TABLE" });
65: while (rs.next()) {
66: String name = rs.getString(3); //TABLENAME
67: System.out.println("name=" + name);
68: }
69: }
70:
71: /**
72: * @param args
73: */
74: public static void main(String[] args) throws Exception {
75: Class.forName("org.h2.Driver");
76: String jdbcUrl = "jdbc:h2:tcp://localhost:9094/DBCOPYDEST";
77: Connection con = DriverManager.getConnection(jdbcUrl, "dbcopy",
78: "password");
79: test(con);
80: }
81:
82: }
|