001package bradleyross.library.database; 002import bradleyross.library.database.DatabaseProperties; 003import bradleyross.library.helpers.GenericPrinter; 004import java.sql.SQLException; 005import java.sql.Connection; 006import java.sql.DatabaseMetaData; 007import java.io.PrintStream; 008import java.io.PrintWriter; 009import java.io.IOException; 010/** 011 * Displays information about database. 012 * @see DatabaseMetaData 013 * @author Bradley Ross 014 * 015 */ 016public class ShowProperties 017{ 018 /** 019 * Object containing information on the database. 020 */ 021 protected DatabaseMetaData meta = null; 022 /** 023 * True indicates that the class was able to 024 * successfully obtain a DatabaseMetaData object. 025 */ 026 protected boolean valid = false; 027 /** 028 * Constructor using java.sql.Connection object. 029 * @param conn Connection object 030 * @throws SQLException if database errors 031 */ 032 public ShowProperties(Connection conn) throws SQLException 033 { 034 try 035 { 036 meta = conn.getMetaData(); 037 } 038 catch (SQLException e) 039 { 040 throw e; 041 } 042 valid = true; 043 } 044 /** 045 * Constructor using my database class. 046 * @param data Object containing database connectivity information. 047 * @throws SQLException if database errors 048 */ 049 public ShowProperties(DatabaseProperties data) throws SQLException 050 { 051 Connection conn; 052 try 053 { 054 conn = data.getConnection(); 055 meta = conn.getMetaData(); 056 } 057 catch (SQLException e) 058 { 059 throw e; 060 } 061 valid = true; 062 } 063 /** 064 * Class returning database meta data object. 065 * @return Database meta data object 066 */ 067 public DatabaseMetaData getMetaData() 068 { 069 return meta; 070 } 071 /** 072 * Sends information on the database to the object. 073 * @param value Object for printing information, 074 * @throws IOException if io errors 075 * @throws SQLException if database errors 076 */ 077 public void printInfo(PrintStream value) throws SQLException, IOException 078 { 079 GenericPrinter printer = new GenericPrinter(value); 080 printInfo(printer); 081 } 082 /** 083 * Sends information on the database to the object. 084 * @param value Object for printing information. 085 * @throws IOException if io errors 086 * @throws SQLException if database errors 087 */ 088 public void printInfo(PrintWriter value) throws SQLException, IOException 089 { 090 GenericPrinter printer = new GenericPrinter(value); 091 printInfo(printer); 092 } 093 /** 094 * Sends information on the database to the object. 095 * @param printer GenericPrinter object to be used for output 096 * @throws IOException if io errors 097 * @throws SQLException if database errors 098 * 099 */ 100 public void printInfo(GenericPrinter printer) throws SQLException, IOException 101 { 102 printer.println("Database is " + meta.getDatabaseProductName() + " " 103 + Integer.toString(meta.getDatabaseMajorVersion()) + "." 104 + Integer.toString(meta.getDatabaseMinorVersion())); 105 printer.println("Handler is " + meta.getDriverName()); 106 printer.println("The term catalog corresponds to " + meta.getCatalogTerm()); 107 printer.println("The term schema corresponds to " + meta.getSchemaTerm()); 108 printer.println("Time/Date functions are " + meta.getTimeDateFunctions()); 109 printer.println("Supports catalogs in data manipulation: " 110 + Boolean.toString(meta.supportsCatalogsInDataManipulation())); 111 printer.println("Supports schemas in data manipulation: " 112 + Boolean.toString(meta.supportsSchemasInDataManipulation())); 113 printer.println("The following characters can be used in unquoted identifier names: " 114 + meta.getExtraNameCharacters()); 115 } 116 /** 117 * Sends information on the database to System.out. 118 * @see System#out 119 * @throws IOException if io errors 120 * @throws SQLException if database errors 121 * 122 */ 123 public void printInfo() throws SQLException, IOException 124 { 125 printInfo(System.out); 126 } 127 128}