Return-Path: X-Original-To: apmail-db-derby-user-archive@www.apache.org Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CDFFFCCD4 for ; Tue, 22 May 2012 19:33:58 +0000 (UTC) Received: (qmail 99158 invoked by uid 500); 22 May 2012 19:33:58 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 99094 invoked by uid 500); 22 May 2012 19:33:58 -0000 Mailing-List: contact derby-user-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Discussion" Delivered-To: mailing list derby-user@db.apache.org Received: (qmail 99086 invoked by uid 99); 22 May 2012 19:33:58 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 22 May 2012 19:33:58 +0000 X-ASF-Spam-Status: No, hits=-5.0 required=5.0 tests=RCVD_IN_DNSWL_HI,SPF_PASS,UNPARSEABLE_RELAY X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of rick.hillegas@oracle.com designates 148.87.113.117 as permitted sender) Received: from [148.87.113.117] (HELO rcsinet15.oracle.com) (148.87.113.117) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 22 May 2012 19:33:49 +0000 Received: from ucsinet22.oracle.com (ucsinet22.oracle.com [156.151.31.94]) by rcsinet15.oracle.com (Sentrion-MTA-4.2.2/Sentrion-MTA-4.2.2) with ESMTP id q4MJXRS8003973 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 22 May 2012 19:33:28 GMT Received: from acsmt357.oracle.com (acsmt357.oracle.com [141.146.40.157]) by ucsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id q4MJXR44018137 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 22 May 2012 19:33:27 GMT Received: from abhmt102.oracle.com (abhmt102.oracle.com [141.146.116.54]) by acsmt357.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id q4MJXQLm025458 for ; Tue, 22 May 2012 14:33:26 -0500 Received: from dhcp-rmdc-twvpn-1-vpnpool-10-159-69-68.vpn.oracle.com (/10.159.69.68) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 22 May 2012 12:33:26 -0700 Message-ID: <4FBBE9FC.8020306@oracle.com> Date: Tue, 22 May 2012 12:33:16 -0700 From: Rick Hillegas User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.7; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 MIME-Version: 1.0 To: derby-user@db.apache.org Subject: Re: best way to check for the exsitence of a table References: <619F13B2042F204E8E8E93D73870255809CFA57E@EXJSQUSDAG04.ad.jefco.com> In-Reply-To: <619F13B2042F204E8E8E93D73870255809CFA57E@EXJSQUSDAG04.ad.jefco.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Source-IP: ucsinet22.oracle.com [156.151.31.94] X-Virus-Checked: Checked by ClamAV on apache.org Hi Pavel, Querying the metadata as you suggest is a good approach. You will want to adjust the arguments to DatabaseMetaData.getTables() however. The arguments in your example may find false matches in other schemas and with tables created with double-quoted names. The following example may help you adjust the arguments to getTable(): public class z { public static void main( String... args ) throws Exception { Connection conn = DriverManager.getConnection( "jdbc:derby:memory:db;create=true" ); conn.prepareStatement( "create table myTable( a int )" ).execute(); DatabaseMetaData dbmd = conn.getMetaData(); ResultSet rs; rs = dbmd.getTables( null, "APP", "MYTABLE", new String[] { "TABLE" } ); System.out.println( "Table 'MYTABLE' exists = " + rs.next() ); rs = dbmd.getTables( null, "APP", "mytable", new String[] { "TABLE" } ); System.out.println( "Table 'mytable' exists = " + rs.next() ); } } Hope this helps, -Rick On 5/22/12 11:33 AM, Pavel Bortnovskiy wrote: > > Hello: > > When reading derby docs, I noticed that the functionWwdUtils.wwdChk4Tablein Derby demos is implemented by executing an update on the table and then relying on the exception mechanism to determine whether or not the table exists. > > My approach was to execute DatabaseMetaData.getTables method (pls see implementation below) and iterate through the resultset. I also wanted to assure case insensitivity. > > Which method is more preferable from the point of efficiency and database operations (locks, etc). > This code might be executed frequently and by multiple threads. > > Thank you, > Pavel. > > > > public static boolean tableExists(final Connection connection, > final String tableName) throws SQLException { > > try { > > final DatabaseMetaData databaseMetaData = > connection.getMetaData(); > > final ResultSet resultSet = > databaseMetaData.getTables(null, null, null, new String[]{"TABLE"}); > > try { > > while (resultSet.next()) { > > if > (resultSet.getString("TABLE_NAME").equalsIgnoreCase(tableName)) { > > return true; > > } > > } > > } finally { > > connection.commit(); > > resultSet.close(); > > } > > return false; > > } catch (SQLException e) { > > Statement statement = null; > > try { > > final Connection c = > Components.getMemoryDb().getConnection(true); > > statement = c.createStatement(); > > final String select = "SELECT * FROM > SYSCS_DIAG.LOCK_TABLE"; > > final ResultSet resultSet = > statement.executeQuery(select); > > final int cnt = resultSet.getMetaData().getColumnCount(); > > final StringBuilder builder = new > StringBuilder("Results of \"" + select + "\":"); > > while (resultSet.next()) { > > builder.append("\n\t"); > > for (int i = 1; i <= cnt; i++) { > > if (i > 1) { > > builder.append(","); > > } > > builder.append(resultSet.getObject(i)); > > } > > } > > logger.info(builder.toString()); > > } finally { > > if (statement != null) { > > statement.close(); > > } > > } > > throw e; > > } > > } > > > Jefferies archives and monitors outgoing and incoming > e-mail. The contents of this email, including any > attachments, are confidential to the ordinary user of the > email address to which it was addressed. If you are not > the addressee of this email you may not copy, forward, > disclose or otherwise use it or any part of it in any form > whatsoever. This email may be produced at the request of > regulators or in connection with civil litigation. > Jefferies accepts no liability for any errors or omissions > arising as a result of transmission. Use by other than > intended recipients is prohibited. In the United Kingdom, > Jefferies operates as Jefferies International Limited; > registered in England: no. 1978621; registered office: > Vintners Place, 68 Upper Thames Street, London EC4V 3BJ. > Jefferies International Limited is authorised and > regulated by the Financial Services Authority. >