Clover coverage report - baseCode - 0.2.5
Coverage timestamp: Tue Apr 12 2005 11:31:58 EDT
file stats: LOC: 243   Methods: 16
NCLOC: 123   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
Handle.java 0% 0% 0% 0%
coverage
 1   
 package baseCode.db;
 2   
 
 3   
 import java.sql.Connection;
 4   
 import java.sql.DriverManager;
 5   
 import java.sql.PreparedStatement;
 6   
 import java.sql.ResultSet;
 7   
 import java.sql.SQLException;
 8   
 import java.sql.Statement;
 9   
 import java.util.HashMap;
 10   
 import java.util.HashSet;
 11   
 import java.util.List;
 12   
 import java.util.Map;
 13   
 import java.util.Set;
 14   
 import java.util.Vector;
 15   
 
 16   
 /**
 17   
  * Database handle container and query convenience functions.
 18   
  * </p>
 19   
  * <p>
 20   
  * Copyright (c) 2004
 21   
  * </p>
 22   
  * <p>
 23   
  * Institution: Columbia University
 24   
  * </p>
 25   
  * 
 26   
  * @author Paul Pavlidis
 27   
  * @version $Id: Handle.java,v 1.6 2005/01/12 19:10:46 pavlidis Exp $
 28   
  */
 29   
 
 30   
 public class Handle {
 31   
 
 32   
    private Connection con;
 33   
 
 34   
    //  private Statement stat;
 35   
    /**
 36   
     * @param host String
 37   
     * @param database String
 38   
     * @param user String
 39   
     * @param password String
 40   
     * @throws SQLException
 41   
     */
 42  0
    public Handle( String host, String database, String user, String password ) throws SQLException {
 43  0
       try {
 44  0
          Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
 45  0
          String url = "jdbc:mysql://" + host + "/" + database + "?relaxAutoCommit=true";
 46  0
          con = DriverManager.getConnection( url, user, password );
 47   
          //       stat = newStatement();
 48   
       } catch ( ClassNotFoundException e ) {
 49  0
          throw new RuntimeException( e );
 50   
       } catch ( InstantiationException e ) {
 51  0
          throw new RuntimeException( e );
 52   
       } catch ( IllegalAccessException e ) {
 53  0
          throw new RuntimeException( e );
 54   
       }
 55   
    }
 56   
 
 57   
    /**
 58   
     * @return java.sql.Connection
 59   
     */
 60  0
    public Connection getCon() {
 61  0
       return con;
 62   
    }
 63   
 
 64  0
    public String quote( String k ) throws SQLException {
 65  0
       return con.nativeSQL( k );
 66   
    }
 67   
 
 68   
    /**
 69   
     * @return java.sql.Statement
 70   
     * @throws SQLException
 71   
     */
 72  0
    public Statement newStatement() throws SQLException {
 73  0
       return con.createStatement();
 74   
    }
 75   
 
 76   
    //  public boolean isClosed() throws SQLException {
 77   
    //      return stat.getResultSet().close();
 78   
    //   }
 79   
 
 80   
    /**
 81   
     * Close the database connection.
 82   
     */
 83  0
    public void closeCon() {
 84  0
       try {
 85  0
          this.con.close();
 86   
       } catch ( SQLException ex ) {
 87  0
          ex.printStackTrace();
 88   
       }
 89  0
       con = null;
 90   
    }
 91   
 
 92   
    // This is really only here for debugging.
 93  0
    public Handle() throws SQLException {
 94  0
       this( "localhost", "tmm", "pavlidis", "toast" );
 95   
    }
 96   
 
 97  0
    public Handle( String host, String database ) throws SQLException {
 98  0
       this( host, database, "pavlidis", "toast" );
 99   
    }
 100   
 
 101   
    /**
 102   
     * @param query String
 103   
     * @return int
 104   
     * @throws SQLException
 105   
     */
 106  0
    public int runUpdateQuery( String query ) throws SQLException {
 107  0
       return newStatement().executeUpdate( query );
 108   
    }
 109   
 
 110   
    /**
 111   
     * @param query String
 112   
     * @return java.sql.ResultSet
 113   
     * @throws SQLException
 114   
     */
 115  0
    public ResultSet runQuery( String query ) throws SQLException {
 116  0
       return newStatement().executeQuery( query );
 117   
    }
 118   
 
 119   
    /**
 120   
     * @param query String
 121   
     * @return java.sql.PreparedStatement
 122   
     * @throws SQLException
 123   
     */
 124  0
    public PreparedStatement prepareStatement( String query ) throws SQLException {
 125  0
       return con.prepareStatement( query );
 126   
    }
 127   
 
 128   
    /**
 129   
     * @param query String
 130   
     * @return java.util.Map
 131   
     * @throws SQLException
 132   
     */
 133  0
    public Map queryToMap( String query ) throws SQLException {
 134  0
       Map result = new HashMap();
 135   
 
 136  0
       ResultSet k = runQuery( query );
 137   
 
 138  0
       if ( k != null ) {
 139  0
          while ( k.next() ) {
 140  0
             result.put( k.getObject( 1 ), k.getObject( 2 ) );
 141   
          }
 142  0
          k.close();
 143   
       }
 144   
 
 145  0
       return result;
 146   
    }
 147   
 
 148   
    /**
 149   
     * @param query String
 150   
     * @return java.util.Set
 151   
     * @throws SQLException
 152   
     */
 153  0
    public Set queryToSet( String query ) throws SQLException {
 154  0
       Set result = new HashSet();
 155   
 
 156  0
       ResultSet k = runQuery( query );
 157   
 
 158  0
       if ( k != null ) {
 159  0
          while ( k.next() ) {
 160  0
             result.add( k.getObject( 1 ) );
 161   
          }
 162  0
          k.close();
 163   
       }
 164   
 
 165  0
       return result;
 166   
 
 167   
    }
 168   
 
 169   
    /**
 170   
     * @param query String
 171   
     * @return java.util.List
 172   
     * @throws SQLException
 173   
     */
 174  0
    public List queryToList( String query ) throws SQLException {
 175  0
       List result = new Vector();
 176   
 
 177  0
       ResultSet k = runQuery( query );
 178   
 
 179  0
       if ( k != null ) {
 180  0
          while ( k.next() ) {
 181  0
             result.add( k.getObject( 1 ) );
 182   
          }
 183  0
          k.close();
 184   
       }
 185   
 
 186  0
       return result;
 187   
    }
 188   
 
 189   
    /**
 190   
     * @param query String
 191   
     * @return Object containing the first result obtained. If no result, it return null.
 192   
     * @throws SQLException
 193   
     */
 194  0
    public Integer queryToInt( String query ) throws SQLException {
 195   
 
 196  0
       ResultSet k = runQuery( query );
 197  0
       if ( k != null ) {
 198  0
          while ( k.next() ) {
 199  0
             return new Integer( k.getInt( 1 ) );
 200   
          }
 201  0
          k.close();
 202   
       }
 203  0
       return null;
 204   
 
 205   
    }
 206   
 
 207   
    /**
 208   
     * @param query String
 209   
     * @return java.lang.Double
 210   
     * @throws SQLException
 211   
     */
 212  0
    public Double queryToDouble( String query ) throws SQLException {
 213   
 
 214  0
       ResultSet k = runQuery( query );
 215  0
       if ( k != null ) {
 216  0
          while ( k.next() ) {
 217  0
             return new Double( k.getDouble( 1 ) );
 218   
          }
 219  0
          k.close();
 220   
       }
 221  0
       return null;
 222   
 
 223   
    }
 224   
 
 225   
    /**
 226   
     * @param query String
 227   
     * @return java.lang.String
 228   
     * @throws SQLException
 229   
     */
 230  0
    public String queryToString( String query ) throws SQLException {
 231   
 
 232  0
       ResultSet k = runQuery( query );
 233  0
       if ( k != null ) {
 234  0
          while ( k.next() ) {
 235  0
             return new String( k.getString( 1 ) );
 236   
          }
 237  0
          k.close();
 238   
       }
 239  0
       return null;
 240   
 
 241   
    }
 242   
 
 243   
 }