值不从servlet插入数据库

时间:2016-10-30 17:02:02

标签: java html servlets

我正在尝试开发一个简单的注册表单,我用HTML设计它,对于业务逻辑,我使用了servlet但是值没有插入到数据库中。下面是html和servlet文件。感谢那些解决的人。

Register.html

<html>
<head>
  <title>Register form</title>
</head>
<body>
    <form method="post" action="Patient">
    PID:<input type="text" name="pid" /><br/>
    Title:<input type="text" name="title" /><br/>
    First Name:<input type="text" name="firstname" /><br/>
    Last Name:<input type="text" name="lastname" /><br/>
    Birthday:<input type="date" name="dob" /><br/>
    Address:<input type="text" name="address" /><br/>
    E-Mail:<input type="text" name="email" /><br/>
    Contact Number:<input type="text" name="contact"/><br/>
    Gender: 
            <input type="radio" name="gender" value="male" checked> Male
            <input type="radio" name="gender" value="female"> Female
            <input type="radio" name="gender" value="other"> Other<br><br>
  Occupation:<input type="text" name="occupation"/><br/>
  Age:<input type="text" name="age"/><br/>

  <label>Spouse/Parent/Legal Guardian Details</label><br>

  Name:<input type="text" name="name"/><br/>
  E-Mail:<input type="email" name="email2"/><br/>
  Address:<input type="text" name="address2"/><br/>
  Contact:<input type="text" name="phone"/><br/>
  Relation:<input type="text" name="relation"/><br/>
  Occupation:<input type="text" name="occupation2"/><br/>
  Age Group:<input type="text" name="agegroup"/><br/>
    <input type="submit" value="Register" />
    </form>
  </body>
 </html>

Servlet.java

import java.io.*; 
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;


public class Patient extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse         response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("Started");
        String pid = request.getParameter("pid");
        String title = request.getParameter("title");
        String firstname = request.getParameter("firstname");
        String lastname = request.getParameter("lastname");
        String dob = request.getParameter("dob");
        String address = request.getParameter("address");
        String email = request.getParameter("email");
        String contact = request.getParameter("contact");
        String gender = request.getParameter("gender");
        String occupation = request.getParameter("occupation");
        String age = request.getParameter("age");
        String name = request.getParameter("name");
        String email2 = request.getParameter("email2");
        String address2 = request.getParameter("address2");
        String phone = request.getParameter("phone");
        String relation = request.getParameter("relation");
        String occupation2 = request.getParameter("occupation2");
        String agegroup = request.getParameter("agegroup");

        try {
        Class.forName("com.mysql.jdbc.Driver");
      Connection      con=DriverManager.getConnection("jdbc:mysql://localhost:3306/copd","root","root");
       PreparedStatement ps=con.prepareStatement("insert into tbl_patient values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

        ps.setString(1, pid);
        ps.setString(2, title);
        ps.setString(3, firstname);
        ps.setString(4, lastname);
        ps.setString(5, dob);
        ps.setString(6, address);
        ps.setString(7, email);
        ps.setString(8, contact);
        ps.setString(9, gender);
        ps.setString(10, occupation);
        ps.setString(11, age);
        ps.setString(12, name);
        ps.setString(13, email2);
        ps.setString(14, address2);
        ps.setString(15, phone);
        ps.setString(16, relation);
        ps.setString(17, occupation2);
        ps.setString(18, agegroup);

        int i=ps.executeUpdate();


        if(i>0)
        {
            out.println("Record has been inserted");
        }
        else
        {
            out.println("Failed to insert the data");
        }
    }
    catch(Exception se)
    {
       se.printStackTrace();
    }
        finally{}
     out.println("ended");
  }
}

1 个答案:

答案 0 :(得分:0)

鉴于您的servlet已激活(打印出“finally”),我只能看到3个选项:

  1. 最重要的是:关闭连接。即使您没有这个丢失的数据问题,这也很重要。通过关闭,您既“提交”又防止泄漏。对于快速测试,在“ps.executeUpdate”之后你将添加“con.close()”(更长时间考虑将它放在'finally'子句中)。
  2. 不太可能的选项是有异常,但您没有看到,所以请检查容器的所有日志文件
  3. 对于这个愚蠢的建议也有一千道歉,但是你能仔细检查一下你是否正在查看正确的数据库/表格等等......至少我个人在复杂的环境中犯了如此愚蠢的错误;)