huvc.DBInterface
Class dBHelper

java.lang.Object
  |
  +--huvc.DBInterface.dBHelper

public class dBHelper
extends java.lang.Object

This class helps to connect to a database, and performs queries to that database. Also contains some utility classes

For example, to connect to a sql server database on machine called CHEETA:

dBHelper db = new dBHelper("jdbc:microsoft:sqlserver://CHEETA:1433", "com.microsoft.jdbc.sqlserver.SQLServerDriver", "user", "password");

To perform a query:

db.doQuery("SELECT * FROM STUDENT");

After this call, the cursor points to the first record.

To process all the records, for example, print to standard output:

do
{
System.out.print(db.getField("SID") + " ");
System.out.print(db.getField("LastName") + " ");
System.out.print(db.getField("FirstName") + " ");
System.out.print(db.getField("MiddleInitial") + " ");
System.out.print(db.getField("Password") + " ");
}while(db.moveNext());


Constructor Summary
dBHelper(java.lang.String _location, java.lang.String _driver)
          Creates connection to db
dBHelper(java.lang.String _location, java.lang.String _driver, java.lang.String user, java.lang.String pass)
          Creates connection to db with username and password
 
Method Summary
 void close()
          Closes both connection and resultset
 boolean doQuery(java.lang.String query)
          Performs a SELECT query on the database.When this is called the cursor is moved to the first record in the result set
 int doUpdate(java.lang.String query)
          Performs all other queries on database.
 java.lang.String getField(java.lang.String columnName)
          Gets the value of a field in the resultset as a string.
 byte[] getFieldAsByte(java.lang.String columnName)
           
 java.sql.Date getFieldAsDate(java.lang.String columnName)
          Gets the value of a field in the resultset as a java.sql.Date object.
 long getFieldAsInt(java.lang.String columnName)
          Gets the value of a field in the resultset as a long.
 double getFieldAsReal(java.lang.String columnName)
          Gets the value of a field in the resultset as a double.
 java.sql.Time getFieldAsTime(java.lang.String columnName)
          Gets the value of a field in the resultset as a java.sql.Time object.
 java.sql.ResultSet getResultSet()
          Gives caller reference to ResultSet.
 boolean moveFirst()
          Moves to the first record
 boolean moveLast()
          Moves to the last record
 boolean moveNext()
          Moves to the next record
 boolean movePrevious()
          Moves to the previous record
static java.lang.String quote(java.lang.String str)
          Utility Class.
Puts string in quotes.
 void reConnect()
          Used to open back connection, if it has been closed.
 void setField(java.lang.String column, java.lang.String value)
          Updates a column with a String value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

dBHelper

public dBHelper(java.lang.String _location,
                java.lang.String _driver)
Creates connection to db
Parameters:
_location - location of database - this is in the form jdbc:odbc:DBName if MS database. if remote, DBName = //host:port/name
_driver - driver of database - for example sun.jdbc.odbc.JdbcOdbcDriver

dBHelper

public dBHelper(java.lang.String _location,
                java.lang.String _driver,
                java.lang.String user,
                java.lang.String pass)
Creates connection to db with username and password
Parameters:
_location - location of database - this is in the form jdbc:odbc:DBName if MS database. if remote, DBName = //host:port/name
_driver - driver of database - for example sun.jdbc.odbc.JdbcOdbcDriver
user - username for database
pass - password for database
Method Detail

reConnect

public void reConnect()
Used to open back connection, if it has been closed. A connection to the database is reopened with the stored properties

close

public void close()
Closes both connection and resultset

doQuery

public boolean doQuery(java.lang.String query)
Performs a SELECT query on the database.When this is called the cursor is moved to the first record in the result set
Parameters:
query - query to be executed
Returns:
true, if query was sucessful, false otherwise

doUpdate

public int doUpdate(java.lang.String query)
Performs all other queries on database. When this is called the cursor is moved to the first record in the result set
Parameters:
query - query to be executed

moveNext

public boolean moveNext()
Moves to the next record
Returns:
true if done, false otherwise

movePrevious

public boolean movePrevious()
Moves to the previous record
Returns:
true if done, false otherwise

moveFirst

public boolean moveFirst()
Moves to the first record

Note: SQLServer driver does not support this method. If you need to go to the first element do:

Returns:
true if done, false otherwise

moveLast

public boolean moveLast()
Moves to the last record
Returns:
true if done, false otherwise

getField

public java.lang.String getField(java.lang.String columnName)
Gets the value of a field in the resultset as a string. When not sure what data type field is, call this method.
Returns:
null if err occurs

getFieldAsInt

public long getFieldAsInt(java.lang.String columnName)
Gets the value of a field in the resultset as a long. Use only when sure result will be an int
Returns:
-99999 if err occurs

getFieldAsReal

public double getFieldAsReal(java.lang.String columnName)
Gets the value of a field in the resultset as a double. Use only when sure result will be a real
Returns:
-99999.9 if err occurs

getFieldAsDate

public java.sql.Date getFieldAsDate(java.lang.String columnName)
Gets the value of a field in the resultset as a java.sql.Date object. Use only when sure result will be a date
Returns:
null if err occurs

getFieldAsTime

public java.sql.Time getFieldAsTime(java.lang.String columnName)
Gets the value of a field in the resultset as a java.sql.Time object. Use only when sure result will be a date
Returns:
null if err occurs

getFieldAsByte

public byte[] getFieldAsByte(java.lang.String columnName)

getResultSet

public java.sql.ResultSet getResultSet()
Gives caller reference to ResultSet. Please use carefully, and only when necessary. For example, when you need to use a ResultSet method that has not been wrapped by this class
Returns:
rs Reference to the ResultSet

setField

public void setField(java.lang.String column,
                     java.lang.String value)
Updates a column with a String value. The database is updated.

If you need to add another datatype, get reference to result set call updateXXX, then call updateRow()

Parameters:
column - column name
value - value to be added

quote

public static java.lang.String quote(java.lang.String str)
Utility Class.
Puts string in quotes. For example, if hello is passed, 'hello' is returned