2016-10-30 37 views
-1

我想開發一個簡單的註冊表單,我已經設計了它在HTML和業務邏輯我使用的servlet,但值不插入數據庫。下面是html和servlet文件。高級感謝那些解決問題的人。值沒有插入到數據庫從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"); 
    } 
} 
+0

DB部分會發生什麼? –

+0

任何異常,日誌,堆棧跟蹤? – Marvin

+0

@Dave Newton&Marvin --- Nothing,page正在成功運行,但值沒有插入到數據庫中,也沒有任何錯誤。 –

回答

0

考慮到你的servlet被激活( 「終於」 被打印),我只能看到3個選項:

  1. 最重要的是:關閉連接。即使你沒有這個缺失的數據問題,這也很重要。通過關閉,你們'承諾'並防止泄漏。對於快速測試,在「ps.executeUpdate」後面添加「con.close()」(在更長的考慮將其放在'finally'子句中)就足夠了。
  2. 一個不太可能的選擇是有一個例外,但你沒有看到它,所以請檢查你的容器的所有日誌文件
  3. 並且也爲這個愚蠢的建議千道歉,但你可以仔細檢查你正在尋找正確的數據庫/表/等...至少我個人在複雜的環境中犯了這麼愚蠢的錯誤;)
+0

感謝您的建議......我會重新檢查並回復您... :) –

+0

對不起,我只是有另一個問題:你看到任何打印輸出「記錄已插入「或」無法插入數據「?謝謝 –

+0

什麼都沒有插入......我只能看到白色的網頁(空白網頁) –