请帮助我发现错误。虽然没有编译错误,但是数据没有被处理到数据库中。 这些代码中没有语法错误。甚至我已经检查了多个站点,但没有发现任何不同。
mvc程序包。
RegisterDao.java
package mvc;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class RegisterDao {
public static Connection getCon() throws ClassNotFoundException, SQLException
{
try{Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){};
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/hcs","root","1234");
return con;
}
public int addRecordpatient(User a)
{
int i=0;
Connection con;
try {
con = getCon();
String qr="insert into signin(phone, name, password, gender, bgrp, age) values(?,?,?,?,?,?)";
PreparedStatement ps=con.prepareStatement(qr);
ps.setInt(1, a.getPhone());
ps.setString(2, a.getName());
ps.setString(3, a.getPassword());
ps.setString(4, a.getGender());
ps.setString(5, a.getBgrp());
ps.setInt(6, a.getAge());
int j=ps.executeUpdate();
if (j!=0) //Just to ensure data has been inserted into the database
{System.out.println("success"); }
con.close();
} catch (Exception e) {
System.out.println(e);
}
//return i;
return 0;
}
}
User.java
package mvc;
public class User {
private int phone;
private String name;
private String password;
private String gender;
private String bgrp;
private int age;
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBgrp() {
return bgrp;
}
public void setBgrp(String bgrp) {
this.bgrp = bgrp;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
servlet-> psignup.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mvc.RegisterDao;
import mvc.User;
@WebServlet("/psignup")
public class psignup extends HttpServlet {
private static final long serialVersionUID = 1L;
public psignup() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
response.setContentType("text/html");
int phone=Integer.parseInt(request.getParameter("phone"));
String name=request.getParameter("name");
String password=request.getParameter("password");
String gender=request.getParameter("gender");
String bgrp=request.getParameter("bgrp");
int age=Integer.parseInt(request.getParameter("age"));
User b=new User();
b.setPhone(phone);
b.setName(name);
b.setPassword(password);
b.setGender(gender);
b.setBgrp(bgrp);
b.setAge(age);
try{
RegisterDao rd= new RegisterDao();
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
catch(Exception e){out.print(e);}
}
}
注册jsp页面---> index.jsp
<form method="post" action="psignup" class=" form-group">
<h1><span class="glyphicon glyphicon-user"></span>Sign Up</h1>
<div class="form-group">
<label for="no">Contact</label>
<input type="tel" class="form-control" id="no" name="phone" placeholder="eg. 9876543210"/>
</div>
<div class="form-group">
<label for="n">Name</label>
<input type="text" class="form-control" id="n" name="name" placeholder="eg. abc pqr xyz"/>
</div>
<div class="form-group">
<label for="p">Password</label>
<input type="password" class="form-control" id="p" name="password" placeholder="eg. Abc123!"/>
</div>
<div class="form-group">
<label for="n">Gender</label>
<input type="text" class="form-control" id="g" name="gender" placeholder="eg. male"/>
</div> <div class="form-group">
<label for="add">blood group</label>
<input type="text" class="form-control" id="bgrp" name="bgrp" placeholder="eg. O+"/>
</div>
<div class="form-group">
<label for="no">Age</label>
<input type="tel" class="form-control" id="age" name="age" placeholder="eg. 101"/>
</div>
<div>
<input type="submit" value="submit" name="submit" class="btn-block btn-primary" id="submit"/>
</div>
</form>
数据库(正常工作)。登录--->
Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| phone | int(11) | NO | PRI | NULL | |
| name | varchar(25) | YES | | NULL | |
| password | varchar(25) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
| bgrp | varchar(25) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
答案 0 :(得分:0)
您可以尝试加载数据库驱动程序;渔获物无济于事。您确定驱动程序已加载吗?
您的RegisterDao
DAO(数据访问对象)具有一个addRecordpatient
方法,该方法将您的User
DTO(数据传输对象)并将其转换为实际查询并将其放入数据库中加上准备好的声明。
您的servlet不调用此方法,它只是实例化DAO。您需要添加
rd.addRecordpatient(user);
实例化对象后。
您是否尝试过调试?同样,任何地方都不会进行数据验证。您应该先将验证逻辑添加到已发布的数据,然后再将其发送到数据库。