cmu.survey.jsp.tags
Class DatabaseConnection

java.lang.Object
  extended by javax.servlet.jsp.tagext.TagSupport
      extended by cmu.survey.jsp.tags.DatabaseConnection
All Implemented Interfaces:
java.io.Serializable, javax.servlet.jsp.tagext.IterationTag, javax.servlet.jsp.tagext.Tag, javax.servlet.jsp.tagext.TryCatchFinally
Direct Known Subclasses:
Survey

public class DatabaseConnection
extends javax.servlet.jsp.tagext.TagSupport
implements javax.servlet.jsp.tagext.TryCatchFinally

Connects to several different types of databases, and is used for most survey tags. It will probably handle anything that JDBC does, but it has been tested only with the following two:
- MYSQL (www.MySQL.com, a free, good, database)
- MS_ACCESS (Microsoft Access)

For example, to connect to a MySQL database, with the database named "databaseName", user "userName", and password "usersPassword", use one of the following:

You can use this as a JSP tag:

<%@ taglib uri="/WEB-INF/SurveyTags.tld" prefix="survey" %>
<survey:connection databaseName="jdbc:mysql://localhost:3306/databaseName?user=userName&password=usersPassword">
</survey:connection>


Or from regular Java code:

cmu.survey.DatabaseConnection connection = new cmu.survey.DatabaseConnection();
connection.setDatabaseName("jdbc:mysql://localhost:3306/databaseName?user=userName&password=usersPassword");
connection.doStartTag();


See Also:
Serialized Form

Field Summary
protected  java.lang.String databaseConnectionString
           
protected  int databaseType
           
static int MS_ACCESS
           
static int MYSQL
           
static int ODBC
           
protected  javax.servlet.jsp.JspWriter out
           
protected  javax.servlet.http.HttpSession session
           
protected  java.sql.Connection theConnect
           
protected  javax.servlet.http.HttpServletRequest theRequest
          protected so that subclasses can use it if needed.
 
Fields inherited from class javax.servlet.jsp.tagext.TagSupport
id, pageContext
 
Fields inherited from interface javax.servlet.jsp.tagext.IterationTag
EVAL_BODY_AGAIN
 
Fields inherited from interface javax.servlet.jsp.tagext.Tag
EVAL_BODY_INCLUDE, EVAL_PAGE, SKIP_BODY, SKIP_PAGE
 
Constructor Summary
DatabaseConnection()
           
 
Method Summary
 boolean checkSQLFieldValid(java.lang.String sqlFieldName)
          Throws a runtime exception if there is a bad field name.
static java.lang.String cleanString(java.lang.String sqlString)
          We clean this string by clearing out all values that need to be escaped out.
 java.lang.String cleanStringBetter(java.lang.String sqlString)
          We clean this string by clearing out all values that need to be escaped out.
static java.lang.String cleanStringThatShouldHaveBeenClean(java.lang.String sqlString, javax.servlet.jsp.PageContext pageContext)
           
 void close()
           
 void doCatch(java.lang.Throwable throwable)
           
 boolean doConnection()
           
 int doEndTag()
          This function closes the connection when the ending tag is encountered
 void doFinally()
           
 int doStartTag()
          This function opens a connection to the requested database when the tag is first encountered
 void finalize()
           
 java.sql.Connection getConnect()
           
static DatabaseConnection getDatabaseConnectionFrom(java.lang.String databaseName)
           
 java.lang.String getDatabaseName()
          Returns the database connection string.
 java.lang.String getDatabaseProgramName()
          Returns the name of this database program.
 int getDatabaseType()
           
static java.lang.String getDebuggingInfo()
           
 java.lang.String getHelpURL()
          Returns a URL with help about installing, starting, and stopping this database program.
 java.lang.Throwable getLastExceptionThrown()
           
 java.lang.String getMySqlDatabaseHost()
          This isn't terribly accurate or adaptable right now.
 java.lang.String getMySqlDatabaseName()
          This isn't terribly accurate or adaptable right now.
 java.lang.String getMySqlDatabasePassword()
          This isn't terribly accurate or adaptable right now.
 java.lang.String getMySqlDatabaseUser()
          This isn't terribly accurate or adaptable right now.
 javax.servlet.http.HttpSession getSession()
           
static java.lang.String getThisFolderURL(javax.servlet.jsp.PageContext pageContext)
          Get the URL with just the folder, as in: http://blahblah.com:8080/survey/
