Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

javatools.db
Class DbInserter  view DbInserter download DbInserter.java

java.lang.Object
  extended byjavatools.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
 

Version:
0.2.0

Field Summary
(package private)  java.util.List fromList
          The list of values that will be stored.
(package private)  java.util.List intoList
          The list of columns in which data will be stored.
(package private)  DbSelector selector
          The selector that can be used to add data.
(package private)  DbAbstractTable table
          The table to insert values into.
 
Constructor Summary
(package private) DbInserter(DbAbstractTable table)
          Creates a new inserter.
(package private) DbInserter(DbAbstractTable table, DbSelector selector)
          Creates a new DbInserter.
 
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.
(package private)  java.lang.String getQueryString()
          Returns the complete query string.
(package private)  java.lang.String getValuesQueryString()
          Returns the sub-query-string for VALUES clause.
 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
 

Field Detail

table

DbAbstractTable table
The table to insert values into.


selector

DbSelector selector
The selector that can be used to add data. It can be null.


intoList

java.util.List intoList
The list of columns in which data will be stored.


fromList

java.util.List fromList
The list of values that will be stored.

Constructor Detail

DbInserter

DbInserter(DbAbstractTable table,
           DbSelector selector)
Creates a new DbInserter.


DbInserter

DbInserter(DbAbstractTable table)
Creates a new inserter.

Method Detail

setSqlValues

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


addColumn

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


setLists

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


execute

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


execute

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


getValuesQueryString

java.lang.String getValuesQueryString()
Returns the sub-query-string for VALUES clause.


getQueryString

java.lang.String getQueryString()
                          throws DbException
Returns the complete query string.