problema conexion java con sql (mi primer conexion) HOLA.
Estoy realizando un programa para guardar nombre, apellido, cargo y sueldo en una base de datos de sql. Uso netbeans 8 y sql server 2008.
La verdad no puedo conectarlo para guardar datos, hace poco estoy estudiando java por mi cuenta y aca estoy muy trabado.
Agradezco su ayuda, tengan en cuenta que se muyyy poco. Desde ya gracias.
La base se llama uno y la tabla personal, usuario de sql sa y clave 1234
Me aparece para cargar los datos, cuando apreto guardar , el error que da es:
error.java.lang.ClassNotfoundException:com.mysql.j dbc.Driver
el codigo completo es
import java.sql.*;
import javax.swing.JOptionPane;
public class ingreso extends javax.swing.JFrame {
public ingreso() {
initComponents();
setSize(400,400);
setLocation(250,250);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
cnom = new javax.swing.JTextField();
cape = new javax.swing.JTextField();
ccar = new javax.swing.JTextField();
csue = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
setTitle("Ingreso de personal");
setName("jframe1"); // NOI18N
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel1.setText("Ingreso del personal");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 40, 150, -1));
jLabel2.setText("Nombre");
getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 90, -1, -1));
jLabel3.setText("Apellido");
getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 140, -1, -1));
jLabel4.setText("Cargo");
getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 180, -1, -1));
jLabel5.setText("Sueldo");
getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 230, -1, -1));
getContentPane().add(cnom, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 90, 130, -1));
getContentPane().add(cape, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 140, 130, -1));
getContentPane().add(ccar, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 180, 130, -1));
getContentPane().add(csue, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 230, 130, -1));
jButton1.setText("Guardar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 270, 80, 20));
jButton2.setText("SALIR");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 270, -1, -1));
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Connection con;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/uno","sa","1234");
Statement stat =con.createStatement ();
String sql = " insert into personal (nom_per , ape_per , car_per, suel_per) values"
+ "(´"+ this.cnom.getText() + "´, ´" + this.cape.getText() +"´, ´" + this.ccar.getText(
) + "´," + this.csue.getText() + "´)";
JOptionPane.showMessageDialog(rootPane, "datos guardados");
}
catch (ClassNotFoundException | SQLException e ){
JOptionPane.showMessageDialog(rootPane, "error: " + e);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
{
dispose();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClass Name());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ingreso.class.g etName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ingreso.class.g etName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ingreso.class.g etName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ingreso.class.g etName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ingreso().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField cape;
private javax.swing.JTextField ccar;
private javax.swing.JTextField cnom;
private javax.swing.JTextField csue;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
// End of variables declaration
private void dipose() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
} |