View Javadoc

    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     public Handle( String host, String database, String user, String password ) throws SQLException {
    43        try {
    44           Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
    45           String url = "jdbc:mysql://" + host + "/" + database + "?relaxAutoCommit=true";
    46           con = DriverManager.getConnection( url, user, password );
    47           //       stat = newStatement();
    48        } catch ( ClassNotFoundException e ) {
    49           throw new RuntimeException( e );
    50        } catch ( InstantiationException e ) {
    51           throw new RuntimeException( e );
    52        } catch ( IllegalAccessException e ) {
    53           throw new RuntimeException( e );
    54        }
    55     }
    56
    57     /***
    58      * @return java.sql.Connection
    59      */
    60     public Connection getCon() {
    61        return con;
    62     }
    63
    64     public String quote( String k ) throws SQLException {
    65        return con.nativeSQL( k );
    66     }
    67
    68     /***
    69      * @return java.sql.Statement
    70      * @throws SQLException
    71      */
    72     public Statement newStatement() throws SQLException {
    73        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     public void closeCon() {
    84        try {
    85           this.con.close();
    86        } catch ( SQLException ex ) {
    87           ex.printStackTrace();
    88        }
    89        con = null;
    90     }
    91
    92     // This is really only here for debugging.
    93     public Handle() throws SQLException {
    94        this( "localhost", "tmm", "pavlidis", "toast" );
    95     }
    96
    97     public Handle( String host, String database ) throws SQLException {
    98        this( host, database, "pavlidis", "toast" );
    99     }
    100
    101    /***
    102     * @param query String
    103     * @return int
    104     * @throws SQLException
    105     */
    106    public int runUpdateQuery( String query ) throws SQLException {
    107       return newStatement().executeUpdate( query );
    108    }
    109
    110    /***
    111     * @param query String
    112     * @return java.sql.ResultSet
    113     * @throws SQLException
    114     */
    115    public ResultSet runQuery( String query ) throws SQLException {
    116       return newStatement().executeQuery( query );
    117    }
    118
    119    /***
    120     * @param query String
    121     * @return java.sql.PreparedStatement
    122     * @throws SQLException
    123     */
    124    public PreparedStatement prepareStatement( String query ) throws SQLException {
    125       return con.prepareStatement( query );
    126    }
    127
    128    /***
    129     * @param query String
    130     * @return java.util.Map
    131     * @throws SQLException
    132     */
    133    public Map queryToMap( String query ) throws SQLException {
    134       Map result = new HashMap();
    135
    136       ResultSet k = runQuery( query );
    137
    138       if ( k != null ) {
    139          while ( k.next() ) {
    140             result.put( k.getObject( 1 ), k.getObject( 2 ) );
    141          }
    142          k.close();
    143       }
    144
    145       return result;
    146    }
    147
    148    /***
    149     * @param query String
    150     * @return java.util.Set
    151     * @throws SQLException
    152     */
    153    public Set queryToSet( String query ) throws SQLException {
    154       Set result = new HashSet();
    155
    156       ResultSet k = runQuery( query );
    157
    158       if ( k != null ) {
    159          while ( k.next() ) {
    160             result.add( k.getObject( 1 ) );
    161          }
    162          k.close();
    163       }
    164
    165       return result;
    166
    167    }
    168
    169    /***
    170     * @param query String
    171     * @return java.util.List
    172     * @throws SQLException
    173     */
    174    public List queryToList( String query ) throws SQLException {
    175       List result = new Vector();
    176
    177       ResultSet k = runQuery( query );
    178
    179       if ( k != null ) {
    180          while ( k.next() ) {
    181             result.add( k.getObject( 1 ) );
    182          }
    183          k.close();
    184       }
    185
    186       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    public Integer queryToInt( String query ) throws SQLException {
    195
    196       ResultSet k = runQuery( query );
    197       if ( k != null ) {
    198          while ( k.next() ) {
    199             return new Integer( k.getInt( 1 ) );
    200          }
    201          k.close();
    202       }
    203       return null;
    204
    205    }
    206
    207    /***
    208     * @param query String
    209     * @return java.lang.Double
    210     * @throws SQLException
    211     */
    212    public Double queryToDouble( String query ) throws SQLException {
    213
    214       ResultSet k = runQuery( query );
    215       if ( k != null ) {
    216          while ( k.next() ) {
    217             return new Double( k.getDouble( 1 ) );
    218          }
    219          k.close();
    220       }
    221       return null;
    222
    223    }
    224
    225    /***
    226     * @param query String
    227     * @return java.lang.String
    228     * @throws SQLException
    229     */
    230    public String queryToString( String query ) throws SQLException {
    231
    232       ResultSet k = runQuery( query );
    233       if ( k != null ) {
    234          while ( k.next() ) {
    235             return new String( k.getString( 1 ) );
    236          }
    237          k.close();
    238       }
    239       return null;
    240
    241    }
    242
    243 }