Jsp中值的NumberFormatException

时间:2014-07-02 14:56:13

标签: java jsp

我将值从JSP传递到java类以将其存储在Database中。它为Jsp中的Contact No.值提供NumberFormatException。我无法弄清楚为什么会这样。对于从Jsp传递给Java类的Contact No.值,java类中从字符串到整数的解析以某种方式失败。

JSP

<%@page import="register.register"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>PenguinSoft(India)</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" />
</head>
<body>
     <%



                        String stdname = request.getParameter("student");
                        String batch = request.getParameter("batch");
                                                String email = request.getParameter("email");
                                                String contact = request.getParameter("contact");
                                                String fees=request.getParameter("fees");
                        if(stdname!=null && batch!=null && email!=null && contact!=null && fees!=null)
                                                {

                            register objadd = new register();
                            boolean flag=objadd.InsertNew(stdname,batch,email,contact,fees);
                            if(flag)
                                                        {
                                                            System.out.println("Yes");
                                                        }
                                                }       

                    %>
<form action="Register.jsp" method="POST">

<div id="header">
    <div id="logo">
        <h1><a href="#"></a></h1>
        <h3><a href="#">PenguinSoft</a></h3>
    </div>
    <div id="menu">
        <ul>
            <li class="first"><a href="#" title="">New Batch</a></li>
            <li><a href="Login.jsp" title="">Register Student</a></li>
            <li><a href="AddEmployeeData.jsp" title="">Fees Record</a></li>
            <li><a href="AddKeywords.jsp" title="">Manage Student</a></li>
            <li><a href="StopWordDetectionForm.jsp" title="">All Details</a></li>
        </ul>
    </div>
    <div id="ad468x60"><a href="#"><img src="images/spam1.gif" alt="" width="468" height="60" /></a></div>
</div>
<div id="page">
    <div id="content">
        <div class="boxed">
            <h1 class="heading">Welcome to SPAM Management</h1>
            <div class="content">
                <img src="images/no-spam-2x.gif" alt="" width="122" height="123" class="left" />
                <p></p>
                <p></p>



<blockquote>


<p align="left"><font size="3">Student Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font>
<input type="text" name="student" size="26" style="font-family: Tahoma; font-size: 10pt" onBlur="return one(this.form)"/></p>
<p align="left"><font size="3">Batch<select name="batch">
            <option>Java</option>
            <option>PHP</option>
            <option>Framework</option>
            <option>Android</option>
            </select>
</font>
</p>
<p align="left"><font size="3">Email ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font>
<input type="text" name="email" size="26" style="font-family: Tahoma; font-size: 10pt" onBlur="return one(this.form)"/></p>
<p align="left"><font size="3">Contact No.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font>
<input type="text" name="contact" size="26" style="font-family: Tahoma; font-size: 10pt" onBlur="return one(this.form)"/></p>
 <p align="left"><font size="3">Fees&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font>
<input type="text" name="fees" size="26" style="font-family: Tahoma; font-size: 10pt" onBlur="return one(this.form)"/></p>   
<p align="left">
</p><br>
<input type="submit" value="Register Student"name="submit" align="center"/>

</form>
            </div>
        </div>
    </div>
    <!-- end #content -->
    <div id="sidebar">
        <div class="boxed">
            <h2 class="heading">Key Features: </h2>
            <div class="content">
                <ul>
                    <li>PenguinSoft(India) is one of the major leading training institute in Software Development in India.</li>
                    <li>Training provided in Java,PHP,Android,database,etc</li>
                    <li>Live project development is also provided</li>
                </ul>
            </div>
        </div>
    </div>
    <!-- end #sidebar -->
</div>
<!-- end #page -->
<div style="clear: both;">&nbsp;</div>
<div id="footer">
    <p id="legal">Copyright &copy; PenguinSoft(India) All Rights Reserved.</p>
    <p id="links"><a href="#">Privacy Policy</a> | <a href="#">Terms of Use</a></p>*/
</div>
</body>
</html>

Java类

package register;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author asus
 */
public class register {


    public boolean InsertNew(String stdname,String stdbatch,String email,String contact,String fees){
          boolean flag = false;
          Connection con = null;
          String connectionURL = "jdbc:mysql://localhost:3306/penguinsoft";

        Connection connection = null;
        ResultSet rs;       

        try {
            // Load the database driver
            Class.forName("com.mysql.jdbc.Driver");
            // Get a Connection to the database
            connection = DriverManager.getConnection(connectionURL, "root", "");
            //Add the data into the database

            //String sql = "insert into sample values(?,?)";
             int stdcontact=Integer.parseInt(contact);
                int stdfees=Integer.parseInt(fees);
            PreparedStatement pst = connection.prepareStatement("INSERT into alldetails VALUES(?,?,?,?,?,?)");

            pst.setString(1,null);
            pst.setString(2,stdname);
            pst.setString(3,email);
            pst.setInt(4,stdcontact);
            pst.setString(5,stdbatch);
            pst.setInt(6,stdfees);


            int numRowsChanged = pst.executeUpdate();
            if(numRowsChanged!=0){
                System.err.println("Successfull execution");
            }
            else{
                            System.err.println("Problem with the insertion query");
            }
            pst.close();
            }
            catch(ClassNotFoundException e){
                            e.printStackTrace();
            }
            catch(SQLException e){
                            e.printStackTrace();
            }
                catch(NumberFormatException nfe)
                {
                    nfe.printStackTrace();
                }
            catch (Exception e){
                            e.printStackTrace();
            }

            finally {
            // Always close the database connection.
            try {
            if (connection != null) connection.close();
            }
            catch (SQLException ignored){
            }
            }

           return true;
         }   
}

错误:

java.lang.NumberFormatException: For input string: "12345678"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:495)
    at java.lang.Integer.parseInt(Integer.java:527)
    at register.register.InsertNew(register.java:37)
    at org.apache.jsp.Register_jsp._jspService(Register_jsp.java:89)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

3 个答案:

答案 0 :(得分:0)

这可能是由于字符串中的一些垃圾/特殊字符造成的。尝试做

Integer.parseInt(source.trim());

而不仅仅是

Integer.parseInt(source);

答案 1 :(得分:0)

看起来像char编码问题。

你可以尝试的一项工作是

       String cleanString = new String(myNum.trim().getBytes("UTF-8"));
       int stdcontact=Integer.parseInt(cleanString);

答案 2 :(得分:0)

 long stdcontact=Long.parseLong(contact);

contactno超出了32位int的范围。