我正在创建一个接受学生数据的简单表单,然后按下提交按钮,数据被发送到“addStdInfoServlet”,这是一个接受数据并使用JDBC插入数据库表学生的servlet。
我写了以下代码,但每当我输入所有信息后提交表格。我的servlet页面没有加载或者没有显示任何响应,而且数据也没有插入到学生表中。
请帮我解决这个问题?
std_registration.html
<form action="addStdInfoServlet" method="POST">
<input type="text" name="form_no" placeholder="Form No.">
<input type="text" placeholder="Course Code" name="ccode">
<input type="text" placeholder="Roll No." name="rollno">
<input type="text" placeholder="Course Name" name="cname"><br>
Course commencing from : <input type="date" name="start_from"> To <input type="date" name="till"><br>
<input type="text" placeholder="Student Surname" name="s_sname">
<input type="text" placeholder="Student Firstname" name="s_fname">
<input type="text" placeholder="Student Middlename" name="s_mname"><br>
<input type="text" placeholder="Blood Group" name="bgrp">
<input type="text" placeholder="Aadhar Card No." name="a_id">
<input type="text" placeholder="Religion" name="religion">
<input type="email" placeholder="Email Address" name="emailid"><br>
<input type="text" placeholder="Father's Surname" name="f_sname">
<input type="text" placeholder="Father's Firstname" name="f_fname">
<input type="text" placeholder="Father's Middlename" name="f_mname">
<input type="text" placeholder="Occupation Of Father" name="occupation"><br>
<textarea rows="6" cols="50" placeholder="Residential Address" name="address"></textarea><br>
<input type="text" placeholder="Income Per Annum" name="income">
<input type="text" placeholder="Contact No." name="contact_no"><br>
Date Of Birth : <input type="date" name="dob"><br>
Select Gender:<br><input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female <br>
Select Category : <br>
<input type="radio" name="cat" value="SC">SC
<input type="radio" name="cat" value="ST">ST
<input type="radio" name="cat" value="NT">NT
<input type="radio" name="cat" value="Others">Others<br><br>
Educational Qualifications :<br><br>
<table border="1">
<tr><th>Examination</th><th>Board/University</th><th>% of Marks</th><th>Year of Passing</th></tr>
<tr><td>10th std</td><td><input type="text" name="sscboard"></td><td><input type="text" name="sscper"></td><td>
<select name="ssc_year">
<option value="Select">Select</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select></td></tr>
<tr><td>12th std</td><td><input type="text" name="hscboard"></td><td><input type="text" name="hscper"></td>
<td><select name="hsc_year">
<option value="Select">Select</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select></td></tr></td></tr>
<tr><td>Degree/Diploma</td><td><input type="text" name="dboard"></td><td><input type="text" name="dper"></td><td>
<select name="d_year">
<option value="Select">Select</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select></td></tr></td></tr>
<tr><td>Other Courses</td><td><input type="text" name="otherboard"></td><td><input type="text" name="oper"></td><td><select name="o_year">
<option value="Select">Select</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select></td></tr>
</table>
Experience : <select name="exp">
<option value="Select">Select</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option></select> yrs.
<br>
<br>
Registration Fee: <br>
<input type="radio" name="reg_fee" value="cash" > Cash
<input type="radio" name="reg_fee" value="dd" > DD <br>
<div class="declaration">
<p>Declaration</p>
<p>I hereby solemnly declare that the information furnished by me in this application is true to the best of my knowledge and belief. I abide the rules & regulation. General information given the prospectus and enforced by the institute from time .If any information found incorrect at any stage of my study. The institute reserves its right to cancel my registration/admission and no fees shall be refunded to me.</p>
<input type="text" placeholder="Place" name="place">
<input type="date" name="admission_date">
Signature of the applicant:
</div>
<input type="submit" value="Submit">
addStdInfoServlet.java
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class addStdInfoServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
response.setContentType("text/html");
int fno = Integer.parseInt(request.getParameter("form_no"));
String ccode = request.getParameter("ccode");
int rno = Integer.parseInt(request.getParameter("rollno"));
String cname = request.getParameter("cname");
String start_from = request.getParameter("start_from");
String till = request.getParameter("till");
String s_sname = request.getParameter("s_sname");
String s_fname = request.getParameter("s_fname");
String s_mname = request.getParameter("s_mname");
String bgrp = request.getParameter("bgrp");
int a_id= Integer.parseInt(request.getParameter("a_id"));
String religion = request.getParameter("religion");
String emailid = request.getParameter("emailid");
String f_sname = request.getParameter("f_sname");
String f_fname = request.getParameter("f_fname");
String f_mname = request.getParameter("f_mname");
String occupation = request.getParameter("occupation");
String address = request.getParameter("address");
double income = Double.parseDouble(request.getParameter("income"));
int contact_no = Integer.parseInt(request.getParameter("contact_no"));
String dob = request.getParameter("dob");
String gender = request.getParameter("gender");
String cat = request.getParameter("cat");
String sscboard = request.getParameter("sscboard");
Double sscper = Double.parseDouble(request.getParameter("sscper"));
int ssc_year = Integer.parseInt(request.getParameter("ssc_year"));
String hscboard = request.getParameter("hscboard");
Double hscper = Double.parseDouble(request.getParameter("hscper"));
int hsc_year = Integer.parseInt(request.getParameter("hsc_year"));
String dboard = request.getParameter("dboard");
Double dper = Double.parseDouble(request.getParameter("dper"));
int d_year = Integer.parseInt(request.getParameter("d_year"));
String otherboard = request.getParameter("otherboard");
Double oper = Double.parseDouble(request.getParameter("oper"));
int o_year = Integer.parseInt(request.getParameter("o_year"));
int exp = Integer.parseInt(request.getParameter("exp"));
String reg_fee = request.getParameter("reg_fee");
String place = request.getParameter("place");
String admission_date = request.getParameter("admission_date");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/idemi?zeroDateTimeBehavior=convertToNull","root","");
String query = "insert into students values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement st = conn.prepareStatement(query);
st.clearParameters();
st.setInt(1,fno);
st.setString(2,ccode);
st.setInt(3,rno);
st.setString(4,cname);
st.setString(5,start_from);
st.setString(6,till);
st.setString(7,s_sname);
st.setString(8,s_fname);
st.setString(9,s_mname);
st.setString(10,bgrp);
st.setInt(11,a_id);
st.setString(12,religion);
st.setString(13,emailid);
st.setString(14,f_sname);
st.setString(15,f_fname);
st.setString(16,f_mname);
st.setString(17,occupation);
st.setString(18,address);
st.setDouble(19,income);
st.setInt(20,contact_no);
st.setString(21,dob);
st.setString(22,gender);
st.setString(23,cat);
st.setString(24,sscboard);
st.setDouble(25,sscper);
st.setInt(26,ssc_year);
st.setString(27,hscboard);
st.setDouble(28,hscper);
st.setInt(29,hsc_year);
st.setString(30,dboard);
st.setDouble(31,dper);
st.setInt(32,d_year);
st.setString(33,otherboard);
st.setDouble(34,oper);
st.setInt(35,o_year);
st.setInt(36,exp);
st.setString(37,reg_fee);
st.setString(38,place);
st.setString(39,admission_date);
st.executeUpdate();
st.close();
conn.close();
out.println(" <!doctype html>"
+"<html>"
+"<head>"
+"<title>Admin Registration</title>"
+"<script src=\"js/jquery.min.js\"></script>"
+"<script src=\"js/script.js\"></script>"
+"<link rel=\"stylesheet\" type=\"text/css\" href=\"style/receipt.css\">"
+"<link rel=\"shortcut icon\" href=\"images/favicon.ico\" type=\"image/x-icon\">"
+"<link rel=\"icon\" href=\"images/favicon.ico\" type=\"image/x-icon\">"
+"</head>"
+"<body>"
+"<div class=\"container\">"
+"<div class=\"container-inner\">"
+"<div class=\"header\">"
+"<div class=\"header-inner1\">"
+"<div class=\"header-pic\">"
+"<img src=\"images/logo.png\">"
+"</div>"
+"<div class=\"header-text\">"
+"<p>ONLINE ADMISSION SYSTEM</p>"
+"</div>"
+"</div>"
+"<div class=\"header-inner2\"> "
+"<div class=\"header-menu\"> "
+"<ul class=\"menu-tab-grp\">"
+"<li><a href=\"index.html\">Home</a></li>"
+"<li><a href=\"about_us.html\">About Us</a></li>"
+"<li><a href=\"registration.html\">Registration</a></li>"
+"<li><a href=\"courses_offered.html\">Courses Offered in IT</a></li>"
+"<li><a href=\"contactus.html\">Contact Us</a></li>"
+"</ul>"
+"</div>"
+"</div>"
+"</div>"
+"<div class=\"main-body\">"
+"<div class='logout'><h4>Student Information added successfully..!</h4></div>"
+"</div>"
+"</div>"
+"</div>"
+"</body>"
+"</html> ");
}
catch(SQLException e)
{
System.out.println(e);
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
doGet(request,response);
}
}
我正在使用MySQL数据库,我创建了一个名为'students'的表,它具有以下结构:
答案 0 :(得分:0)
sql命令中有一个拼写错误:insert into vales
正确的语法是insert into valUes
。
现在您可以非常轻松地调试此代码:您可以打印sql命令并尝试直接在sql developer中运行。
但是,我建议使用PreparedStatement
并使用其setString
,setInt
,...方法来设置参数,因为这样更安全。
答案 1 :(得分:0)
正如tamas rev所说你有拼写错误,如果你有其他错误,请告诉我们。
此外,您可以使用sql profiler
来查看在sql中执行的确切内容。
您的所有专栏都是"Not null
&#34; ,请确保您的查询包含所有字段。
BTW,你的< /form>
在哪里?
你可以调试吗?我的意思是,你可以轻松调试它并捕捉异常发生的地方。
最后,您可以在try / catch块中捕获Exception,以便捕获并打印它。
答案 2 :(得分:0)
只尝试插入2或3个字段。我已经测试了相同的代码,它适用于我(仅插入前两个字段)。