I'm taking a picture from my web cam, and saving first in D: drive, and from the D: drive to my DB in MySQL as Blob. I want to show that picture when I change rows in a tableview. All the other values are working in the tableview, showing in textfields, but I can't retrieve the image from the DB. here is a picture of my tableview:
Tableview OK There is a ImageView, is where I want to show image corresponding to selected row.
First of all this is the code to save the picture to db.
//Guardar datos visitantes en DB
public void guardar(ActionEvent event) throws Exception{
if ( txtcedula.getText().equals("") ||
txtnombres.getText().equals("") ||
txtapellidos.getText().equals("")||
txtapartamento.getText().equals("")||
txtcelular.getText().equals("") ||
txtobservaciones.getText().equals("")) {
lblcampos.setText("No debe haber campos vacios");
} else {
ConexionSQL cn = new ConexionSQL();
Connection con = cn.conexion();
FileInputStream fis = null;
String ubicaimg = "@../../../../../../Kamui/Imagenes/foto.jpg";
File archivo = new File(ubicaimg);
fis = new FileInputStream(archivo);
//fin busqueda de la foto
String ced,nom,ape,conj,apto,cel,obs;
String sql="";
ced=txtcedula.getText();
nom=txtnombres.getText();
ape=txtapellidos.getText();
conj=cmbconjunto.getSelectionModel().getSelectedItem().toString();
apto=txtapartamento.getText();
cel=txtcelular.getText();
obs = txtobservaciones.getText();
sql="INSERT INTO visitantes (cedula_visi,nombre_visi,apellidos_visi,nomconj_visi,apartamento_visi,celular_visi,observaciones_visi,foto_visi) VALUES (?,?,?,?,?,?,?,?)";
try{
PreparedStatement sta = con.prepareStatement(sql);
sta.setString(1,ced);
sta.setString(2,nom);
sta.setString(3,ape);
sta.setString(4,conj);
sta.setString(5,apto);
sta.setString(6,cel);
sta.setString(7,obs);
sta.setBinaryStream(8, fis, (int) archivo.length());//se convierte la imagen en binary y se guarda como BLOB
sta.executeUpdate();
lblcampos.setText("Guardado Con exito");
lblinfocamara.setVisible(false);
} catch(Exception e){
String info = e.getMessage();
lblinfo.setText(info);
}
}
}
And here is the code to show the other values in the textfields
package application;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import javax.swing.ImageIcon;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
public class ver_visitantes implements Initializable {
@FXML private TableView<visitantes> tbvisitantes;//enlazar tableview con el objeto visitantes
@FXML private TableColumn<visitantes, String> clcedula;
@FXML private TableColumn<visitantes, String> clnombres;
@FXML private TableColumn<visitantes, String> clapellidos;
@FXML private TableColumn<visitantes, String> clhentrada;
@FXML private TableColumn<visitantes, String> clconjunto;
@FXML private TableColumn<visitantes, String> clapartamento;
@FXML private TableColumn<visitantes, String> clcelular;
@FXML private TableColumn<visitantes, String> clobservaciones;
@FXML private ImageView imgfotovisi;
@FXML private TextField txtcedula;
@FXML private TextField txtnombres;
@FXML private TextField txtapellidos;
@FXML private TextField txtconjunto;
@FXML private TextField txtapto;
@FXML private TextField txtcelular;
@FXML private TextField txtobservaciones;
@FXML private Label lbltest;
private ObservableList<visitantes> visitorlist;//dar nombre al observable list que se llena con el objeto visitantes
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
ConexionSQL cnt = new ConexionSQL();
cnt.conexion();
visitorlist = FXCollections.observableArrayList();
visitantes.llenarlistavisitas(cnt.conexion(), visitorlist);
tbvisitantes.setItems(visitorlist);//llenar table view con la lista
imgfotovisi.getClass().getResourceAsStream("imagenes/huellabiometrico.jpg");
//enlazar cada columna con el campo a llenar del resultset con los metodos getter
clcedula.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getcedula()));
clnombres.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getnombres()));
clapellidos.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getapellidos()));
clconjunto.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getconjunto()));
clapartamento.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getapartamento()));
clcelular.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getcelular()));
clobservaciones.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getobservaciones()));
clhentrada.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().gethentrada()));
gestionarEventos();
/*visitantes visi = new visitantes(null, null, null, null, null, null, null, null, null);
String image;
image = visi.getfoto();
imgfotovisi.setId(image);*/
}
public void gestionarEventos(){
tbvisitantes.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<visitantes>() {
@Override
public void changed(ObservableValue<? extends visitantes> arg0,
visitantes valorAnterior, visitantes valorSeleccionado) {
if (valorSeleccionado!=null){
txtcedula.setText(String.valueOf(valorSeleccionado.getcedula()));
txtnombres.setText(valorSeleccionado.getnombres());
txtapellidos.setText(valorSeleccionado.getapellidos());
txtconjunto.setText(String.valueOf(valorSeleccionado.getconjunto()));
txtapto.setText(String.valueOf(valorSeleccionado.getapartamento()));
txtcelular.setText(String.valueOf(valorSeleccionado.getcelular()));
txtobservaciones.setText(String.valueOf(valorSeleccionado.getobservaciones()));
}
}
});
}
}
and this is the visitantes object
package application;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
public class visitantes {
private StringProperty cedula;
private StringProperty nombres;
private StringProperty apellidos;
private StringProperty hentrada;
private StringProperty conjunto;
private StringProperty apartamento;
private StringProperty celular;
private StringProperty observaciones;
private Blob foto;
public visitantes(String cedula,String nombres,String apellidos,String hentrada,String conjunto,String apartamento,String celular,String observaciones,Blob foto){
this.cedula = new SimpleStringProperty(cedula);
this.nombres = new SimpleStringProperty(nombres);
this.apellidos = new SimpleStringProperty(apellidos);
this.hentrada = new SimpleStringProperty(hentrada);
this.conjunto = new SimpleStringProperty(conjunto);
this.apartamento = new SimpleStringProperty(apartamento);
this.celular = new SimpleStringProperty(celular);
this.observaciones = new SimpleStringProperty(observaciones);
this.foto = foto;
}
public String getnombres(){
return nombres.get();
}
public void setnombres(String nombres){
this.nombres = new SimpleStringProperty(nombres);
}
public String getcedula(){
return cedula.get();
}
public void setcedula(String cedula){
this.cedula = new SimpleStringProperty(cedula);
}
public String getapellidos(){
return apellidos.get();
}
public void setapellidos(String apellidos){
this.apellidos = new SimpleStringProperty(apellidos);
}
public String gethentrada(){
return hentrada.get();
}
public void sethentrada(String hentrada){
this.hentrada = new SimpleStringProperty(hentrada);
}
public String getconjunto(){
return conjunto.get();
}
public void setconjunto(String conjunto){
this.conjunto = new SimpleStringProperty(conjunto);
}
public String getapartamento(){
return apartamento.get();
}
public void setapartamento(String apartamento){
this.apartamento = new SimpleStringProperty(apartamento);
}
public String getcelular(){
return celular.get();
}
public void setcelular(String celular){
this.celular = new SimpleStringProperty(celular);
}
public String getobservaciones(){
return observaciones.get();
}
public void setobservaciones(String observaciones){
this.observaciones = new SimpleStringProperty(observaciones);
}
public Blob getfoto(){
return foto;
}
public static void llenarlistavisitas(Connection connection, ObservableList<visitantes> lista){
try {
String sql="SELECT * FROM visitantes";
Statement statement = connection.createStatement();
ResultSet visitantes = statement.executeQuery(sql);
while (visitantes.next()){
lista.add (
new visitantes(
visitantes.getString("cedula_visi"),
visitantes.getString("nombre_visi"),
visitantes.getString("apellidos_visi"),
visitantes.getString("hentrada"),
visitantes.getString("nomconj_visi"),
visitantes.getString("apartamento_visi"),
visitantes.getString("celular_visi"),
visitantes.getString("observaciones_visi"),
visitantes.getBlob("foto_visi")
)
);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
I hope you can help me with this. Thanks a lot.