我正在尝试将一些值存储在MySql数据库中。该值取自jsp页面。但是解析似乎有些问题,我无法弄清楚。这是我的代码
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>
<%//**The Problem is Somewhere in this part of code**
String stdname = request.getParameter("student");
String batch = request.getParameter("batch");
String email = request.getParameter("email");
String contact = request.getParameter("contact");
String msg;
String fees=request.getParameter("fees");
int stdcontact=Integer.parseInt(contact);
int stdfees=Integer.parseInt(fees);
if (stdname!= null ) {
register objadd = new register();
boolean flag = objadd.InsertNew(stdname,batch,email,stdcontact,stdfees);
if (flag) {
request.getRequestDispatcher("success.jsp").forward(request, response);
} else {
msg = "Invalid data entered!";
}
}
%>
<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
</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
</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.
</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
</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;"> </div>
<div id="footer">
<p id="legal">Copyright © 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 Class
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,int contact,int 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(?,?)";
PreparedStatement pst = connection.prepareStatement("INSERT into alldetails VALUES(?,?,?,?,?,?)");
pst.setString(1,null);
pst.setString(2,stdname);
pst.setString(3,email);
pst.setInt(4,contact);
pst.setString(5,stdbatch);
pst.setInt(6,fees);
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 (Exception e){
e.printStackTrace();
}
finally {
// Always close the database connection.
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
}
}
return true;
}
}
错误
HTTP Status 500 - An exception occurred processing JSP page /Register.jsp at line 21
type Exception report
message An exception occurred processing JSP page /Register.jsp at line 21
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /Register.jsp at line 21
18: String contact = request.getParameter("contact");
19: String msg;
20: String fees=request.getParameter("fees");
21: int stdcontact=Integer.parseInt(contact);
22: int stdfees=Integer.parseInt(fees);
23: if (stdname!= null ) {
24: register objadd = new register();
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:454)
java.lang.Integer.parseInt(Integer.java:527)
org.apache.jsp.Register_jsp._jspService(Register_jsp.java:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.41 logs.
问题似乎在JSP文件中......请帮我解决一下
答案 0 :(得分:2)
stacktrace清楚地告诉异常
NumberFormatException
在
行int stdcontact=Integer.parseInt(contact);
联系人是null
抛出:
NumberFormatException - 如果字符串不包含可解析的整数。
在你的情况下,它是null,不可解析。
使用try/catch
包围您的代码,并添加null
支票。