Changeset 16883 for greenstone3


Ignore:
Timestamp:
2008-08-18T16:20:25+12:00 (16 years ago)
Author:
xiao
Message:

add changes to work with the ActivityManagement.java

Location:
greenstone3/trunk/src/java/org/greenstone/gsdl3/sql
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/DatabaseFactory.java

    r16652 r16883  
    3636        return server;
    3737    } catch (Exception e) {
    38         logger.info("Couldn't load the database server "+ fullName);
     38        logger.debug("Couldn't load the database server "+ fullName);
    3939    }
    4040   
     
    5757       catch (Exception e2) {
    5858           // failed again, give up
    59            logger.info("Couldn't load the sql statement  "+ sqlstate);
     59           logger.debug("Couldn't load the sql statement  "+ sqlstate);
    6060       }
    6161       }
     
    9797            catch (Exception e2) {
    9898            // failed again, give up
    99             logger.info("Couldn't load the sql statement  "+ sqlstate);
     99            logger.debug("Couldn't load the sql statement  "+ sqlstate);
    100100            }
    101101        }
    102102        }
    103103    } catch (Exception e) {
    104         logger.info("Couldn't load the database wrapper "+ fullName);
     104        logger.debug("Couldn't load the database wrapper "+ fullName);
    105105    }
    106106   
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/MetadataDBWrapper.java

    r15081 r16883  
    22
    33import java.util.ArrayList;
     4
     5import java.sql.ResultSet;
     6import java.sql.SQLException;
     7
    48import org.greenstone.gsdl3.util.DBInfo;
    59import org.greenstone.gsdl3.sql.SQLServer;
     
    1519    public boolean openConnection(String databasepath);
    1620
     21    public boolean openAndCreateConnection(String databasepath);
     22
    1723     //return a list of rows
    1824    public ArrayList executeQuery(String query_statement);
     25
     26    public ResultSet queryResultSet(String query_statement);
     27
     28    public boolean execute(String stat);
     29
     30    public boolean executeUpdate(String stat);
     31
     32    public void check4Table(String stat) throws SQLException;
    1933
    2034    public void closeConnection(String databasepath);
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/derby/DerbyDBWrapper.java

    r16630 r16883  
    1414import java.util.HashMap;
    1515import java.util.Iterator;
     16
    1617
    1718import org.greenstone.gsdl3.util.DBInfo;
     
    6667    }
    6768    catch(SQLException sqle){
    68         logger.error("Database Error occured when creating a statement object",sqle);
     69        logger.debug("Database Error occured when creating a statement object",sqle);
    6970        return false;
    7071    }
     
    7273    }
    7374
    74     //return a list of rows array->hashmap
     75    public boolean openAndCreateConnection(String databasepath){
     76        connection = sqlServer.connectAndCreate(databasepath);
     77
     78        if (connection == null) {
     79            logger.error("sql connection is null. database path="+databasepath);
     80            return false;
     81        }
     82
     83        try{
     84            connection.setAutoCommit(true);
     85            //by passing the two arguments, the ResultSet returned from executeQuery is updatable
     86            statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
     87            if (statement == null) {
     88                logger.error("statement is null");
     89                return false;
     90            }
     91
     92        }
     93        catch(SQLException sqle){
     94            logger.debug("Database Error occured when creating a statement object",sqle);
     95            return false;
     96        }
     97        return true;
     98    }
     99
    75100    public synchronized ArrayList executeQuery(String query_statement){
    76101    //the database hasn't been correct yet
     
    89114           HashMap arow = new HashMap();
    90115           for(int i = 1; i <= numOfColumns ; i++){
    91            arow.put(rsmd.getColumnName(i).toLowerCase(), rs.getObject(i));
     116               arow.put(rsmd.getColumnName(i).toLowerCase(), rs.getObject(i));
    92117           }
    93118           results.add(arow); 
     
    95120    }
    96121    catch(SQLException sqle){
    97         logger.error("Database Error occured when executeQuery " + query_statement,sqle);
     122        logger.debug("Database Error occured when executeQuery " + query_statement,sqle);
    98123        return results;
    99124    }
     125    catch(Exception e){         
     126        logger.debug(e);
     127        return null;
     128    }
    100129    return results;
    101130    }
    102    
    103      /** check for the existence of a table**/
    104     public boolean check4Table (String check_table) {
    105          try {
    106       statement.execute(check_table);
    107       }  catch (SQLException sqle) {
    108       String theError = (sqle).getSQLState();
    109       if (theError.equals("42X05"))   // Table does not exist
    110           {
    111           return false;
    112           }   
    113       else{
    114           return true;
    115       }
    116       }
    117       //  System.out.println("Just got the warning - table exists OK ");
    118       return true;
    119     }   
     131    /**
     132     * Same as executeQuery except the return type is ResultSet
     133     * @param stat
     134     * @return ResultSet of querying the statement 'stat'
     135     */
     136    public synchronized ResultSet queryResultSet(String stat){
     137        ResultSet rs = null;
     138        try{           
     139            if (statement == null){
     140                logger.info("Null sql statement provided.");
     141                return null;
     142            }
     143            logger.debug(stat);
     144            rs = statement.executeQuery(stat);
     145           
     146            logger.info("sql stat="+stat+ " result="+rs);
     147        }
     148        catch(SQLException sqle){
     149            logger.info("Database Error occured when execute query " + stat, sqle);
     150            return null;
     151        }
     152        catch(Exception e){         
     153            logger.info("Exception="+e);
     154            return null;
     155        }
     156        logger.debug(" result="+rs);
     157        return rs;
     158    }
     159    /**
     160     * Used by create (table)
     161     */
     162    public synchronized boolean execute(String stat) {
     163        boolean rs;
     164        try{           
     165            if (statement == null){
     166                logger.info("statement is null.");
     167                return false;
     168            }
     169
     170            rs = statement.execute(stat);
     171//          connection.commit();
     172        }
     173        catch(SQLException sqle){           
     174            logger.debug("Database Error occured when execute query " + stat, sqle);
     175            return false;
     176        }
     177        catch(Exception e){         
     178            logger.debug(e);
     179            return false;
     180        }
     181        return rs;
     182    }
     183    /**
     184     * Used by insert, update, and delete
     185     */
     186    public synchronized boolean executeUpdate(String stat) {
     187        int rs;
     188        try{           
     189            if (statement == null){
     190                logger.info("statement is null.");
     191                return false;
     192            }
     193           
     194            //either the row count of INSERT, UPDATE OR DELETE statement, or 0 if the statement returns nothing
     195            rs = statement.executeUpdate(stat);
     196//          connection.commit();
     197            logger.info("sql stat="+stat+ " result="+rs);
     198        }
     199        catch(SQLException sqle){           
     200            logger.info("Database Error occured when execute query " + stat, sqle);
     201            return false;
     202        }
     203        catch(Exception e){         
     204            logger.info("Exception="+e);
     205            return false;
     206        }
     207        logger.info(" result="+rs);
     208        return (rs==-1)? false : true;
     209    }
     210    /**
     211     * Used by checking the existence of table
     212     * Same as the method 'execute' except throws SQLException
     213     */
     214    public void check4Table(String stat) throws SQLException {
     215        statement.executeQuery(stat);
     216    }
    120217
    121218    public void closeConnection(String databasepath){
Note: See TracChangeset for help on using the changeset viewer.