Tener agregado en libraries el conector mysql para java
import javax.swing.JOptionPane;
public class frmActualizar extends javax.swing.JFrame {
coneBD cn = new coneBD();
public frmActualizar() {
initComponents();
}
private void txtBuscarActionPerformed(java.awt.event.ActionEvent evt) { String cad = txtBuscar.getText();
String SQL="select * from productos where barcode like '"+cad+"%';";
int op=0;
try{
cn.Conectar();
cn.st=cn.conec.createStatement();
cn.rt=cn.st.executeQuery(SQL);
while (cn.rt.next()){
txtNom.setText(cn.rt.getString(2));
txtPre.setText(cn.rt.getString(3));
txtStock.setText(cn.rt.getString(4));
txtIma.setText(cn.rt.getString(5));
op=1;
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
}
if (op==0) {
JOptionPane.showMessageDialog(null, "No tenemos el producto "+cad);
}
}
private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) { String SQL2="update productos set Nombre='"+txtNom.getText()+"',Precio='";
SQL2 += txtPre.getText()+"',Stock='"+txtStock.getText()+"',imagen='";
SQL2 +=txtIma.getText()+"' where barcode='"+txtBuscar.getText()+"';";
try{
cn.Conectar();
cn.st=cn.conec.createStatement();
int p=cn.st.executeUpdate(SQL2);
JOptionPane.showMessageDialog(null, "Registro Modificado");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
}
}
Clase de conexion a base de datos 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());
}
}
}
No hay comentarios:
Publicar un comentario