Está en la página 1de 2

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package factura;

import com.mysql.cj.protocol.Resultset;
import crud.Conexion;
import crud.ProductoDAO;
import crud.Productos;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
*
* @author Johan
*/
public class Funcion {

//se crea variable


private String mensaje=" ";
//se crea un objeto
private ProductoDAO producDao=new ProductoDAO();

public void Listar(Connection conexion, JTable tabla){


//se crea tabla por defecto
DefaultTableModel model;
//se agregan columnas
String []
columnas={"codigo_producto","nombre","descripcion","precio","cantidad","codigo_impu
esto"};
//a la tabla por defecto se le agregan el nombre de las columnas
model = new DefaultTableModel(null, columnas);
// se trae los datos de la columna productos de la bd
String sql = "SELECT * FROM productos ORDER BY codigo_producto";
//se le agrega a las filas
String [] filas=new String[6];
Statement st=null;
Resultset rs=null;

try {
//se crea una conexcion
st= conexion.createStatement();
//se ejecutar el query
ResultSet resultado =st.executeQuery(sql);
while(resultado.next()){
//se insertan los valores de la bd
for (int i = 0; i < 6; i++) {
filas[i]=resultado.getString(i+1);
}
model.addRow(filas);

}
tabla.setModel(model);
} catch (Exception e) {
//atrapa el error
JOptionPane.showMessageDialog(null, "no se puede mostrar la
tabla");
}
}

Productos prod =new Productos();


public String Agregar (Productos p){
//se hace la conexcion a las bd
Connection conexion= Conexion.getConnection();
try {
//se usa el metodo agregar de productoN.java
mensaje= producDao.Agregar(conexion, p);

} catch (Exception e) {
//atrapa el error
mensaje= mensaje+" "+ e.getMessage();
}finally{
try {
if(conexion!=null){
conexion.close();
}
} catch (Exception e) {
}
}

return mensaje;

También podría gustarte