求教!Integer.parseInt()将从页面获取的数据转型时老出错
这是报错
org.apache.jasper.JasperException: An exception occurred processing JSP page /register_deal.jsp at line 42
39: Integer price=Integer.parseInt("10");
40: String address=request.getParameter("address");
41: String telephone2=request.getParameter("telephone");
42: Integer telephone=Integer.parseInt(telephone2);
43: String email=request.getParameter("email");
44: String certification=request.getParameter("certification");
45: String certification2=request.getParameter("certification2");
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:515)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:426)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
这是我的源代码(初学,代码不规范,见谅!)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'register_deal.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url= "jdbc:mysql://localhost:3306/graduationproject?user=root&password=123&useUnicode=true&characterEncoding=GB2312";
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
request.setCharacterEncoding("gb2312");
String ICP=request.getParameter("ICP");
String name=request.getParameter("name");
String brief=request.getParameter("brief");
String material=request.getParameter("material");
String price2=request.getParameter("price");
Integer price=Integer.parseInt("10");
String address=request.getParameter("address");
String telephone2=request.getParameter("telephone");
Integer telephone=Integer.parseInt(telephone2);
String email=request.getParameter("email");
String certification=request.getParameter("certification");
String certification2=request.getParameter("certification2");
String sql="insert into supplier values ('"+ICP+"','"+name+"','"+brief+"','"+material+"','"+price+"','"+address+"','"+telephone+"','"+email+"','"+certification+"','"+certification2+"')";
Integer insertnumber=statement.executeUpdate(sql);
%>
<% if(insertnumber!=0)
{ %>
<p><b><font color="">注册成功</font></b></p>
<meta http-equiv="refresh" content="5;URL=index.jsp">
<% }
else
{ %>
<p><b><font color="#FF0000">注册失败</font></b></p>
<meta http-equiv="refresh" content="5;URL=register.jsp">
<% }
%>
</body>
</html>
我尝试过Integer.parseInt("price2"),Integer.parseInt(""+price2),Integer.parseInt("10"),只有最后这个没有报错,其他都报一样的错……请指教!
[解决办法]
有可能是你那字符串非 数字型字符串
Integer.parseInt("12") 得出的是 int
integer.valueof("13”); 得出的是integer
用integer.valueof("13”); 试下!!
[解决办法]
你debug下,看看telephone的值是多少。你是不是用空的电话号码测试的
[解决办法]
42: Integer telephone=Integer.parseInt(telephone2);
如果:telephone2="0431-12345678"就出错了,还有"0431-12345678转8080"这种情况
telephone不能用Integer.parseInt()转换
[解决办法]
把price2打印出来看看是不有数字以外的字符
[解决办法]