Saturday, May 25, 2013

Simple facade for making calls to a database

 /*********************************************************************/
 /*(c) Copyright IBM Corp. 2003  All rights reserved.                 */
 /*                                                                   */
 /*This sample program is owned by International Business Machines    */
 /*Corporation or one of its subsidiaries ("IBM") and is copyrighted  */
 /*and licensed, not sold.                                            */
 /*                                                                   */
 /*You may copy, modify, and distribute this sample program in any    */
 /*form without payment to IBM,  for any purpose including developing,*/
 /*using, marketing or distributing programs that include or are      */
 /*derivative works of the sample program.                            */
 /*                                                                   */
 /*The sample program is provided to you on an "AS IS" basis, without */
 /*warranty of any kind.  IBM HEREBY  EXPRESSLY DISCLAIMS ALL         */
 /*WARRANTIES EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO*/
 /*THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTIC-*/
 /*ULAR PURPOSE. Some jurisdictions do not allow for the exclusion or */
 /*limitation of implied warranties, so the above limitations or      */
 /*exclusions may not apply to you.  IBM shall not be liable for any  */
 /*damages you suffer as a result of using, modifying or distributing */
 /*the sample program or its derivatives.                             */
 /*                                                                   */
 /*Each copy of any portion of this sample program or any derivative  */
 /*work,  must include a the above copyright notice and disclaimer of */
 /*warranty.                                                          */
 /*                                                                   */
 /*********************************************************************/


package com.ibm.DB2Excel;
 /*********************************************************************/
  /******************************************************************/
  /* Simple facade for making calls to a database                   */
  /******************************************************************/
 import java.sql.*;
import java.util.ArrayList;
import java.util.List;
 /**
 * Simple facade that hides the details of using the JDBC API such as loading the JDBC driver etc.
 */
public class SQLFacade {
     static final String NO_TABLE = "----------------------";
     /**
     * the name of IBM DB2 JDBC driver
     */
    private static final String jdbcDriver = "COM.ibm.db2.jdbc.app.DB2Driver";
     /**
     *
     */
    private static final String jdbcURL = "jdbc:db2:";
     /**
     * Connection to a database
     */
    private Connection connection;
     /**
     * Creates a connection to a database
     * @param username username to connect to the database with. Can be null.
     * @param password password. Can be null.
     * @param database name of the database to which we connect. Cannot be null.
     * @param port port on which to connect to database
     *
     * @exception SQLException if a database error occurs
     * @exception ClassNotFoundException if a error occurs when loading the JDBC driver
     *
     */
    public SQLFacade(String username, String password, String database, String port) throws SQLException, ClassNotFoundException {
         Class.forName(jdbcDriver);
        String dbURL = jdbcURL + database;
         if (!port.equals("")) {
            System.out.println("Need to add port");
        }
         if (!username.equals("")) {
            connection = DriverManager.getConnection(dbURL, username, password);
        } else {
            connection = DriverManager.getConnection(dbURL);
        }
    }
     /**
     * Execute the SQL query
     * @param query SQL query to execute
     * @return ResultSet
     * @exception SQLException if a database error occurs
     */
    public ResultSet executeQuery(String query) throws SQLException {
        PreparedStatement st = connection.prepareStatement(query);
        return st.executeQuery();
    }
     /**
     *  Queries the database for the tables created by the user
     */
    public Object[] getTablesFromDb() throws SQLException {
        DatabaseMetaData md = connection.getMetaData();
        String types[] = {"TABLE"};
        ResultSet set = md.getTables(null, null, null, types);
        List list = new ArrayList();

        list.add(NO_TABLE);
         while (set.next()) {
            list.add(set.getObject(3));
        }
        set.close();
        if (list.size() > 0)
            return list.toArray();
        else
            return null;
    }

    /**
     * Close connection to database
     */
    public void close() {
        try {
            connection.close();
        } catch (SQLException se) {
            connection = null;
        }
    }
     protected void finalize() {
        close();
    }
}

No comments:

Post a Comment