download DBConnect.java
Language: Java
License: LGPL
Copyright: (c) 2000-2003 by Patrick Lacson (patrick@lacson.net) *
LOC: 139
Project Info
JScripture Project(jscripture)
Server: SourceForge
Type: cvs
...ripture\simon\src\simon\db\
   DBConnect.java

/*****************************************************************************
 *                                                                           *
 *  Copyright (c) 2000-2003 by Patrick Lacson (patrick@lacson.net)           *
 *                                                                           *
 *  Version: $Id: DBConnect.java,v 1.1.1.1 2001/03/27 08:49:14 placson Exp $     *
 *                                                                           *
 *  This library is free software; you can redistribute it and/or modify it  *
 *  under the terms of the GNU Lesser General Public License as published    *
 *  by the Free Software Foundation; either version 2 of the License, or     *
 *  (at your option) any later version.                                      *
 *                                                                           *
 *  This library is distributed in the hope that it will be useful, but      *
 *  WITHOUT ANY WARRANTY; without even the implied warranty of               *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        *
 *  Lesser General Public License for more details.                          *
 *                                                                           *
 *  You should have received a copy of the GNU Lesser General Public         *
 *  License along with this library; if not, write to the Free Software      *
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  *
 *                                                                           *
 *****************************************************************************/

package simon.db;

import java.sql.*;
import java.util.*;


/**
 * DBConnect is a generic JDBC Wrapper that allows the
 * underlying low-level JDBC code to be encapsulated.  
 *
 * @author Patrick Lacson <a href="mailto:patrick@lacson.net">patrick@lacson.net</a>
 * @version $Id: DBConnect.java,v 1.1.1.1 2001/03/27 08:49:14 placson Exp $
 * 
 */

public class DBConnect {

    public final static int DB_MYSQL=1;
    public final static int DB_ORACLE=2;
    public final static int DB_MSSQL=3;
    public final static int DB_OTHER=4;
    public DBConnect() {
	connections=0;
	isOutputDebug=false;
    }

    /**
     * This method causes the connection to be made to the
     * data source.
     *
     * @return True if connection was successful, False if connection
     * was unsucessful
     */
    public boolean connection() {
	try {
	    if(dbDriver==null)
		throw new Exception("must run loadInfo method first!");
	} catch(Exception e) {
	    e.printStackTrace();
	}

	try {
	    Class.forName(dbDriver).newInstance();
	    DriverManager.setLoginTimeout(30);
	    
	    if(isOutputDebug)
		DriverManager.setLogStream(System.out);
	    
	    if(login!=null)
		con = DriverManager.getConnection(dbURL,login,password);
	    else
		con = DriverManager.getConnection(dbURL);
	    

	    connections++;
	    if(catalog!=null)
		con.setCatalog(catalog);
	    
	    stmt = con.createStatement();
	    
	    return true;
	} catch(ClassNotFoundException c) {
	    System.out.println("connect() not OK!");
	    System.out.println("I couldn't find the jdbc class driver!..ClassNotFoundException caught.");
	} catch(InstantiationException ie) {
	    System.out.println("connect() not OK!");
	    System.out.println("InstantiationException caught!");
	} catch(IllegalAccessException iae) {
	    System.out.println("connect() not OK!");
	    System.out.println("IllegalAccessException caught!");
	} catch(SQLException s) {
	    System.out.println("connect() not OK>");
	    System.out.println("SQLException caught.");
	    s.printStackTrace();
	}
	return false;
    }

    /**
     * Method checks for the connection state of the object
     *
     * @return If the connection() method was invoked, returns true.
     * If the connection() method was invoked but returned
     * an error or false value, returns false
     */
    public boolean isConnected() {
	return (con!=null);
    }

    /**
     * Method that declares the debug mode of the wrapper
     *
     * @param d The boolean parameter passed is either true or false
     * to indicate a verbose debug or no debug, respectively.
     *
     * Note: By default the debug output is sent to the
     * standard out (System.out)
     */
    public void isDebug(boolean d) { 
	this.isOutputDebug=d; 
    }


