Image Inclusion in Database - LongBlob

0

I am coding a simple register, where it needs to also record in the database a photo, which should be from the tibo BLOB.

This is my Person Class:

public class Pessoa {

    private Integer idPessoa;
    private String nome;
    private Date dataNascimento;
    private String cpf;    
    private byte[] foto;

This is my inclusion DAO:

public class PessoaDAO {
    public void addPessoa(Pessoa pessoa) {
        Conecta c = new Conecta();
        try {              
            if ("sucesso".equals(c.getMsg())) {
                String sql = "INSERT INTO pessoa (nome, dataNascimento, cpf,foto) VALUES (?,?,?,?)";
                PreparedStatement pstm;

                pstm = c.getConexao().prepareStatement(sql);

                pstm.setString(1, pessoa.getNome());                
                pstm.setDate(2, pessoa.getDataNascimento());
                pstm.setString(3, pessoa.getCpf());                
                pstm.setBytes(4, pessoa.getFoto());
                pstm.execute();

            }


        } catch (SQLException ex) {
            Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

And this is my inclusion Servlet, where it is presenting error when I give a getParameter in the photo, where it says I should change to String, does anyone know how to adjust in the servlet?

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
           PessoaDAO pessoadao = new PessoaDAO();

        String nome = request.getParameter("nome");              


            String dataNasc = request.getParameter("dataNascimento");

            SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date dataUtil = formato.parse(dataNasc);

            java.sql.Date dataSql = new java.sql.Date(dataUtil.getTime());

        String cpf = request.getParameter("cpf");   
        // Diz que devo mudar de byte para String 
        byte[] foto = request.getParameter("foto");

        int idUsuario =     Integer.parseInt(request.getParameter("idUsuario"));
        Pessoa pessoa = new Pessoa(nome,dataSql,cpf,foto);

        pessoadao.addPessoa(pessoa);
           Usuario usuario = new Usuario();
        RequestDispatcher rd; 
            UsuarioDAO udao=new UsuarioDAO();
            for(Usuario u : udao.getLista()){
                if(u.getIdUsuario()==idUsuario){
                    usuario=u;
                    break;
                }

            }

                request.setAttribute("usuario", usuario);

                    rd = getServletContext().getRequestDispatcher("/cadpessoa.jsp");
                    rd.forward(request, response);

        } catch (ParseException ex) {
            Logger.getLogger(ServletIncluiPessoa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
asked by anonymous 17.11.2018 / 16:01

2 answers

0

I'm not really sure what the need for your application is.

But if possible, the ideal would be to save your images to some directory or FTP server, and save only the path in the database, so that the application can recover the image.

Save images to the database causes a number of problems, among them the increase of the database, making it difficult to maintain it.

    
20.11.2018 / 19:14
0

Try:

String fotoString = request.getParameter("foto");
byte[] foto = fotoString.getBytes();
    
20.11.2018 / 17:51