|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javatools.db.DbInserter
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', 30To 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 |
public int setSqlValues(java.sql.PreparedStatement stmt, int i) throws DbException, java.sql.SQLException
stmt
- The prepared statement.i
- An index (obscure).
DbException
- If something goes wrong.
java.sql.SQLException
- If something goes wrong.public void addColumn(DbColumn into, java.lang.Object from)
into
- The column we are inserting into.from
- The column from a selector that we are getting a value from.public void setLists(java.util.List pIntoList, java.util.List pFromList)
pIntoList
- The list containing the columns to put data into.pFromList
- The list containing the data that will be put.public int execute(DbConnection dbcon) throws DbException
dbcon
- The connection to use.
DbException
- If something goes wrong.public int execute() throws DbException
DbException
- If something goes wrong.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |