使用java类和bean不会在数据库中插入数据

时间:2014-09-04 15:43:30

标签: java servlets

我是Java Web Development的新手。开始学习MVC。我面临问题,即数据无法保存在数据库中。 我没有得到任何例外。 RequestDispatcher只显示我已包含的页面。

很抱歉,如果我没有将此问题写入类别。

代码在这里

控制器代码:

package manipulationStudentData;


import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;

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(description = "Insertion Of Data", urlPatterns = { "/InsertionOfData" })
public class InsertionController extends HttpServlet {
private static final long serialVersionUID = 1L;

public InsertionController() {
    super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  
ServletException, IOException {
    ManipulateStudentBean insertStudentData=new ManipulateStudentBean();
    DatabaseActivityDao database=new DatabaseActivityDao();

    String date=""+request.getParameter("day")+"-"+request.getParameter("month")+"-
    "+request.getParameter("year");

    Date dob=null;

    try{
         dob=new SimpleDateFormat("dd-MM-yyyy").parse(date);
    }catch(Exception e){
        e.printStackTrace();
    }


    insertStudentData.setFirstName(request.getParameter("firstName"));
    insertStudentData.setLastName(request.getParameter("lastName"));
    insertStudentData.setFatherName(request.getParameter("fatherName"));
    insertStudentData.setGender(request.getParameter("gender"));
    insertStudentData.setDob(dob);
    insertStudentData.setClassNo(request.getParameter("classNo"));
    insertStudentData.setAddress(request.getParameter("address"));

    database.InsertData(insertStudentData);
    request.getRequestDispatcher("/ManipulationPage.jsp").include(request, response);


}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
    doGet(request,response);
}

}

BeanClass:

package manipulationStudentData;

import java.util.Date;

public class ManipulateStudentBean {

   int studentId;
String firstName="";
String lastName="";
String fatherName="";
String gender="";
  Date dob;
String classNo="";
String address="";


public int getStudentId() {
    return studentId;
}
public void setStudentId(int studentId) {
    this.studentId = studentId;
}

public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public String getFatherName() {
    return fatherName;
}
public void setFatherName(String fatherName) {
    this.fatherName = fatherName;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public Date getDob() {
    return dob;
}
public void setDob(Date dob) {
    this.dob = dob;
}
public String getClassNo() {
    return classNo;
}
public void setClassNo(String classNo) {
    this.classNo = classNo;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

}

插入数据库代码,只需在此处添加方法:

  public void InsertData(ManipulateStudentBean studentdata){



    String query="INSERT INTO students  
    (FirstName,LastName,FatherName,Gender,DateOfBirth,Class,Address) VALUES "
    + "('" + studentdata.getFirstName() +" ' ' "+ studentdata.getLastName() +" ' '"+ 
    studentdata.getFatherName() +" '  '" +  studentdata.getGender() +" '  '"+ 
    studentdata.getDob() +" '  '" + studentdata.getClassNo() 
    + " '  '" + studentdata.getAddress() + " '  '" ;

    try{

        connection=DatabaseConnection.getConnection();
        stment=connection.createStatement();
        stment.executeQuery(query);


    }catch(Exception e){
        e.printStackTrace();
    }

finally{

        /*Closing Statement*/

        try{
            if(stment !=null){
                stment.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        /*Closing Connection*/

        try{
            if(connection !=null){
                connection.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }

    }
 }

我在不使用MVC模型的情况下检查了相同的查询。

1 个答案:

答案 0 :(得分:0)

更改您的查询:

String query="INSERT INTO students  
    (FirstName,LastName,FatherName,Gender,DateOfBirth,Class,Address) VALUES "
    + "('" + studentdata.getFirstName() +" ', ' "+ studentdata.getLastName() +" ', '"+ 
    studentdata.getFatherName() +" ',  '" +  studentdata.getGender() +" ' , '"+ 
    studentdata.getDob() +" ' , '" + studentdata.getClassNo() 
    + " ' , '" + studentdata.getAddress() + " ' )" ;
根据汤姆的建议,你错过了逗号&结束括号。

由于您只是捕获异常并打印堆栈跟踪,您将在服务器日志中看到异常,之后servlet将请求转发到JSP页面。

或者,您可以使用预处理语句代替使用语句,请参阅此处的示例:

http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/