Java Tutorial/Database/JDBC Annotation

Материал из Java эксперт
Перейти к: навигация, поиск

JDBC Annotations

The following table lists the Select annotation elements.

ElementTypeAccessibilitysql or valueStringThe SQL statement to be executedtableNameStringThe name of the table that will be updated when the DataSet.sync method is calledreadOnlybooleanIndicates the returned DataSet is read-onlyconnectedbooleanIndicates if the DataSet is connected to the data sourceallColumnsMappedbooleanIndicates if there is a one-to-one mapping between the column names in the SQL statement and the fields in the returned DataSetscrollablebooleanIndicates if the returned DataSet is scrollable. This element only takes effect only when in connected mode.



   <source lang="java">

import java.sql.BaseQuery; import java.sql.DataSet; import java.sql.Select; public interface UserQueries extends BaseQuery {

   // Select all users
   @Select (sql ="SELECT userId, firstName, lastName FROM Users",
            readOnly=false, connected=false, tableName="Users")
   DataSet<User> getAllUsers ();
   // Select user by name */
   @Select (sql ="SELECT userId, firstName, lastName FROM Users"
            + "WHERE userName=?", readOnly=false, connected=false,
            tableName ="Users")
   DataSet<User> getUserByName(String userName);

}</source>





Update annotation

The Update annotation is used to decorate a method specifying an SQL update statement.

The method"s return value can be an int or void.

Only if the return value type is int will the update count be returned.

ElementTypeAccessibilitysql or valueStringThe SQL statement to be executedkeysGenerated KeysIndicates if autogenerated keys are returned. The default value is GeneratedKeys.NO_KEYS_RETURNED



   <source lang="java">

import java.sql.BaseQuery; import java.sql.DataSet; import java.sql.Select; public interface UserQueries extends BaseQuery {

   // Delete user
   @Update ("DELETE Users WHERE firstName={firstName}" +
            "AND lastName={lastName}")
   int deleteUser (String firstName, String lastName);

}</source>