KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > dbschema > util > DBSchemaHelper


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.dbschema.util;
23
24
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.DatabaseMetaData JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.DriverManager JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35
36 /**
37  *
38  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
39  */

40 public class DBSchemaHelper
41 {
42    private static final String JavaDoc TABLE_NAME = "TABLE_NAME";
43
44    public static Table getTable(DatabaseMetaData JavaDoc dbMD, String JavaDoc tableName) throws SQLException JavaDoc
45    {
46       ResultSet JavaDoc rs = dbMD.getColumns(null, null, tableName, null);
47       Map JavaDoc columns = new HashMap JavaDoc();
48       while(rs.next())
49       {
50          final Column column = new Column(rs);
51          columns.put(column.getName(), column);
52       }
53       safeClose(rs);
54       return new Table(tableName, columns);
55    }
56
57    public static List JavaDoc getTableNames(DatabaseMetaData JavaDoc dbMD) throws SQLException JavaDoc
58    {
59       ResultSet JavaDoc rs = dbMD.getTables(null, null, null, null);
60       List JavaDoc results = new ArrayList JavaDoc();
61       while(rs.next())
62       {
63          results.add(rs.getString(TABLE_NAME));
64       }
65       safeClose(rs);
66       return results;
67    }
68
69    public static Connection JavaDoc getConnection(String JavaDoc url, String JavaDoc user, String JavaDoc pwd) throws SQLException JavaDoc
70    {
71       return DriverManager.getConnection(url, user, pwd);
72    }
73
74    public static void safeClose(Connection JavaDoc con)
75    {
76       if(con != null)
77          try
78          {
79             con.close();
80          }
81          catch(Exception JavaDoc e){}
82    }
83
84    public static void safeClose(ResultSet JavaDoc rs)
85    {
86       if(rs != null)
87          try
88          {
89             rs.close();
90          }
91          catch(Exception JavaDoc e){}
92    }
93 }
94
Popular Tags