//Author: ^-^Veerle^-^ import java.sql.Connection; import java.sql.ResultSet; import java.sql.DatabaseMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector; public class databaseListTables { private Vector vecTables = new Vector(); private Connection databaseConnection; public databaseListTables (Connection databaseConnection) throws SQLException { this.databaseConnection = databaseConnection; setupList(); } public void setupList () throws SQLException { ResultSet rsList; DatabaseMetaData dmd = databaseConnection.getMetaData(); rsList = dmd.getTables(null, null, "%", null); while (rsList.next()) { if (rsList.getString(4).equalsIgnoreCase("TABLE")) { vecTables.addElement(rsList.getString(3)); } } } public void addTable (String strTableName) throws Exception { try { Statement s = databaseConnection.createStatement(); s.execute("create table " + strTableName + " (Title varchar(40), Year integer)"); s.close(); vecTables.addElement(strTableName); } catch (SQLException ex) { throw new Exception("Error creating " + strTableName); } } public void databaseDeleteTable (String strTableName) throws Exception { try { Statement s = databaseConnection.createStatement(); s.execute("drop table " + strTableName); s.close(); vecTables.removeElement(strTableName); } catch (SQLException ex) { throw new Exception("Error deleting " + strTableName); } } public String[] getList () { String [] strTemp = new String[1]; strTemp[0] = "---"; if (vecTables.size() == 0) { return strTemp; } String[] strList = new String[vecTables.size()]; for (int i = 0; i < vecTables.size(); i++) { strList[i] = (String)vecTables.elementAt(i); } return strList; } public ResultSet getRecords (String strTableName) throws Exception { try { Statement s = databaseConnection.createStatement(); s.execute("select * from " + strTableName); return s.getResultSet(); } catch (SQLException ex) { throw new Exception("Error reading from " + strTableName); } } }