/******************************************************************************/ /* File: datamanager.beh */ /* */ /* Describes the class that actually implements operations for connecting to */ /* the intended database, executing a specified SQL statement, and finally */ /* returning the result. For an Insert request, it throws an exception in */ /* case of a failure or prints an ok message. For a Query request, it throws */ /* an exception if there is a failure, or returns a vector of vectors */ /* representing the rows and columns. */ /* */ /******************************************************************************/ DataManager { {{ private ConnectionArgs myconnArgs; // java.sql type needed for database processing private Connection connection; public DataManager(ConnectionArgs connArgs) { myconnArgs = connArgs; } // used to extract data out of the specified database public Vector getData(String SQLStmt) throws Exception { Vector rows = new Vector(); // used for the returned data, a vector of vectors initializeConnection(); // getting the data out of the database // java.sql types needed for database processing Statement statement; ResultSet rs; try { statement = connection.createStatement(); rs = statement.executeQuery( SQLStmt ); rows = processResultSet( rs ); statement.close(); } catch ( SQLException sqlex ) {sqlex.printStackTrace();} shutdownConnection(); return rows; } // initializing the connection private void initializeConnection() throws SQLException { try { Class.forName( myconnArgs.driver ); connection = DriverManager.getConnection(myconnArgs.url, myconnArgs.userid, myconnArgs.password ); } catch ( ClassNotFoundException cnfex ) { System.err.println("Failed to load JDBC driver." ); cnfex.printStackTrace(); System.exit( 1 ); // terminate program } catch ( SQLException sqlex ) { System.err.println( "Unable to connect" );sqlex.printStackTrace();} } // shutting down the connection private void shutdownConnection() throws SQLException { try { connection.close(); } catch ( SQLException sqlex ) {System.err.println( "Unable to disconnect" );sqlex.printStackTrace();} } // processing the data returned by the database private Vector processResultSet( ResultSet rs ) throws SQLException { Vector rows = new Vector(); // position to first record boolean moreRecords = rs.next(); // If there are no records, return an empty Vector if ( ! moreRecords ) return (new Vector()); try { // get column heads ResultSetMetaData rsmd = rs.getMetaData(); // get row data do { rows.addElement( getNextRow( rs, rsmd ) ); } while ( rs.next() ); } catch ( SQLException sqlex ) {sqlex.printStackTrace();} return rows; } // get one row at a time as a Vector private Vector getNextRow( ResultSet rs, ResultSetMetaData rsmd ) throws SQLException { Vector currentRow = new Vector(); for ( int i = 1; i <= rsmd.getColumnCount(); ++i ) switch( rsmd.getColumnType( i ) ) { case Types.NULL: currentRow.addElement(null); break; case Types.VARCHAR: currentRow.addElement(rs.getString(i)); break; case Types.INTEGER: currentRow.addElement(new Long(rs.getLong(i))); break; case Types.REAL: currentRow.addElement(new Float(rs.getFloat(i))); break; default: System.out.println( "Type was: " + rsmd.getColumnTypeName( i ) ); } return currentRow; } // used to insert data into the specified database public void putData(String SQLStmt) throws Exception { initializeConnection(); // inserting the data into the database try { Statement statement = connection.createStatement(); int result = statement.executeUpdate( SQLStmt ); if ( result == 1 ) System.out.println( "\nInsertion successful\n" ); else System.out.println( "\nInsertion failed\n" ); statement.close(); } catch ( SQLException sqlex ) { System.out.println("\nInsert Statement caused an error: Check if entry already exists!\n\n"); sqlex.printStackTrace();} shutdownConnection(); return; } }} }