import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class frmBuscarBC extends javax.swing.JFrame {
coneBD cn=new coneBD();
DefaultTableModel modelo=new DefaultTableModel();
public frmBuscarBC() {
initComponents();
String titu[]={"IDProducto","Nombre","Precio","Stock"};
modelo.setColumnIdentifiers(titu);
tblPro.setModel(modelo);
tblPro.getColumnModel().getColumn(1).setPreferredWidth(200);
}
private void txtBuscarActionPerformed(java.awt.event.ActionEvent evt) {
String SQL="select * from productos where barcode like '"+txtBuscar.getText()+"%';";
String datos[]=new String[4];
int op=0;
try{
cn.Conectar();
cn.st=cn.conec.createStatement();
cn.rt=cn.st.executeQuery(SQL);
while (cn.rt.next()){
datos[0]=cn.rt.getString(1);
datos[1]=cn.rt.getString(2);
datos[2]=cn.rt.getString(3);
datos[3]=cn.rt.getString(4);
modelo.addRow(datos);
op=1;
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
}
if (op==0) {
JOptionPane.showMessageDialog(null, "No tenemos el producto "+txtBuscar.getText());
} else {
txtBuscar.setText(null);
}
}
Clase de conexion coneBD.java
import java.sql.*;
import javax.swing.JOptionPane;
public class coneBD {
public static PreparedStatement prest=null;
public static CallableStatement cllst=null;
public static Connection conec=null;
public static Statement st=null;
public static ResultSet rt=null;
public static String usu;
public static String contra;
public static String host;
public static String db;
public void Conectar(){
try{
usu="root"; contra=""; host="localhost"; db="ventas";
String url="jdbc:mysql://"+host+"/"+db;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conec=DriverManager.getConnection(url,usu,contra);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
}
}
}
Tener agregado a libraries el conector mysql para java.
No hay comentarios:
Publicar un comentario