net.spy.db
Class SpyDB

java.lang.Object
  extended by net.spy.SpyObject
      extended by net.spy.db.SpyDB
Direct Known Subclasses:
SpyCacheDB

public class SpyDB
extends SpyObject

SpyDB is an abstraction of both net.spy.pool and java.sql.


Field Summary
protected static int INIT_FROM_CONFIG
          Initialization type for SpyDB initialized from a config.
protected static int INIT_FROM_CONN
          Initialization type for SpyDB initialized from a Connection.
 
Constructor Summary
SpyDB(java.sql.Connection c)
          Get a SpyDB object wrapping the given connection.
SpyDB(SpyConfig c)
          Create a SpyDB object based on the description found in the passed in SpyConfig object.
 
Method Summary
 void close()
          Free an established database connection - alias to freeDBConn()
static java.lang.String dbquoteStr(java.lang.String in)
          Make a string safe for usage in a SQL query, quoting apostrophies, etc...
 java.sql.ResultSet executeQuery(java.lang.String query)
          Execute a query and return a resultset, will establish a database connection if necessary.
 int executeUpdate(java.lang.String query)
          Execute a query that doesn't return a ResultSet, such as an update, delete, or insert.
 void freeDBConn()
          Free an established database connection.
protected  SpyConfig getConfig()
          Get the configuration from which this SpyDB was instatiated.
 java.sql.Connection getConn()
          Get a connection out of the pool.
 int getInitType()
          Get the type of initialization that created this object.
protected  ConnectionSource getSource()
          Get the source of connections.
protected  void init()
          Initialize SpyDB.
 boolean isClosed()
          Has close() been called?
 java.sql.CallableStatement prepareCall(java.lang.String query)
          Prepare a callable statement.
 java.sql.PreparedStatement prepareStatement(java.lang.String query)
          Prepare a statement.
 
Methods inherited from class net.spy.SpyObject
getLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INIT_FROM_CONFIG

protected static final int INIT_FROM_CONFIG
Initialization type for SpyDB initialized from a config.

See Also:
Constant Field Values

INIT_FROM_CONN

protected static final int INIT_FROM_CONN
Initialization type for SpyDB initialized from a Connection.

See Also:
Constant Field Values
Constructor Detail

SpyDB

public SpyDB(SpyConfig c)
Create a SpyDB object based on the description found in the passed in SpyConfig object.

The configuration may vary greatly depending on the connector. The only configuration option for SpyDB itself is dbConnectionSource which specifies the name of the class that implements ConnectionSource that will be providing connections for this SpyDB instance. The default is net.spy.db.ObjectPoolConnectionSource. You will also need to provide any additional parameters that are required by the ConnectionSource in this config.

Parameters:
c - SpyConfig object describing how to connect.

SpyDB

public SpyDB(java.sql.Connection c)
Get a SpyDB object wrapping the given connection.

Parameters:
c - the connection to wrap.
Method Detail

getInitType

public int getInitType()
Get the type of initialization that created this object.

Returns:
either INIT_FROM_CONFIG or INIT_FROM_CONN

executeQuery

public java.sql.ResultSet executeQuery(java.lang.String query)
                                throws java.sql.SQLException
Execute a query and return a resultset, will establish a database connection if necessary.

Parameters:
query - SQL query to execute.
Throws:
java.sql.SQLException - an exception is thrown if the connection fails, or the SQL query fails.

executeUpdate

public int executeUpdate(java.lang.String query)
                  throws java.sql.SQLException
Execute a query that doesn't return a ResultSet, such as an update, delete, or insert.

Parameters:
query - SQL query to execute.
Throws:
java.sql.SQLException - an exception is thrown if the connection fails, or the SQL query fails.

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String query)
                                            throws java.sql.SQLException
Prepare a statement.

Parameters:
query - SQL query to prepare.
Throws:
java.sql.SQLException - thrown if something bad happens.

prepareCall

public java.sql.CallableStatement prepareCall(java.lang.String query)
                                       throws java.sql.SQLException
Prepare a callable statement.

Parameters:
query - SQL query to prepare for call.
Throws:
java.sql.SQLException - thrown if something bad happens.

getConn

public java.sql.Connection getConn()
                            throws java.sql.SQLException
Get a connection out of the pool. A given SpyDB object can only maintain a single database connection, so if multiple connections from the pool are needed, multiple SpyDB objects will be required.

Throws:
java.sql.SQLException - An exception may be thrown if a database connection cannot be obtained.

freeDBConn

public void freeDBConn()
Free an established database connection. The connection is whatever connection has already been instablished by this instance of the object. If a connection has not been established, this does nothing.


close

public void close()
Free an established database connection - alias to freeDBConn()


init

protected void init()
Initialize SpyDB. This allows any subclasses to perform further initialization.


dbquoteStr

public static java.lang.String dbquoteStr(java.lang.String in)
Make a string safe for usage in a SQL query, quoting apostrophies, etc...

Parameters:
in - the string that needs to be quoted
Returns:
a new, quoted string

isClosed

public boolean isClosed()
Has close() been called?


getSource

protected ConnectionSource getSource()
Get the source of connections.

Returns:
a ConnectionSource instance, or null if this SpyDB instance was created from a config

getConfig

protected SpyConfig getConfig()
Get the configuration from which this SpyDB was instatiated.

Returns:
the config, or null if the SpyDB was created from a connection


Copyright © 1995-2005 SPY Internetworking. All Rights Reserved.