如何保存通过表单发送的连接数据?

时间:2013-07-01 23:31:09

标签: java mysql jsp servlets

现在我无法理解我的实现

我的Servlet

package br.com.cad.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.com.cad.dao.Cadastro;
import br.com.cad.basica.Contato;

public class AddDados extends HttpServlet{

    protected void service(HttpServletRequest request, HttpServletResponse response)   
            throws IOException, ServletException { 


 PrintWriter out = response.getWriter();

 ...

    StringBuilder finalDate = new StringBuilder("DataNascimento1")
.append("/"+request.getParameter("DataNascimento‌​2"))
.append("/"+request.getParameter("DataNascimento3"));

 try {  
     Date date = new SimpleDateFormat("dd/MM/yyyy"); //here there are error 
     finalDate.toString(); 

 } catch(ParseException e) {  
     out.println("Erro de conversão da data");  
     return;   
 } 
 Contato contato = new Contato();  
     ??????????

My Object Dao

    package br.com.cad.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Date;

import br.com.cad.dao.ConnectDb;
import br.com.cad.basica.Contato;
public class Cadastro {  

    private Connection connection;  


    public Cadastro() {  
        this.connection = new ConnectDb().getConnection();  
    }  

    public void adiciona(Contato contato) {  
        String sql = "INSERT INTO dados_cadastro(pf_nome, pf_ultimonome, pf_rg, pf_cpf, pf_sexo,pf_dt_nasc) VALUES(?,?,?,?,?,?,?,?)";  
        try {  

            PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setDate(6, new Date( contato.getDataNascimento1().getTimeInMillis()) );
stmt.execute();  
            stmt.close();  
            System.out.println("Cadastro realizado com sucesso!.");  
        } catch(SQLException sqlException) {  
            throw new RuntimeException(sqlException);  
        }  
    }  

My Class Contato

package br.com.cad.basica;

import java.util.Calendar;

public class Contato {

    private Long id;
    private String nome;
    private String sobrenome;
    private String email;
    private String endereco;
    private Calendar dataNascimento1;
    private Calendar dataNascimento2;
    private Calendar dataNascimento3;
    private String rg;
    private String cpf;
    private String sexo;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getSobrenome() {
        return sobrenome;
    }


... getters and setters...

请帮我识别代码中的一些错误并将其实现为在我的数据库中保存数据......

2 个答案:

答案 0 :(得分:0)

您应该将值传递为01,02,03,而不是传递1,2,3(几天和几个月)。检查您的网络表单和日期选择值。

答案 1 :(得分:0)

Date date = new SimpleDateFormat("dd/MM/yyyy"); //here there are error 

这是错误的声明。

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

这不会导致问题。