|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.billpringle.utils.WrpDb
public class WrpDb
This class provides a wrapper for a database connection. It is expected that clients will create their own classes that extend this class. The client class would then provide the connection information for their specific database. This would allow client programs to connect to the database without having to provide the connection information. A class extension can be done as follows:
public class EMailDb extends WrpDb
{
public static String strDatabase = new String("dbname");
public static String strHost = new String("hostname");
public static String strUser = new String("username");
public static String strPassword = new String("password");
public EMailDb() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException
{
super(strDatabase, strHost, strUser, strPassword);
}
}
By default, a MySQL database connection is established with the following parameters:
To establish a connection using different parameters use one of two means:
WrpDb db = WrpDb(dbname, hostname, username, password);
WrpDb db = WrpDb(false);
setDbName(dbname);
setHostName(hostname)
...
openConnection();
A connection can also be established to a non-MySQL database:
WrpDb db = WrpDb(false);
setConnectString(connectionstring);
openConnection();
![]() |
![]() |
Unless noted otherwise, all materials available for download from my site are copyrighted by Bill Pringle, and are licensed under a Creative Commons License. |
Field Summary | |
---|---|
java.sql.Connection |
conn
Actual database connection. |
(package private) static long |
serialVersionUID
The serial version ID |
java.lang.Character |
SqlQuote
The character to be used for quoting strings in queries. |
static java.lang.Character |
SQLQUOTE_DOUBLE
A double quote character ("). |
static java.lang.Character |
SQLQUOTE_SINGLE
A single quote character ('). |
java.sql.Statement |
stmt
The actual Statement object. |
java.lang.String |
strConnect
The connection String. |
java.lang.String |
strDatabase
The database name. |
java.lang.String |
strHost
The host name This variable is used to build the connection string by the method buildConnectString(). |
java.lang.String |
strPassword
The user password This variable is used to build the connection string by the method buildConnectString(). |
java.lang.String |
strUser
The user name This variable is used to build the connection string by the method buildConnectString(). |
Constructor Summary | |
---|---|
WrpDb()
This is the default constructor. |
|
WrpDb(java.lang.Boolean open)
This constructor won't open the connection if the parameter is false. |
|
WrpDb(java.lang.String strConnect)
This constructor will open a database using the specified connection string. |
|
WrpDb(java.lang.String dbName,
java.lang.String hostName,
java.lang.String userName,
java.lang.String userPassword)
This constructor opens the database using specified parameters. |
Method Summary | |
---|---|
void |
buildConnectString()
Rebuild the connection string using the previously specified components to the string. |
void |
close()
Close the database connection, suppressing any exceptions. |
void |
closeConnection()
Close database connection. |
java.sql.ResultSet |
executeQuery(java.lang.String query)
Execute the specified SQL query and return the result of the query. |
int |
executeUpdate(java.lang.String query)
Execute the specified SQL update query and return the result. |
java.util.Vector<java.lang.String> |
getColumn(java.sql.ResultSet rs,
java.lang.String colName)
Return the column for the specified ResultSet. |
java.lang.String |
getConnectString()
Get the current connection string. |
boolean |
getMoreResults()
Test if more results sets remaining. |
boolean |
getMoreResults(int current)
Test if more results sets remaining, specifying what to do with current Result Set. |
java.sql.ResultSet |
getResultSet()
Get next result set. |
java.lang.Character |
getSqlQuote()
Return what type of quote is being used for queries. |
void |
openConnection()
Establish a connection to the database. |
void |
setConnectString(java.lang.String str)
Manually set the connection string. |
void |
setDbName(java.lang.String dbName)
Rebuild the connection string using the specified database name. |
void |
setHostName(java.lang.String hostName)
Rebuild the connection string using the specified host name. |
void |
setPassword(java.lang.String password)
Rebuild the connection string using the specified password. |
void |
setSqlQuote(java.lang.Character sqlQuote)
Specify what quote character should be used to enclose literals. |
void |
setUserName(java.lang.String userName)
Rebuild the connection string using the specified user name. |
java.lang.String |
sqlSafe(java.lang.String s)
Make sure string is safe for SQL insert. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
static final long serialVersionUID
public java.lang.String strDatabase
public java.lang.String strHost
public java.lang.String strUser
public java.lang.String strPassword
public java.lang.String strConnect
public java.sql.Connection conn
This connection is declared public, so that the client can use the object directly. This can result in an unstable condition, and so the client should use all caution, and assume all responsibility for this action.
public java.sql.Statement stmt
This connection is declared public, so that the client can use the object directly. This can result in an unstable condition, and so the client should use all caution, and assume all responsibility for this action.
public java.lang.Character SqlQuote
This character is declared public, and can be set using the setSqlQuote() method.
public static java.lang.Character SQLQUOTE_SINGLE
This variable can be used to modify the quote character that will be used to quote strings in queries using the setSqlQuote() method.
public static java.lang.Character SQLQUOTE_DOUBLE
This variable can be used to modify the quote character that will be used to quote strings in queries using the setSqlQuote() method
Constructor Detail |
---|
public WrpDb() throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.sql.SQLException
java.lang.ClassNotFoundException
java.lang.IllegalAccessException
java.lang.InstantiationException
java.sql.SQLException
public WrpDb(java.lang.String dbName, java.lang.String hostName, java.lang.String userName, java.lang.String userPassword) throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.sql.SQLException
dbName
- the name of the database.hostName
- the name of the database server.userName
- is the user name for the database connectionuserPassword
- is the user password for the database connection
java.sql.SQLException
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException
public WrpDb(java.lang.String strConnect) throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.sql.SQLException
strConnect
- connection string
java.lang.ClassNotFoundException
java.lang.IllegalAccessException
java.lang.InstantiationException
java.sql.SQLException
public WrpDb(java.lang.Boolean open) throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.sql.SQLException
This constructor can be used to create the object but not open the connection. The various parameters that make up the connection string can be specified using the various setter routines, (including specifying the connection string directly). Once complete, the database connection can then be opened using the openConnection().
open
- if true, the connection is opened; otherwise it isn't
java.lang.ClassNotFoundException
java.lang.IllegalAccessException
java.lang.InstantiationException
java.sql.SQLException
Method Detail |
---|
public void setDbName(java.lang.String dbName)
dbName
- the name of the database to be openedpublic void setHostName(java.lang.String hostName)
hostName
- the name of the MySql database host machinepublic void setUserName(java.lang.String userName)
userName
- the name of the user within the connection stringpublic void setPassword(java.lang.String password)
password
- the user passwordpublic void buildConnectString()
This method is called by all of the setter methods, so the user should never need to call this method directly
public void setConnectString(java.lang.String str)
This method can be used to establish a connection string for a non-MySQL database. For MySQL databases, the client can set database, host name, user name, and password with a constructor and/or setter routines.
str
- the connection string to be used to open the database.public java.lang.String getConnectString()
This method returns the current connection string.
public void openConnection() throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.sql.SQLException
This method assumes that the connection string has been set up correctly.
java.lang.ClassNotFoundException
java.lang.IllegalAccessException
java.lang.InstantiationException
java.sql.SQLException
public void closeConnection() throws java.sql.SQLException
java.sql.SQLException
public void close()
public java.sql.ResultSet executeQuery(java.lang.String query) throws java.sql.SQLException
query
- the SQL query to execute
java.sql.SQLException
public boolean getMoreResults()
public boolean getMoreResults(int current)
If an SQLException occurs, this method will return false. See java.sql.Statement.getMoreResults.
current
- constant defined in Statement
public java.sql.ResultSet getResultSet()
public int executeUpdate(java.lang.String query) throws java.sql.SQLException
query
- the update query to be executed
java.sql.SQLException
public java.util.Vector<java.lang.String> getColumn(java.sql.ResultSet rs, java.lang.String colName) throws java.sql.SQLException
rs
- the ResultSet from an SQL selection querycolName
- the name of the column to be retrieved
java.sql.SQLException
public java.lang.String sqlSafe(java.lang.String s)
This method will do the following:
It is the client's responsibility to call this method. This method should be used for each query before it is executed that involves user data in any form. In addition to preventing accidental data corruption (e.g., embedded quotes within a field value), it will also help prevent insertion attacks.
s
- candidate query string
public java.lang.Character getSqlQuote()
This method can be used to determine how to handle embedded quotes. For example, if the query has literals enclosed in single quotes, then an apostrophe can cause problems. Likewise, if the query has literals enclosed in double quotes, then a double-quoted substring must be handled differently than if it were enclosed in single quotes
public void setSqlQuote(java.lang.Character sqlQuote)
sqlQuote
- the sqlQuote to set
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |