|
|
爱看球的蟠桃 · PS如何制作行星光晕效果?(上篇)-百度经验· 3 月前 · |
|
|
睡不着的楼房 · sama地下偶像1 - 抖音· 1 年前 · |
|
|
愤怒的豆芽 · 什么是 Visual Studio ...· 1 年前 · |
|
|
豪气的冲锋衣 · 国务院侨办主任裘援平会见香港潮州商会访问团· 1 年前 · |
|
|
有胆有识的油条 · 用真心践行“师德” 用行动追求梦想-文学院官网· 1 年前 · |
我有以下
SQL Query :
SELECT COUNT(*) FROM DOG where ID = 'SampleId';
我正在尝试用
java
写这段代码:
public int returnCountOfDogTable(String id){
String sql= "SELECT COUNT(*) FROM DOG WHERE ID =:id";
Query query = persistence.entityManager().createNativeQuery(sql);
query.setParameter("id", id);
List<Integer> resultList = query.getResultList();
int result = resultList.get(0);
return result;
}
然而,我得到了这个异常:
java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer
我该如何解决这个问题呢?
简单地尝试一下:
public int returnCountOfDogTable(String id) {
//...
List<BigDecimal> resultList = query.getResultList();
BigDecimal result = resultList.get(0);
return result.toIntValue();
}
您还可以使用
Number
并调用
intValue()
Query query = entityManager.createNativeQuery("SELECT COUNT(*) FROM DOG WHERE ID =:id");
query.setParameter("id", 1);
int count = ((Number) query.getSingleResult()).intValue();
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@PersistenceContext
protected EntityManager em;
public int getCountQuery(String sql) {
int resultCount = 0;
try {
Query query = em.createNativeQuery(sql);
|
|
爱看球的蟠桃 · PS如何制作行星光晕效果?(上篇)-百度经验 3 月前 |
|
|
睡不着的楼房 · sama地下偶像1 - 抖音 1 年前 |
|
|
豪气的冲锋衣 · 国务院侨办主任裘援平会见香港潮州商会访问团 1 年前 |
|
|
有胆有识的油条 · 用真心践行“师德” 用行动追求梦想-文学院官网 1 年前 |