Galera tô precisando de uma ajuda. Um trabalho para o curso: Preciso salvar uma foto e depois recuperá-la. Salvar e recuperar o caminho da foto, eu consegui fazer. O desafio está sendo em recuperar a imagem do banco. Usando página “jsp”.
Segue os exemplos:
Para salvar (está funcionando) na jsp usei <input type="file"/>, com o seguinte método:
public void inserir (Aluno alu) throws Exception{
String sql = "INSERT INTO tb_aluno (nome, foto, sexo, estado_civil, cidade, uf, cpf, descricao) VALUES (?,?,?,?,?,?,?,?)";
try{
stmt = conn.prepareStatement(sql);
stmt.setString(1, alu.getNome());
File image = new File(alu.getFoto());
FileInputStream fis = new FileInputStream(image);
stmt.setBinaryStream(2, (InputStream)fis,(int)(image.length()));
stmt.setInt(3, alu.getSexo());
stmt.setInt(4, alu.getEstado_civil());
stmt.setString(5, alu.getCidade());
stmt.setInt(6, alu.getUf());
stmt.setString(7, alu.getCpf());
stmt.setString(8, alu.getDescricao());
stmt.execute();
stmt.close();
}catch(SQLException erro){
throw new RuntimeException(erro);
}
}
Como faço para recuperar a imagem do banco usando a seguinte ArrayList?
Quero mostrar numa <img src="" width="40" height="50">
Método:
public ArrayList<Aluno> listar_por_nome (String value)throws Exception{
String sql = "SELECT * FROM tb_aluno WHERE nome LIKE '%"+value+"%'";
try{
st = conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next()){
Aluno alu = new Aluno();
alu.setId(rs.getInt("id"));
alu.setNome(rs.getString("nome"));
alu.setFoto(rs.getString("foto"));
alu.setSexo(rs.getInt("sexo"));
alu.setEstado_civil(rs.getInt("estado_civil"));
alu.setCidade(rs.getString("cidade"));
alu.setUf(rs.getInt("uf"));
alu.setCpf(rs.getString("cpf"));
alu.setDescricao(rs.getString("descricao"));
lista.add(alu);
}
}catch(SQLException erro){
throw new RuntimeException(erro);
}
return lista;
}
É claro que o exemplo acima só traz um monte de caracateres!
No aguardo gente.