static java.lang.String getThisFullURL(javax.servlet.jsp.PageContext pageContext)
          Get Full URL -- as in http://blahblah.com:8080/survey/page1.jsp
static java.lang.String getURLFileName(javax.servlet.jsp.PageContext pageContext)
           
 boolean initializeAtStartTag()
           
 boolean isOpen()
           
static java.lang.Number parseAndCleanNumberStringThatShouldHaveBeenClean(java.lang.String sqlString)
           
static java.lang.Number parseAndCleanNumberStringThatShouldHaveBeenCleanStatic(java.lang.String sqlString)
           
 void reinit()
           
protected  void saveBugMessage(java.lang.String error)
           
protected  void saveBugMessage(java.lang.String error, java.lang.Throwable e)
           
protected  void saveNotificationMessage(java.lang.String error)
           
protected  void saveNotificationMessage(java.lang.String error, java.lang.Throwable e)
           
 void setDatabaseName(java.lang.String newDatabaseName)
           
 void setHttpServletRequest(javax.servlet.http.HttpServletRequest request)
           
 void setLastExceptionThrownToNull()
           
 void setPageContext(javax.servlet.jsp.PageContext context)
           
 void setSendErrorMessages(boolean emailTheErrorMessages)
           
 
Methods inherited from class javax.servlet.jsp.tagext.TagSupport
doAfterBody, findAncestorWithClass, getId, getParent, getValue, getValues, release, removeValue, setId, setParent, setValue
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

theConnect

protected java.sql.Connection theConnect

databaseConnectionString

protected java.lang.String databaseConnectionString

databaseType

protected int databaseType

MYSQL

public static final int MYSQL
See Also:
Constant Field Values

MS_ACCESS

public static final int MS_ACCESS
See Also:
Constant Field Values

ODBC

public static final int ODBC
See Also:
Constant Field Values

out

protected javax.servlet.jsp.JspWriter out

session

protected javax.servlet.http.HttpSession session

theRequest

protected javax.servlet.http.HttpServletRequest theRequest
protected so that subclasses can use it if needed. In DatabaseConnection, theRequest is used solely for checking what host name there is so we can automatically set an email address to send bug messages from -- so we know what server bug messages are from.

Constructor Detail

DatabaseConnection

public DatabaseConnection()
Method Detail

getDebuggingInfo

public static java.lang.String getDebuggingInfo()

getConnect

public java.sql.Connection getConnect()
Returns:
the connection object

getDatabaseConnectionFrom

public static DatabaseConnection getDatabaseConnectionFrom(java.lang.String databaseName)

getThisFullURL

public static final java.lang.String getThisFullURL(javax.servlet.jsp.PageContext pageContext)
Get Full URL -- as in http://blahblah.com:8080/survey/page1.jsp


getThisFolderURL

public static final java.lang.String getThisFolderURL(javax.servlet.jsp.PageContext pageContext)
Get the URL with just the folder, as in: http://blahblah.com:8080/survey/

Parameters:
pageContext -
Returns:

getURLFileName

public static final java.lang.String getURLFileName(javax.servlet.jsp.PageContext pageContext)

cleanStringThatShouldHaveBeenClean

public static final java.lang.String cleanStringThatShouldHaveBeenClean(java.lang.String sqlString,
                                                                        javax.servlet.jsp.PageContext pageContext)

parseAndCleanNumberStringThatShouldHaveBeenCleanStatic

public static final java.lang.Number parseAndCleanNumberStringThatShouldHaveBeenCleanStatic(java.lang.String sqlString)

parseAndCleanNumberStringThatShouldHaveBeenClean

public static java.lang.Number parseAndCleanNumberStringThatShouldHaveBeenClean(java.lang.String sqlString)

cleanStringBetter

public java.lang.String cleanStringBetter(java.lang.String sqlString)
We clean this string by clearing out all values that need to be escaped out. This method should be used on SQL strings that are to be used in SQL, such as field, table, and database names. For example, if the desired SQL String is: "SELECT * FROM "+TABLE-NAME+" WHERE "+FIELD+"='"+VALUE+"'" make sure to do: "SELECT * FROM "+cleanString(TABLE-NAME)+" WHERE "+cleanString(FIELD)+"='"+cleanStringValue(VALUE)+"'" IMPORTANT NOTE: After using this, make sure to use apostrophes around your VALUE field, not quotes! If you use " the user can still hack in using " to end the sentence. Only ' and \ are escaped. This is better than just a cleanString(String) because it checks what type of database we're using and if it's Microsoft Access, it substitutes _ instead of \\' which can sometimes confuse MS Access... It doesn't seem to care for the escapes on apostrophes.


