Creación de la Base de Datos: ejemplo
Creación de la base de datos en Access:
Nombre de la base: prueba_java.accdb
Tabla: prueba
Nota el campo telefono es texto
Lo primero que necesitamos es descargar el driver llamado ucanaccess-3.0.2. Usaremos la versión (3.0.2) esta la pueden descargar desde el siguiente enlace:
https://mega.nz/#F!So8GGTpT!u2jgrB0YKiG6rIxgRNX0Hg
Después de la descarga nos quedaran los siguientes archivos:
https://mega.nz/#F!So8GGTpT!u2jgrB0YKiG6rIxgRNX0Hg
Después de la descarga nos quedaran los siguientes archivos:
Bien ahora nos vamos a NetBeans para importar las librerías en el proyecto que deseen.
Crear el proyecto
Damos clic izquierdo en la carpeta de librerías y damos clic en Add JAR/FOLDER:
Ahora seleccionaremos todas las librerías manteniendo presionada la tecla de control y damos clic en abrir.
Nos quedara de la siguiente forma:
Conexión con la base de de datos
Crear un paquete llamado clases
Crear una clase con el nombre conexion.java
Código del archivo conexion.java:
package Clases;
import java.sql.*;
import javax.swing.JOptionPane;
public class conexion {
Connection con=null;
public conexion(){
try{
//con=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\Ismael\\Documents\\NetBeansProjects\\PruebaConexionBD\\prueba_java.accdb");
con=DriverManager.getConnection("jdbc:ucanaccess://prueba_java.accdb");
//Statement st=con.createStatement();
JOptionPane.showMessageDialog(null, "Conexión exitosa bien");
}catch (SQLException e){
JOptionPane.showMessageDialog(null, "Conexión fallida"+e);
}
}
public Connection getConnection(){
return con;
}
}
Nota: escribir la ruta correctamente y el nombre de la base de datos.
Crear otro paquete llamado Frames
Crear un nuevo jFrame Form y llama a tu archivo inicio.java
vista del formulario:
nombre de las cajas de texto:
Código de la clase inicio.java:
package Frames;
import Clases.conexion;
import java.sql.Connection;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class inicio extends javax.swing.JFrame {
Connection con=null;
conexion conect=new conexion();
public inicio() {
initComponents();
con=conect.getConnection();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
txtNombre = new javax.swing.JTextField();
txtPaterno = new javax.swing.JTextField();
txtTelefono = new javax.swing.JTextField();
txtMaterno = new javax.swing.JTextField();
txtEdad = new javax.swing.JTextField();
btnGuardar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Nombre:");
jLabel2.setText("Paterno:");
jLabel4.setText("Materno:");
jLabel5.setText("Teléfono:");
jLabel6.setText("Edad:");
btnGuardar.setText("Guardar");
btnGuardar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGuardarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel6)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtPaterno, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(62, 62, 62)
.addComponent(txtEdad, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(24, 24, 24))
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(txtMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap(242, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(btnGuardar)
.addGap(257, 257, 257))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4)
.addGap(26, 26, 26)
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addComponent(jLabel6))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(txtPaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(txtMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(txtEdad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(44, 44, 44)
.addComponent(btnGuardar)
.addGap(114, 114, 114))
);
pack();
}// </editor-fold>
private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {
try{
Statement st=con.createStatement();
String sql="insert into prueba (Nombre, Paterno, Materno, Telefono, Edad) values"+"('"+this.txtNombre.getText()+"','"+this.txtPaterno.getText()+"','"+this.txtMaterno.getText()+"','"+this.txtTelefono.getText()+"','"+this.txtEdad.getText()+"')";
st.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "guardado exitosamente");
}catch (Exception e) {
JOptionPane.showMessageDialog(null, "sea producido un error"+e);
}
}
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.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(inicio.class.getName()).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 inicio().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnGuardar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JTextField txtEdad;
private javax.swing.JTextField txtMaterno;
private javax.swing.JTextField txtNombre;
private javax.swing.JTextField txtPaterno;
private javax.swing.JTextField txtTelefono;
// End of variables declaration
}



No hay comentarios:
Publicar un comentario