Getting parameter sent via html form and saving in my db

Posted by Wesley on Stack Overflow See other posts from Stack Overflow or by Wesley
Published on 2013-07-02T17:03:01Z Indexed on 2013/07/02 17:05 UTC
Read the original article Hit count: 253

Filed under:
|
|
|

I have error in my code i don't know to solve it please help me: My 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();
     String nome = request.getParameter("nome");
     String sobrenome = request.getParameter("sobrenome");
     String rg = request.getParameter("rg");  
     String cpf = request.getParameter("cpf");  
     String sexo = request.getParameter("sexo");
     StringBuilder finalDate = new StringBuilder("DataNascimento1")
.append("/"+request.getParameter("DataNascimento??2"))
.append("/"+request.getParameter("DataNascimento3"));

 try {  
     SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
     finalDate.toString(); 

 } catch(ParseException e) {  
     out.println("Erro de conversão da data");  
     return;   
 } 
 Contato contato = new Contato();  
     contato.setNome(nome); 
     contato.setSobrenome(sobrenome);
     contato.setRg(rg);  
     contato.setCpf(cpf);  
     contato.setSexo(sexo);
        if ("Masculino".equals(contato.getSexo())) {  
         contato.setSexo("M");  
            } else {  
         contato.setSexo("F");  
        }  
     contato.setDataNascimento1(dataNascimento1); //error here ?????
     contato.setDataNascimento2(dataNascimento2); //error here ?????
     contato.setDataNascimento3(dataNascimento3); //error here ?????
 Cadastro dao = new Cadastro();  
     dao.adiciona(contato);
 out.println("<html>");  
 out.println("<body>");  
 out.println("Contato " + contato.getNome() + " adicionado com sucesso");  
 out.println("</body>");  
 out.println("</html>"); 

} 
}

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.setString(1, contato.getNome());  
            stmt.setString(2, contato.getSobrenome());
            stmt.setString(3, contato.getRg());  
            stmt.setString(4, contato.getCpf());
            stmt.setString(5, contato.getSexo());
            stmt.setDate(6, new Date( contato.getDataNascimento1().getTimeInMillis()) ); // i think there are error here i don't know to solve it ?????


            stmt.execute();  
            stmt.close();  
            System.out.println("Cadastro realizado com sucesso!.");  
        } catch(SQLException sqlException) {  
            throw new RuntimeException(sqlException);  
        }  
    }  
}

My class cadastro

 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;
        }
      ...getters and setters

I need to saving data in my mysql db, but i have some doubt about this code main how to get parameter send form html combobox( 1 for day, 2 for month, 3 for year of birth) i concatened with StringBuilder finalDate ... so i have some problem in my code please help me!!!

© Stack Overflow or respective owner

Related posts about java

Related posts about jsp