You are on page 1of 3

String cadena2,cadena3,cadena4,cadena5,cadena6,cadena7;

cadena2 = txt_nombre.getText();
cadena3 = txt_domicilio.getText();
cadena4 = txt_telefono.getText();
cadena5 = txt_nick.getText();
cadena6 = txt_password.getText().toString();
cadena7 = cmb_tipoUsuario.getSelectedItem().toString();

if (txt_nombre.getText().equals("") || (txt_domicilio.getText().equals("")) ||
(txt_telefono.getText().equals("")) || (txt_nick.getText().equals(""))
|| (txt_password.getText().equals("")) ||
(cmb_tipoUsuario.getSelectedItem().equals(null))) {

javax.swing.JOptionPane.showMessageDialog(this,"Debe llenar todos los


campos \n","AVISO!",javax.swing.JOptionPane.INFORMATION_MESSAGE);
txt_nombre.requestFocus();
}

else {
try {

String url =
"jdbc:mysql://localhost:3306/nombre_de_tu_base_de_datos";
String usuario = "root";
String contrasea = "tu_contrasea";

Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url,usuario,contrasea);
if ( con != null )

System.out.println("Se ha establecido una conexin a la base de


datos " +
"\n " + url );

stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO usuarios VALUES('" + 0 +
"','"+cadena2+"','"+cadena3+"','"+cadena4+"','"+cadena5+"','"+cadena6+"','
"+cadena7+"')");
System.out.println("Los valores han sido agregados a la base de
datos ");

} catch (InstantiationException ex) {


Logger.getLogger(nombre_de_la_clase.class.getName()).log(Level.SEVERE, null,
ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(nombre_de_la_clase.class.getName()).log(Level.SEVERE, null,
ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(nombre_de_la_clase.class.getName()).log(Level.SEVERE, null,
ex);
} catch (SQLException ex) {
Logger.getLogger(nombre_de_la_clase.class.getName()).log(Level.SEVERE, null,
ex);
}

finally {
if (con != null) {
try {
con.close();

stmt.close();
} catch ( Exception e ) {
System.out.println( e.getMessage());
}
}
}
javax.swing.JOptionPane.showMessageDialog(this,"Registro exitoso!
\n","AVISO!",javax.swing.JOptionPane.INFORMATION_MESSAGE);
}
this.txt_nombre.setText("");
this.txt_domicilio.setText("");
this.txt_telefono.setText("");
this.txt_nick.setText("");
this.txt_password.setText("");