本文整理匯總了Java中org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject方法的典型用法代碼示例。如果您正苦於以下問題:Java NamedParameterJdbcTemplate.queryForObject方法的具體用法?Java NamedParameterJdbcTemplate.queryForObject怎麽用?Java NamedParameterJdbcTemplate.queryForObject使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
的用法示例。
在下文中一共展示了NamedParameterJdbcTemplate.queryForObject方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: tableExists
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; //導入方法依賴的package包/類
public static boolean tableExists(NamedParameterJdbcTemplate namedParameterJdbcTemplate, String tableName) throws PostgresException {
String sql = "SELECT EXISTS (\n"
+ " SELECT 1 \n"
+ " FROM pg_catalog.pg_class c\n"
+ " JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n"
+ " WHERE n.nspname = 'public'\n"
+ " AND c.relname = :table_name\n"
+ ")";
SqlParameterSource paramSource = new MapSqlParameterSource("table_name", tableName);
try {
return namedParameterJdbcTemplate.queryForObject(sql, paramSource, Boolean.class);
} catch (DataAccessException e) {
throw new PostgresException(String.format("Error while checking if table [%s] exists", tableName), e);
}
}
示例2: removeTestFormData
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; //導入方法依賴的package包/類
private void removeTestFormData() {
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dataSource);
int formId = -1;
try {
formId = template.queryForObject("select formid from form where formid=:formid",
new MapSqlParameterSource("formid", 9999), Integer.class);
} catch(Exception e) {
System.out.println("Failed to get formId, not removing form!");
return;
}
System.out.println("\n!!!Retrieve test form ID result: " + formId);
int formResult = template.update("delete from form where formid=:formid",
new MapSqlParameterSource("formid", formId));
System.out.println("\n!!!Remove test Form result: " + formResult);
}
示例3: selecteerMinMaxIdVoorSelectie
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; //導入方法依賴的package包/類
@Override
public MinMaxPersoonCacheDTO selecteerMinMaxIdVoorSelectie() {
final String sql
= "select min(id) as laag, max(id) as hoog from kern.perscache";
final NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(selectieDataSource);
final Map<String, Object> parameters = new HashMap<>();
return jdbcTemplate.queryForObject(sql, parameters, (resultSet, i) -> {
final MinMaxPersoonCacheDTO minMaxPersoonCacheDTO = new MinMaxPersoonCacheDTO();
minMaxPersoonCacheDTO.setMinId(resultSet.getInt("laag"));
minMaxPersoonCacheDTO.setMaxId(resultSet.getInt("hoog"));
return minMaxPersoonCacheDTO;
});
}
示例4: removeTestUserData
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; //導入方法依賴的package包/類
private void removeTestUserData() {
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dataSource);
long userId = -1;
try {
userId = template.queryForObject("select userid from public.user where username=:username",
new MapSqlParameterSource("username", "[email protected]"), Long.class);
} catch(Exception e) {
System.out.println("Failed to get userId, not removing any other test data");
return;
}
System.out.println("\n!!!Retrieve test user ID result: " + userId);
if(userId < 1) {
System.out.println("Invalid userId: " + userId + ", not removing any further test data");
return;
}
int contactResult = template.update("delete from contact where userid=:userid",
new MapSqlParameterSource("userid", userId));
System.out.println("\n!!!Remove test Contacts result: " + contactResult);
int userorgResult = template.update("delete from userorg where userid=:userid",
new MapSqlParameterSource("userid", userId));
System.out.println("\n!!!Remove userorgs result: " + userorgResult);
int userResult = template.update("delete from public.user where username=:username",
new MapSqlParameterSource("username", "[email protected]"));
System.out.println("\n!!!Remove test user result: " + userResult);
// TODO: remove contacts and userorg, etc
}