为什么我总是得到numberformatexception? 我尝试将数据库中的值检索到我的jsp。 我想在我的jsp中查看我的连接表。
我有2个实体
项目类
public class Item {
@Id
@Column(name="p_id")
private String pid;
@Column(name="p_name")
private String pname;
@Column(name="catId")
private String catid;
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getCatid() {
return catid;
}
public void setCatid(String catid) {
this.catid = catid;
}}
类别类
public class Category {
@Id
@Column(name="c_id")
private int cid;
@Column(name="catId")
private String catid;
@Column(name="c_name")
private String cname;
@Column(name="c_brand")
private String cbrand;
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCbrand() {
return cbrand;
}
public void setCbrand(String cbrand) {
this.cbrand = cbrand;
}
public String getCatid() {
return catid;
}
public void setCatid(String catid) {
this.catid = catid;
}}
我的Item类中的btw catId是Category的外键。
我尝试在我的DaoImpl中创建hql并加入两个表,看起来像这样
public List<Item> getItem() {
EntityManager entityManager = transactionManager.getEntityManagerFactory().createEntityManager();
Query sqlQuery = entityManager.createQuery("SELECT i.pname,c.cname,c.cbrand FROM Item i,Category c where i.catid= c.catid");
List<Item>iList = sqlQuery.getResultList();
if (entityManager.isOpen())
entityManager.close();
return iList;
}
@Controller
@RequestMapping(value = "/list")
public String result(HttpServletRequest request, ModelMap map) {
List<Item> iList = catService.getItem();
map.addAttribute("iList", iList);
return "new/list";
}
@my view jsp看起来像这样
<c:if test="${not empty iList}">
<table border="1">
<tr>
<th>Product name</th>
<th>Category</th>
<th>Brand</th>
<c:forEach var="p" items="${iList}">
<td>${p.pname}</td>
<td>${p.cname}</td><td>${p.cbrand}</td>
</c:forEach>
</tr>
</table>
</c:if>
我总是得到这个错误:
Stacktrace:] with root cause
java.lang.NumberFormatException: For input string: "pname"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at javax.el.ArrayELResolver.coerce(ArrayELResolver.java:158)
at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:45)
at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)
at org.apache.jsp.pages.new_.list_jsp._jspx_meth_c_005fforEach_005f0(list_jsp.java:243)
at org.apache.jsp.pages.new_.list_jsp._jspx_meth_c_005fif_005f0(list_jsp.java:204)
at org.apache.jsp.pages.new_.list_jsp._jspService(list_jsp.java:98)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
有人可以帮我解决这个问题吗?提前谢谢!
答案 0 :(得分:1)
我不知道这是一个好的做法还是对,但这段代码有效......
public List<Customer> getView( int startPage, int maxResults) {
EntityManager entityManager = transactionManager.getEntityManagerFactory().createEntityManager();
Session session = entityManager.unwrap(Session.class);
String sql = "SELECT customer.firstname, customer.lastname, customer.address, account.ano, account.bank FROM customer, account WHERE customer.cid = account.cid";
SQLQuery query = session.createSQLQuery(sql);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List<Customer> vList = query.list();
if (entityManager.isOpen())
entityManager.close();
// session.close();
return vList;
}