1 22 package org.jboss.test.cmp2.dbschema.util; 23 24 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.DatabaseMetaData ; 28 import java.sql.Connection ; 29 import java.sql.DriverManager ; 30 import java.util.List ; 31 import java.util.ArrayList ; 32 import java.util.Map ; 33 import java.util.HashMap ; 34 35 36 40 public class DBSchemaHelper 41 { 42 private static final String TABLE_NAME = "TABLE_NAME"; 43 44 public static Table getTable(DatabaseMetaData dbMD, String tableName) throws SQLException 45 { 46 ResultSet rs = dbMD.getColumns(null, null, tableName, null); 47 Map columns = new HashMap (); 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 getTableNames(DatabaseMetaData dbMD) throws SQLException 58 { 59 ResultSet rs = dbMD.getTables(null, null, null, null); 60 List results = new ArrayList (); 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 getConnection(String url, String user, String pwd) throws SQLException 70 { 71 return DriverManager.getConnection(url, user, pwd); 72 } 73 74 public static void safeClose(Connection con) 75 { 76 if(con != null) 77 try 78 { 79 con.close(); 80 } 81 catch(Exception e){} 82 } 83 84 public static void safeClose(ResultSet rs) 85 { 86 if(rs != null) 87 try 88 { 89 rs.close(); 90 } 91 catch(Exception e){} 92 } 93 } 94 | Popular Tags |