Tener agregado a libraries el conector mysql para java.
import javax.swing.JOptionPane;
public class frmInsertar extends javax.swing.JFrame {
coneBD cn=new coneBD();
public frmInsertar() {
initComponents();
}
private void btnInsertarActionPerformed(java.awt.event.ActionEvent evt) { int op=0;
String sql="insert into productos values('"+txtBar.getText()+"','"+txtNom.getText()+"','"+txtPre.getText()+"','"+txtStock.getText()+"','"+txtImg.getText()+"');";
try{
cn.Conectar();
cn.st=cn.conec.createStatement();
int p=cn.st.executeUpdate(sql);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
op=1;
}
if (op==0) {
JOptionPane.showMessageDialog(null, "Registro Agregado");
}
}
private void txtBarActionPerformed(java.awt.event.ActionEvent evt) { JOptionPane.showMessageDialog(null, "Enter:"+txtBar.getText());
}
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());
}
}
}
minuvasoft10@gmail.com
No hay comentarios:
Publicar un comentario