gpt4 book ai didi

java - 数据未插入 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 20:22:01 25 4
gpt4 key购买 nike

我一直在尝试使用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数据库与之连接。

Here is screenshot

图片链接当我尝试仅使用名字、姓氏和密码这 3 个字段时,一切正常,但当我尝试添加更多字段并运行它时,数据库中没有任何更新。servlet 中没有错误。

它仅适用于名字和姓氏。请问有人可以帮助我吗?

最佳答案

您可以尝试以下操作:

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

这比字符串连接更好,因为它可以处理

  • SQL 注入(inject),
  • 可读(我的口味)并且不易出错。

如果您想了解有关 PreparedStatement 的更多信息,请查看 JDBC tutorial .

关于java - 数据未插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39465906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com