- NetBeans 6.9
- MySQL 5.1
Para este ejercicio se creará una aplicación de tipo Cliente/Servidor cuya interfaz se ilustra a continuación:
Para grabar una imagen en una base de datos MySQL debemos utilizar el tipo de dato BLOB, para este caso utilizaremos la tabla PRODUCTO cuya estructura se muestra a continuación:
+-----------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------+------+-----+---------+----------------+
| PRO_ID | int(11) | NO | PRI | NULL | auto_increment | | AUT_ID | int(11) | NO | MUL | NULL | | | CAT_ID | int(11) | NO | MUL | NULL | | | EDI_ID | int(11) | NO | MUL | NULL | | | PRO_NOMBRE | varchar(100) | NO | | NULL | | | PRO_DESCRIPCION | text | YES | | NULL | | | PRO_STOCK | int(11) | NO | | NULL | | | PRO_PRECIO | decimal(10,2) | NO | | NULL | | | PRO_FOTO | blob | YES | | NULL | | +-----------------+---------------+------+-----+---------+----------------+
9 rows in set (0.04 sec)
La clase entity para la tabla PRODUCTO quedaría implementada de la siguiente manera:
private int id;
private int autor;
private int categoria;
private int editorial;
private String nombre;
private String descripcion;
private int stock;
private double precio;
private byte[] foto;
// Implementar los métodos getters y setters
}
Para la columna de tipo blob se crea un campo de tipo ARRAY de BYTES.
El método que corresponde a la capa de persistencia que crea un nuevo producto en la base de datos se implementa de la siguiente manera:
package dao.componentes;
import dao.database.AccesoDB;
import dao.entity.Producto;
import dao.interfaces.IProductoDAO;
import excepcion.TiendaException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class ProductoDAO implements IProductoDAO {
public void crear(Producto prod) throws TiendaException {
Connection cn = null;
PreparedStatement pstm = null;
try {
cn = AccesoDB.getConnection();
cn.setAutoCommit(false);
String query = "insert into producto(AUT_ID,CAT_ID,EDI_ID,PRO_NOMBRE," +
"PRO_DESCRIPCION,PRO_STOCK,PRO_PRECIO,PRO_FOTO) " +
"values(?,?,?,?,?,?,?,?)";
pstm = cn.prepareStatement(query);
pstm.setInt(1, prod.getAutor());
pstm.setInt(2, prod.getCategoria());
pstm.setInt(3, prod.getEditorial());
pstm.setString(4, prod.getNombre());
pstm.setString(5, prod.getDescripcion());
pstm.setInt(6, prod.getStock());
pstm.setDouble(7, prod.getPrecio());
pstm.setBytes(8, prod.getFoto());
pstm.executeUpdate();
cn.commit();
} catch (Exception e) {
try {
cn.rollback();
} catch (Exception e1) {
}
throw new TiendaException(e.getMessage());
} finally {
try {
pstm.close();
} catch (Exception e) {
}
}
}
. . .
. . .
}
Para acceder a la capa DAO se utiliza una capa model implementada de la siguiente manera:
package model;
import dao.componentes.ProductoDAO;
import dao.entity.Producto;
import excepcion.TiendaException;
import java.util.List;
public class CatalogoModel {
public void crearProducto( Producto prod) throws TiendaException{
new ProductoDAO().crear(prod);
}
. . .
. . .
}
Primero se debe seleccionar el archivo para lo cual se ha implementado el botón Buscar [...], el script es el siguiente:
private void btnBuscarFotoActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser openFile = new JFileChooser();
int returnValue = openFile.showOpenDialog(this);
if (returnValue == JFileChooser.CANCEL_OPTION) {
return;
}
File archivo = openFile.getSelectedFile();
this.txtFoto.setText(archivo.getAbsolutePath());
ImageIcon tmp = new ImageIcon(archivo.getAbsolutePath());
ImageIcon imagen = new ImageIcon(tmp.getImage().getScaledInstance(
this.lblFoto.getWidth(),
this.lblFoto.getHeight(),
Image.SCALE_DEFAULT));
this.lblFoto.setIcon(imagen);
}
Y para grabar el registro en la base de datos se ha implementado el botón Grabar, el script es el siguiente:
private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {
try {
Producto prod = new Producto();
prod.setAutor(listaAutores.get(cboAutor.getSelectedIndex()).getId());
prod.setCategoria(listaCategorias.get(cboCategoria.getSelectedIndex()).getId());
prod.setEditorial(listaEditoriales.get(cboEditorial.getSelectedIndex()).getId());
prod.setNombre(txtNombre.getText());
prod.setDescripcion(txtDescripcion.getText());
prod.setStock(Integer.parseInt(txtStock.getText()));
prod.setPrecio(Double.parseDouble(txtPrecio.getText()));
// Obtener la imagen
File archivo = new File(this.txtFoto.getText());
byte[] imgFoto = new byte[(int) archivo.length()];
InputStream input = new FileInputStream(archivo);
input.read(imgFoto);
prod.setFoto(imgFoto);
model.crearProducto(prod);
utilitarios.Util.showMessage("Producto creado en la base de datos satisfactoriamente.");
limpiarFormulario();
} catch (Exception e) {
utilitarios.Util.showError("ERROR: " + e.getMessage());
}
}
Para obtener la aplicación ejemplo utilice el siguiente enlace:
Descargar Código Fuente de la Aplicación
