The TableUtil class is a descendant of JdbcUtil. This class allows you to interact with database tables in a similar fashion to a persistence model such as JPA, however this is not a persistence model.
The TableUtil class (or its descendants) allow for the developer to manage rows in a table without writing SQL statements. A model object (POJO) is created for each table; the only requirement to make it work is the presence of the @JdbcTable annotation (described below).
:::java
package com.example.model;
import java.io.Serializable;
import java.util.Date;
import com.roth.jdbc.annotation.JdbcTable;
import com.roth.jdbc.annotation.PermissiveBinding;
import com.roth.jdbc.model.StateBean;
@JdbcTable(name = "example", // (1)
primaryKeyColumns = {"example_id"}) // (2)
@PermissiveBinding
public class ExampleBean implements Serializable, StateBean {
private static final long serialVersionUID = 1L;
private Long exampleId;
// More fields implied
public Long getExampleId() { return exampleId; } // (3)
public void setExampleId(Long exampleId) { this.exampleId = exampleId; }
// More getters and setters implied
@Override
public boolean isNew() { // (4)
boolean result = exampleId == null;
// Do whatever needs to be done every time here.
if (result) {
// Do whatever needs to be done when the record is new here.
}
return result;
}
}
Column names from the table are referenced in lower case. This makes the interaction uniform across different DBMSs. The Java equivalent of a column name is the same string, but with underscores removed, and all characters that followed underscores up-cased. When looking for the getter or setter, the first character is up-cased, and the word "get" or "set" is prepended.
Example:
example_id translates to getExampleId and setExampleId
customer_type_desc translates to getCustomerTypeDesc and setCustomerTypeDesc
address_line_1 translates to getAddressLine1 and setAddressLine1
The methods that TableUtil provides (aside from those inherited from JdbcUtil) are:
get
The get method retrieves a singular POJO for the requested record.
:::java
public <T> T get(Class<?> beanClass, String where) throws SQLException;
public <T> T get(Class<?> beanClass, String where, Object bean) throws SQLException;
// Examples:
ExampelBean bean = get(ExampleBean.class, "example_id = 1"); // (1)
ExampelBean bean = get(ExampleBean.class, "example_id = {exampleId}", searchBean); // (2)
getList
The getList method retrieves multiple POJOs in a List for the requested records.
:::java
public <T> T getList(Class<?> beanClass) throws SQLException;
public <T> T getList(Class<?> beanClass, String where) throws SQLException;
public <T> T getList(Class<?> beanClass, String where, Object bean) throws SQLException;
public <T> T getList(Class<?> beanClass, String where, String order, Object bean) throws SQLException;
// Examples:
List<ExampelBean> beans = getList(ExampleBean.class); // (1)
List<ExampelBean> beans = getList(ExampleBean.class, "example_id <= 10"); // (2)
List<ExampelBean> beans = getList(ExampleBean.class, "example_id = {exampleId}", searchBean); // (3)
List<ExampelBean> beans = getList(ExampleBean.class, "color = {color}", "example_id", searchBean); // (4)
List<ExampelBean> beans = getList(ExampleBean.class, null, "example_id", null); // (5)
example_id.example_id.insert
The insert method inserts one or more rows into one or more tables.
:::java
public int insert(Object bean) throws SQLException;
public int[] insert(Object[] beans) throws SQLException;
public int[] insert(Collection<?> beans) throws SQLException;
Examples:
ExampleBean bean = ...;
int rowsAffected = insert(bean); // (1)
StateBean[] beans = ...;
int[] rowsAffected = insert(beans); // (2)
List<StateBean> beans = ...;
int[] rowsAffected = insert(beans); // (3)
update
The update method updates one or more rows in one or more tables.
:::java
public int update(Object bean) throws SQLException;
public int[] update(Object[] beans) throws SQLException;
public int[] update(Collection<?> beans) throws SQLException;
Examples:
ExampleBean bean = ...;
int rowsAffected = update(bean); // (1)
StateBean[] beans = ...;
int[] rowsAffected = update(beans); // (2)
List<StateBean> beans = ...;
int[] rowsAffected = update(beans); // (3)
save
The save method inserts or updates one or more rows in one or more tables. Unlike the insert and update methods, the save method requires that the POJO implement the StateBean interface. The isNew method is used to determine whether to insert or update.
:::java
public int save(Object bean) throws SQLException;
public int[] save(Object[] beans) throws SQLException;
public int[] save(Collection<?> beans) throws SQLException;
Examples:
ExampleBean bean = ...;
int rowsAffected = save(bean); // (1)
StateBean[] beans = ...;
int[] rowsAffected = save(beans); // (2)
List<StateBean> beans = ...;
int[] rowsAffected = save(beans); // (3)
delete
The delete method deletes one or more rows from one or more tables.
:::java
public int delete(Object bean) throws SQLException;
public int[] delete(Object[] beans) throws SQLException;
public int[] delete(Collection<?> beans) throws SQLException;
Examples:
ExampleBean bean = ...;
int rowsAffected = delete(bean); // (1)
StateBean[] beans = ...;
int[] rowsAffected = delete(beans); // (2)
List<StateBean> beans = ...;
int[] rowsAffected = delete(beans); // (3)
getMap
The getMap method gets a LinkedHashMap<String,String> from a specified table. This method is useful when used in conjunction with the select tag in JSPs.
:::java
public LinkedHashMap<String,String> getMap(String table, String key, String value) throws SQLException;
public LinkedHashMap<String,String> getMap(String table, String key, String value, String order) throws SQLException;
public LinkedHashMap<String,String> getMap(String table, String key, String value, String order, String where) throws SQLException;
Examples:
LinkedHashMap<String,String> options = getMap("customer_type", "customer_type_id", "description"); // (1)
LinkedHashMap<String,String> options = getMap("customer_type", "customer_type_id", "description", "description"); // (2)
String filter = applyParameters("cusotmer_cat_id = {1}", customerCatId);
LinkedHashMap<String,String> options = getMap("customer_type", "customer_type_id", "description", "description", filter); // (3)