我想開發一個簡單的註冊表單,我已經設計了它在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");
}
}
DB部分會發生什麼? –
任何異常,日誌,堆棧跟蹤? – Marvin
@Dave Newton&Marvin --- Nothing,page正在成功運行,但值沒有插入到數據庫中,也沒有任何錯誤。 –