Database Image Inclusion - Longblob

Asked

Viewed 62 times

0

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

This is my Class Person:

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 I’m showing error when I give a getParameter in the photo, where he says I should change to String, someone knows how to adjust in 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);
        }
    }

2 answers

0

Try:

String fotoString = request.getParameter("foto");
byte[] foto = fotoString.getBytes();

0

I do not know very well what the need for its application.

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

Save images in the database behind a series of problems, among them the increase of the database, thus making it difficult to maintain it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.