cleanString

public static final java.lang.String cleanString(java.lang.String sqlString)
We clean this string by clearing out all values that need to be escaped out. This method should be used on SQL strings that are to be used in SQL, such as field, table, and database names. For example, if the desired SQL String is: "SELECT * FROM "+TABLE-NAME+" WHERE "+FIELD+"='"+VALUE+"'" make sure to do: "SELECT * FROM "+cleanString(TABLE-NAME)+" WHERE "+cleanString(FIELD)+"='"+cleanStringValue(VALUE)+"'" IMPORTANT NOTE: After using this, make sure to use apostrophes around your VALUE field, not quotes! If you use " the user can still hack in using " to end the sentence. Only ' and \ are escaped. Whenever possible, use the non-static version, cleanStringBetter(String).


checkSQLFieldValid

public boolean checkSQLFieldValid(java.lang.String sqlFieldName)
Throws a runtime exception if there is a bad field name. Should be used to check when users pass in a field name.


getDatabaseName

public java.lang.String getDatabaseName()
Returns the database connection string.

Returns:

setDatabaseName

public void setDatabaseName(java.lang.String newDatabaseName)

getDatabaseType

public int getDatabaseType()

getDatabaseProgramName

public java.lang.String getDatabaseProgramName()
Returns the name of this database program.

Returns:

getHelpURL

public java.lang.String getHelpURL()
Returns a URL with help about installing, starting, and stopping this database program.

Returns:

getMySqlDatabaseHost

public java.lang.String getMySqlDatabaseHost()
This isn't terribly accurate or adaptable right now. In the future it should be revised. TODO make smarter.

Returns:

getMySqlDatabaseName

public java.lang.String getMySqlDatabaseName()
This isn't terribly accurate or adaptable right now. In the future it should be revised. TODO make smarter.

Returns:

getMySqlDatabaseUser

public java.lang.String getMySqlDatabaseUser()
This isn't terribly accurate or adaptable right now. In the future it should be revised. TODO make smarter.

Returns:

getMySqlDatabasePassword

public java.lang.String getMySqlDatabasePassword()
This isn't terribly accurate or adaptable right now. In the future it should be revised. TODO make smarter.

Returns:

getSession

public javax.servlet.http.HttpSession getSession()

setHttpServletRequest

public void setHttpServletRequest(javax.servlet.http.HttpServletRequest request)

setPageContext

public void setPageContext(javax.servlet.jsp.PageContext context)
Specified by:
setPageContext in interface javax.servlet.jsp.tagext.Tag
Overrides:
setPageContext in class javax.servlet.jsp.tagext.TagSupport

setSendErrorMessages

public void setSendErrorMessages(boolean emailTheErrorMessages)

getLastExceptionThrown

public java.lang.Throwable getLastExceptionThrown()

setLastExceptionThrownToNull

public void setLastExceptionThrownToNull()

saveBugMessage

protected void saveBugMessage(java.lang.String error,
                              java.lang.Throwable e)

saveBugMessage

protected void saveBugMessage(java.lang.String error)

saveNotificationMessage

protected void saveNotificationMessage(java.lang.String error,
                                       java.lang.Throwable e)

saveNotificationMessage

protected void saveNotificationMessage(java.lang.String error)

isOpen

public boolean isOpen()

doConnection

public boolean doConnection()

initializeAtStartTag

public boolean initializeAtStartTag()

close

public void close()

finalize

public void finalize()
Overrides:
finalize in class java.lang.Object

doStartTag

public int doStartTag()
This function opens a connection to the requested database when the tag is first encountered

Specified by:
doStartTag in interface javax.servlet.jsp.tagext.Tag
Overrides:
doStartTag in class javax.servlet.jsp.tagext.TagSupport

doEndTag

public int doEndTag()
This function closes the connection when the ending tag is encountered

Specified by:
doEndTag in interface javax.servlet.jsp.tagext.Tag
Overrides:
doEndTag in class javax.servlet.jsp.tagext.TagSupport

doCatch

public void doCatch(java.lang.Throwable throwable)
Specified by:
doCatch in interface javax.servlet.jsp.tagext.TryCatchFinally

doFinally

public void doFinally()
Specified by:
doFinally in interface javax.servlet.jsp.tagext.TryCatchFinally

reinit

public void reinit()