javatools.db
Class DbUpdater

java.lang.Object
  |
  +--javatools.db.DbUpdater

public class DbUpdater
extends java.lang.Object

A class used to update records from SQL tables. The constructor is not public. To obtain a DbUpdater call DbTable.updater(); Example: To update all the people who are (younger than 18 or older than 80) and whose name is "Fred"... to have a favourite_team of "Raiders";

 DbDatabase db = ...;
 DbTable people = db.getTable("PEOPLE");
 DbUpdater updater = people.deleter();
 updater.addColumn(people.getColumn("FAVOURITE_TEAM"), "Raiders");
 updater.setWhere(people.getColumn("AGE").lessThan(new Integer(18)).or(
    people.getColumn("AGE").greaterThan(new Integer(80))).and(
    people.getColumn("NAME").equal("FRED"));
 int numberOfPeopleUpdated = updater.execute();
 
This is equivilent to...
 UPDATE PEOPLE SET FAVOURITE_TEAM='Raiders' WHERE (AGE < 18 OR AGE > 80 ) AND NAME='Fred'
 
Note the use of equal(), NOT equals(). To get more fancy, to update the same group of people to have a favourite team the same as Bill's team, we use a sub-select...
 DbDatabase db = ...;
 DbTable people = db.getTable("PEOPLE");
 DbSelector bills_team = db.selector();
 bills_team.addColumn(people.getColumn("FAVOURITE_TEAM"));
 bills_team.setWhere(people.getColumn("NAME").equal("BILL"));
 DbUpdater updater = people.deleter();
 updater.addColumn(people.getColumn("FAVOURITE_TEAM"), bills_team);
 updater.setWhere(people.getColumn("AGE").lessThan(new Integer(18)).or(
    people.getColumn("AGE").greaterThan(new Integer(80))).and(
    people.getColumn("NAME").equal("FRED"));
 int numberOfPeopleUpdated = updater.execute();
 
This is equivilent to...
 UPDATE PEOPLE SET FAVOURITE_TEAM=(SELECT FAVOURITE_TEAM FROM PEOPLE WHERE NAME='Bill')
    WHERE (AGE < 18 OR AGE > 80 ) AND NAME='Fred'
 


Field Summary
(package private)  java.util.List fromList
           
(package private)  java.util.List intoList
           
(package private)  DbTable table
           
(package private)  DbExpr where
           
 
Constructor Summary
(package private) DbUpdater(DbTable table)
           
 
Method Summary
 void addColumn(DbColumn into, java.lang.Object from)
          Add a column specification to update.
 int execute()
          Execute this command on the default connection.
 int execute(DbConnection dbcon)
          Execute this delete command on a specific connection.
(package private)  java.lang.String getQueryString()
           
(package private)  int setSqlValues(java.sql.PreparedStatement stmt, int i)
           
 void setWhere(DbExpr where)
          Set the where condition on which records to update.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

table

DbTable table

intoList

java.util.List intoList

fromList

java.util.List fromList

where

DbExpr where
Constructor Detail

DbUpdater

DbUpdater(DbTable table)
Method Detail

setWhere

public void setWhere(DbExpr where)
Set the where condition on which records to update.

Parameters:
where - The new where value

addColumn

public void addColumn(DbColumn into,
                      java.lang.Object from)
Add a column specification to update. The new value can either be a raw value - Integer, String, java.sql.Date etc. Or it can be a DbSelector in the case of a sub-select.

Parameters:
into - The column to update.
from - The new value.

execute

public int execute(DbConnection dbcon)
            throws DbException
Execute this delete command on a specific connection.

Parameters:
dbcon - Description of Parameter
Returns:
The number of record affected.
Throws:
DbException - Description of Exception

execute

public int execute()
            throws DbException
Execute this command on the default connection.

Returns:
The number of record affected.
Throws:
DbException - Description of Exception

setSqlValues

int setSqlValues(java.sql.PreparedStatement stmt,
                 int i)
           throws DbException,
                  java.sql.SQLException
DbException
java.sql.SQLException

getQueryString

java.lang.String getQueryString()
                          throws DbException
DbException