org.bibalex.daf.installer
Class DBConnection

java.lang.Object
  extended by org.bibalex.daf.installer.DBConnection

public class DBConnection
extends java.lang.Object

Copyright (c) 2006-2007 by Bibliotheca Alexandrina This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

Author:
shehab.kamal and mohammed.abuouda@bibalex.org

Field Summary
private  java.sql.Connection connection
          Holds the connection to be used by each method.
private  java.lang.String dbDriver
          Holds the database driver that is used in assocaition with the database url to create a connection.
private  java.lang.String dbUrl
          Holds the Database url on the form jdbc:subprotocol:subname.
private static java.lang.String driver
          Holds the database driver that is used in assocaition with the database url to create a connection.
private static DBConnection instance
          Holds the sole instace of DBConnection.
private static java.sql.Connection Sqlconnection
           
private static java.lang.String url
          Holds the Database url on the form jdbc:subprotocol:subname.
 
Constructor Summary
private DBConnection()
          An empty constructor, to override the constructor supplied by the compiler.
  DBConnection(DBConnection connection)
          A deep copy constructor.
private DBConnection(java.lang.String driver, java.lang.String url)
          Creates a new instacne of DBconnection.
 
Method Summary
 boolean _startTransaction()
          Starts a new transaction
 boolean _stopTransaction(boolean isSuccessful)
          Stops the current transaction by either commiting or rolling it back.
static boolean checkSqlConnection()
          Checks if there is a SQL Connection as default instance or not.
static DBConnection CreateNewDBConnection(java.lang.String driver, java.lang.String url)
          Creates and returns a new DBConnection, that is different from the default instance and will not be considered the default instance.
static void dropAll()
           
 OperationInfo executeNonQuery(java.lang.String query)
          Executes the specified query and gets the number of rows affected.
 OperationInfo executeProcedure(java.lang.String procName, java.util.ArrayList procArguments)
          Executes the given stored procedure.
 OperationInfo executeReader(java.lang.String query)
          Executes the specified query and returns a DataTable representing the ResultSet.
static OperationInfo executeScalar(java.lang.String query)
          Executes the specified query and returns the first column of the first row of the ResultSet.
static java.lang.String executeScript(java.lang.String dbname, java.lang.String dbuser, java.lang.String dbpassword, java.lang.String scriptpath, boolean verbose)
           
static java.lang.String executeScript2(java.lang.String host, java.lang.String dbuser, java.lang.String dbpassword, java.lang.String scriptpath, boolean verbose)
           
 java.lang.String formulateArguments(java.util.ArrayList arguments)
           
static DBConnection getDefaultInstance()
          Gets the default DBConnection.
static java.sql.Connection getSqlConnection()
          Gets Sql Server DBConnection.
static void setDefaultConnectionString(java.lang.String driver, java.lang.String url)
          Sets the connection string to be sued when a new connection is to be created.
 boolean startTransaction()
          Starts a new transaction
 boolean stopTransaction(boolean isSuccessful)
          Stops the current transaction by either commiting or rolling it back.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

driver

private static java.lang.String driver
Holds the database driver that is used in assocaition with the database url to create a connection.


url

private static java.lang.String url
Holds the Database url on the form jdbc:subprotocol:subname.


instance

private static DBConnection instance
Holds the sole instace of DBConnection.


dbDriver

private java.lang.String dbDriver
Holds the database driver that is used in assocaition with the database url to create a connection.


dbUrl

private java.lang.String dbUrl
Holds the Database url on the form jdbc:subprotocol:subname.


connection

private java.sql.Connection connection
Holds the connection to be used by each method.


Sqlconnection

private static java.sql.Connection Sqlconnection
Constructor Detail

DBConnection

private DBConnection()
An empty constructor, to override the constructor supplied by the compiler.


DBConnection

private DBConnection(java.lang.String driver,
                     java.lang.String url)
Creates a new instacne of DBconnection.

Parameters:
driver - The database driver.
url - The database Url.

DBConnection

public DBConnection(DBConnection connection)
A deep copy constructor.

Parameters:
connection - The connection to .be copied.
Method Detail

setDefaultConnectionString

public static void setDefaultConnectionString(java.lang.String driver,
                                              java.lang.String url)
Sets the connection string to be sued when a new connection is to be created.

Parameters:
driver - The database driver.
url - The database Url.

checkSqlConnection

public static boolean checkSqlConnection()
Checks if there is a SQL Connection as default instance or not.

Returns:
true if there exist, false otherwise.

getSqlConnection

public static java.sql.Connection getSqlConnection()
Gets Sql Server DBConnection.

Returns:
The default DBConnection.

getDefaultInstance

public static DBConnection getDefaultInstance()
Gets the default DBConnection.

Returns:
The default DBConnection.

CreateNewDBConnection

public static DBConnection CreateNewDBConnection(java.lang.String driver,
                                                 java.lang.String url)
Creates and returns a new DBConnection, that is different from the default instance and will not be considered the default instance. The Object returned is totally independent from the default configuration and doesn't affect any static members of this class.

Parameters:
driver - The database driver.
url - The database Url.
Returns:
a new DBConnection instance.

executeReader

public OperationInfo executeReader(java.lang.String query)
Executes the specified query and returns a DataTable representing the ResultSet.

Parameters:
query - The query to be executed.
Returns:
An OperationInfo instance whose Result is a DataTable representing the ResultSet.

executeScalar

public static OperationInfo executeScalar(java.lang.String query)
Executes the specified query and returns the first column of the first row of the ResultSet.

Parameters:
query - The query to be executed.
Returns:
An OperationInfo instance whose Result is a object containing a single value.

executeNonQuery

public OperationInfo executeNonQuery(java.lang.String query)
Executes the specified query and gets the number of rows affected.

Parameters:
query - The query to be executed, likely an INSERT, UPDATE or DELETE query.
Returns:
An OperationInfo instance whose Result is an integer representing the number of rows affected.

executeProcedure

public OperationInfo executeProcedure(java.lang.String procName,
                                      java.util.ArrayList procArguments)
Executes the given stored procedure.

Parameters:
procName - The name of the stored procedure.
procArguments - The arguments of the stored proceure.
Returns:
An operation info that holds the result of the stored procedure.

formulateArguments

public java.lang.String formulateArguments(java.util.ArrayList arguments)

_startTransaction

public boolean _startTransaction()
Starts a new transaction

Returns:
A boolean indicating if the operation done successfully or not.

_stopTransaction

public boolean _stopTransaction(boolean isSuccessful)
Stops the current transaction by either commiting or rolling it back.

Parameters:
isSuccessful - Indicates whether the transaction should be committed or rolled back.
Returns:
A boolean indicating if the operation done successfully or not.

startTransaction

public boolean startTransaction()
Starts a new transaction

Returns:
A boolean indicating if the operation done successfully or not.

stopTransaction

public boolean stopTransaction(boolean isSuccessful)
Stops the current transaction by either commiting or rolling it back.

Parameters:
isSuccessful - Indicates whether the transaction should be committed or rolled back.
Returns:
A boolean indicating if the operation done successfully or not.

executeScript

public static java.lang.String executeScript(java.lang.String dbname,
                                             java.lang.String dbuser,
                                             java.lang.String dbpassword,
                                             java.lang.String scriptpath,
                                             boolean verbose)

executeScript2

public static java.lang.String executeScript2(java.lang.String host,
                                              java.lang.String dbuser,
                                              java.lang.String dbpassword,
                                              java.lang.String scriptpath,
                                              boolean verbose)

dropAll

public static void dropAll()