| public interface class java.sql Update
|
Java SE 6 |
Update annotation must not be used in-conjunction with a SQL statement
that returns a ResultSet.
A Query interface may contain zero, one or more methods decorated with an Update
annotation.
The following provides an example of using the Update annotation.
// Define DataSet type
public class Mammal {
public String name;
public String description;
public int age;
public int weight;
}
// Define the interface containing the methods representing SQL statements
// that can be invoked.
interface MyQueries extends BaseQuery {
@Select("select name, description, age from mammal")
DataSet<Mammal> getAllMammals();
@Update("delete from mammal")
int deleteAllMammals();
}
To invoke the deleteAllMammals() method, an instance of
the MyQueries interface must be created by invoking either the
Connnection.createQueryObject or DataSource.createQueryObject
method.
MyQueries mq = con.createQueryObject(MyQueries.class); int success = mq.deleteAllMammals();
sql annotation element allows developers to specify parameter markers
similar to PreparedStatements. Parameter markers are defined as:
Update
annotation. If this occurs, a SQLRuntimeException will be thrown.
sql
annotation element.
interface MyQueries extends BaseQuery {
@Update(sql="update mammal set description= ?1 where age <10")
int setDescription(String desc);
}
MyQueries mq = con.createQueryObject(MyQueries.class);
int success = mq.setDescription("a young mammal");
When the setDescription() method is invoked, the value specified
for the parameter desc will be used as the value for the parameter
marker.
AutoGeneratedKeys annotation is used
to indicate that a Dataset data class will be used to store
any auto generated keys that occur as the result of executing a method decorated
with an Update annotation. The data class may contain one or more fields
that comprise the auto generated key. The Update annotation element
keys would be set to a value of
GeneratedKeys.RETURNED_KEYS_DRIVER_DEFINED to indicate that the
JDBC driver will determine the columns to return to represent the auto-generated keys.
A value of GeneratedKeys.RETURNED_KEYS_COLUMNS_SPECIFIED for the
keys annotation element indicates that the columns contained in
the data class decorated by the AutoGeneratedKeys annotation
are returned as the auto generated keys from the method invocation.
In the above example, the@AutoGeneratedKeys public class TabKeys { public String col1; } public interface MyQueries extends BaseQuery{@Update(sql="insert into tabName(?1, ?2)", keys=GeneratedKeys.RETURNED_KEYS_DRIVER_DEFINED) DataSet<TabKeys> addPerson(String name, int age); }
MyQueries.addPerson method indicates that
it can return auto generated keys upon its execution. The returned auto generated
keys may be accessed as follows:
MyQueries mq = con.createQueryObject(MyQueries.class);
DataSet<TabKeys> keys = mq.addPerson("Jane Doe", 29);
for (TabKeys key : keys) {
System.out.println("Key=" + key.col1 );
}
Update annotation can return the number of
rows affected by the method invocation by specifying a return type of int.
If a return type of void is specified, an update count will not be
returned.
interface MyQueries extends BaseQuery {
@Update(sql="update mammal set weight = 5 where weight > ?1")
int shrinkBigMammals(int weight);
}
A returned value of 0 indicates that no rows were updated. A returned
value greater than 0 indicates that 1 or more rows were affected
by the method invocation.
| since | 1.6 |
| Optional Elements | |||||
|---|---|---|---|---|---|
| abstract public GeneratedKeys | keys() Details
Determines whether auto-generated keys are returned or not
| ||||
| abstract public String | sql() Details
The SQL command to execute. The SQL command that is specified can return
an update count. The SQL command must not return a result set.
Note: If the
| ||||
| abstract public String | value() Details
The SQL command to execute. The SQL command that is specified can return
an update count. The SQL command must not return a result set.
Note: If the
| ||||
| About DocWeb · Bundles · Export · Export All | Top 10 · Statistics · Login |
| About Sun · Contact · Privacy · Terms of Use · Trademarks | Java SE 6 · Copyright © 1994-2009 Sun Microsystems, Inc.All rights reserved. Use is subject to license terms |
![]() |
![]() |
|