    /**
     * Closes the connection
     */
    public void close() {
	if(con!=null)
	    try {
		con.close();
	    } catch(SQLException s) {
		System.out.println("Error in trying to close connection..");
		s.printStackTrace();
	    }
    }


    /**
     * Set the catalog(database) for this connection instance.
     *
     * @param c String parameter is the name of the database.
     */
    public void setCatalog(String c) {
	if(c!=null) {
	    try {
		con.setCatalog(c);
	    } catch(SQLException s) {
		System.out.println("setCatalog() failed! using catalog: " + c);
		s.printStackTrace();
	    }
	} else return;
    }

    /**
     * Set the Database Type for this connection instance.
     * @param dbt int value.  Possible values are
     * DBConnect.DB_MYSQL, DBConnect.DB_ORACLE, DBConnect.DB_MSSQL or
     * DBConnect.DB_OTHER
     */
    public void setDbType(int dbt) {
	this.dbtype = dbt;
    }

    /**
     * Returns a resultSet that is scrollable.  Only available
     * with databases that support the scrollable cursor.
     * Microsoft Access does not support this feature, nor
     * does MySQL 3.23.
     *
     * @param sql SQL String used to load the ResultSet.
     * i.e. SELECT name,email from userTbl where id=10
     * @return Returns the ResultSet with type SCROLLABLE_INSENSITIVE
     */
    public ResultSet getScrollableResultSet(String sql) {
	if(con==null) {
	    System.out.println("must run connect() method first before running getScrollableResultSet() method!");
	    return null;
	}
	try {
	    stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
				       ResultSet.CONCUR_UPDATABLE);
	    //stmt.setFetchSize(25);
	    rs = stmt.executeQuery(sql);
	    return rs;
	} catch(SQLException s) {
	    System.out.println("getSrollableResultSet() failed!");
	    s.printStackTrace();
	}
	return rs;
    }

    /**
     * Returns the current connection instance
     *
     * @return Current Connection value, NULL if no connection
     * was instantiated
     */
    public Connection getConnection() {
	if(con!=null) return con;
	else return null;
    }

    /**
     * This method returns the resultset object.
     *
     * @param sql SQL String used to load the resultset.
     * @return ResultSet object.
     */
    public ResultSet getResultSet(String sql) {
	if(con==null) {
	    System.out.println("must run connect() method first before running getResultSet() method!");
	    return null;
	}
	try {
	    stmt = con.createStatement();
	    rs = stmt.executeQuery(sql);
	    return rs;
	} catch(SQLException s) {
	    System.out.println("getResultSet() failed!");
	    s.printStackTrace();
	}
	return rs;
    }

    /**
     * Execute a SQL Statement
     *
     * @param sql String parameter that contains the SQL Statement to be
     * executed
     */
    public void executeUpdate(String sql) {
	try {
	    stmt.executeUpdate(sql);
	} catch(SQLException s) {
	    System.out.println("executeUpdate() failed!");
	    System.out.println(s);
	}
    }

    /**
     * This method loads the information requried to make a
     * connection. The login, password are optional.  If
     * the URL already contains the login and password, these
     * parameters can be kept null.
     *
     * @param url String parameter that contains the JDBC URL
     * ie. jdbc:odbc:TSTDB
     * @param driver String parameter that indicates the driver class
     * to be used.
     * ie. com.inet.tds.TdsDriver
     * @param login String parameter used for the login. (optional)
     * @param password String parameter used for password (optional)
     */

    public void loadInfo(String url, String driver, String login, String password) {
	this.dbURL=url;
	this.dbDriver=driver;
	this.login=login;
	this.password=password;
    }

    private Connection con		=null;
    private ResultSet rs		=null;
    private Statement stmt		=null;
    private CallableStatement cstmt	=null;
    private String dbDriver		=null;
    private String dbURL		=null;
    private String login		=null;
    private String password		=null;
    private String catalog		=null;
    private static int connections	=0;
    private boolean isOutputDebug	=false;
    private int dbtype			=4;

}


About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us