javatools.db
Class DbInserter

java.lang.Object
  |
  +--javatools.db.DbInserter
Direct Known Subclasses:
DbReferencedInserter

public class DbInserter
extends java.lang.Object

A class used to insert records into SQL tables. The constructor is not public. To obtain a DbInserter call DbTable.inserter(); Example: To insert a record into the people table...

 DbDatabase db = ...;
 DbTable people = db.getTable("PEOPLE");
 DbInserter inserter = people.inserter();
 inserter.addColumn(people.getColumn("NAME"), "Fred"));
 inserter.addColumn(people.getColumn("FAVOURITE_TEAM"), "Raiders");
 inserter.addColumn(people.getColumn("AGE"), new Integer(30));
 int numberOfPeopleInserted = inserter.execute();
 
This is equivilent to...
 INSERT INTO PEOPLE(NAME, FAVOURITE_TEAM, AGE) VALUES('Fred', 'Raiders', 30)
 
The same thing as above can be achieved using a SELECT clause, and this can lead us to creating much more complex expressions...
 DbDatabase db = ...;
 DbSelector selector = db.selector();
 DbTable people = db.getTable("PEOPLE");
 DbInserter inserter = people.inserter(selector);
 inserter.addColumn(people.getColumn("NAME"), selector.addColumn("Fred")));
 inserter.addColumn(people.getColumn("FAVOURITE_TEAM"), selector.addColumn("Raiders"));
 inserter.addColumn(people.getColumn("AGE"), selector.addColumn(new Integer(30)));
 int numberOfPeopleInserted = inserter.execute();
 
This is equivilent to...
 INSERT INTO PEOPLE(NAME, FAVOURITE_TEAM, AGE) SELECT 'Fred', 'Raiders', 30
 
To get more fancy we can insert data that has been selected from another table. To insert all the people from the PLAYERS table into the PEOPLE table who are older than 20, and we set their favourite team to be the team they play for...
 DbDatabase db = ...;
 DbSelector selector = db.selector();
 DbTable people = db.getTable("PEOPLE");
 DbTable players = db.getTable("PLAYERS");
 DbInserter inserter = people.inserter(selector);
 inserter.addColumn(people.getColumn("NAME"), selector.addColumn(players.getColumn("NAME")));
 inserter.addColumn(people.getColumn("FAVOURITE_TEAM"), selector.addColumn(players.getColumn("TEAM")));
 inserter.addColumn(people.getColumn("AGE"), selector.addColumn(players.getColumn("AGE")));
 selector.setWhere(players.getColumn("AGE").greaterThan(new Integer(20)));
 int numberOfPeopleInserted = inserter.execute();
 
This is equivilent to...
 INSERT INTO PEOPLE(NAME, FAVOURITE_TEAM, AGE) SELECT NAME, TEAM, AGE FROM PLAYERS WHERE AGE > 20
 


Method Summary
 void addColumn(DbColumn into, java.lang.Object from)
          Specify the value of a column to add.
 int execute()
          Execute this command on the default connection.
 int execute(DbConnection dbcon)
          Execute this command on a specific connection.
 void setLists(java.util.List pIntoList, java.util.List pFromList)
          Directly sets intoList and fromList.
 int setSqlValues(java.sql.PreparedStatement stmt, int i)
          Puts data into a prepared statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setSqlValues

public int setSqlValues(java.sql.PreparedStatement stmt,
                        int i)
                 throws DbException,
                        java.sql.SQLException
Puts data into a prepared statement.

Parameters:
stmt - The prepared statement.
i - An index (obscure).
Returns:
An index, obscure.
Throws:
DbException - If something goes wrong.
java.sql.SQLException - If something goes wrong.

addColumn

public void addColumn(DbColumn into,
                      java.lang.Object from)
Specify the value of a column to add.

Parameters:
into - The column we are inserting into.
from - The column from a selector that we are getting a value from.

setLists

public void setLists(java.util.List pIntoList,
                     java.util.List pFromList)
Directly sets intoList and fromList.

Parameters:
pIntoList - The list containing the columns to put data into.
pFromList - The list containing the data that will be put.

execute

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

Parameters:
dbcon - The connection to use.
Returns:
The number of record affected.
Throws:
DbException - If something goes wrong.

execute

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

Returns:
The number of record affected.
Throws:
DbException - If something goes wrong.