我一直在尝试使用Java开发网页。
我的index.html
:
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Simple Dark Form</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<form action="../PatientRegistrationServlet" method="post">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Sofia' rel='stylesheet' type='text/css'>
<div class='login'>
<h2>Register</h2>
<input name='First Name' placeholder='First Name' type='text'>
<input name='Last Name' placeholder='Last Name' type='text'>
<input name='ad1' placeholder='Address Line 1' type='text'>
<input name='Birthdate' placeholder='Birt date' type='text'>
<input name='ad2' placeholder='Address Line 2' type='text'>
<input name='Country' placeholder='Country' type='text'>
<input name='State' placeholder='State' type='text'>
<input name='city' placeholder='city' type='text'>
<input name='pcode' placeholder='Pincode' type='text'>
<input name='Blood group' placeholder='Blood group' type='text'>
<input name='Email' placeholder='E-Mail' type='text'>
<input name='Gender' placeholder='Gender' type='text' >
<input name='mobilno' placeholder='Mobile no' type='text'>
<input id='pw' name='pw' placeholder='Password' type='password'>
<div class='agree'>
<input id='agree' name='agree' type='checkbox'>
<label for='agree'></label>Accept rules and conditions
</div>
<input class='animated' type='submit' value='Register' ">
<a class='forgot' href="../cpaneliclinix/login.jsp">Already have an account?</a>
</div>
</form>
</body>
</html>
这是我的java servlet页面代码:
package com.iclinix.controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/PatientRegistrationServlet")
public class PatientRegistrationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public PatientRegistrationServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fname = request.getParameter("First Name");
String lname = request.getParameter("Last Name");
String addr1 = request.getParameter("ad1");
String bd = request.getParameter("Birthdate");
String addr2 = request.getParameter("ad2");
String cntry = request.getParameter("Country");
String st1 = request.getParameter("State");
String ct = request.getParameter("city");
String pc = request.getParameter("pcode");
String bg = request.getParameter("Blood group");
String mail= request.getParameter("Email");
String gen = request.getParameter("Gender");
String mno = request.getParameter("mobilno");
String pass = request.getParameter("pw");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/iclinix","root","root");
Statement st = con.createStatement();
st.executeUpdate("INSERT INTO tbl_patient_registration (first_name, last_name, add1, birth_date, add2, country, state, city, pincode, bloodgroup, email, gender, mobileno, password)"+ "values('"+fname+"','"+lname+"','"+addr1+"','"+bd+"','"+addr2+"','"+cntry+"','"+st1+"','"+ct+"','"+pc+"','"+bg+"','"+mail+"','"+gen+"','"+mno+"','"+pass+"')");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我一直在尝试存储用户在MySQL数据库中以表格形式提供的注册页面的详细信息。 MySQL数据库与它连接。
图片链接 当我尝试仅使用名字,姓氏和密码的3个字段时,Everthing工作正常,但是当我尝试添加更多字段并运行它时,数据库中没有任何更新。 servlet中没有错误。
它只使用名字和姓氏。请有人帮帮我吗?
答案 0 :(得分:0)
您可以尝试以下方法:
String insertStatement = "INSERT INTO tbl_patient_registration (first_name, last_name, add1, birth_date, add2, country, state, city, pincode, bloodgroup, email, gender, mobileno, password) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pst = con.prepareStatement(insertStatement);
pst.setString(1, fname);
pst.setString(2, lname);
pst.setString(3, addr1);
pst.setDate(4, bd); // here I am guessing that the type of the column is date; if I am wrong use setString instead
... // set the rest of the parameters here
pst.setString(14, pass);
pst.executeUpdate(); // execute the statement after setting the params
这比字符串连接更好,因为它处理
如果您想了解有关PreparedStatement
的更多信息,请查看JDBC tutorial。