PROGRAMA DE BASES
DE DATOS EN JAVA
BASE DE
DATOS : BASEDEDATOS.MDB
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
class DatosPersonales extends Frame implements ActionListener
{ Button crear, ver, insertar, cerrar;
TextField informacion;
Panel principal;
Connection conexion;
public void DatosPersonales()
{ //super("Datos personales");
setSize(200,120);
principal=new
Panel();
crear=new Button ("Crear");
crear.addActionListener(this);
ver=new
Button ("Ver");
ver.addActionListener(this);
insertar=new Button("Insertar");
insertar.addActionListener(this);
cerrar=new Button("Cerrar");
cerrar.addActionListener(this);
informacion=new
TextField(20);
principal.add(informacion);
principal.add(crear);
principal.add(insertar);
principal.add(ver);
principal.add(cerrar);
addWindowListener(new Cerrar());
principal.setBackground(SystemColor.control);
add(principal);
setVisible(true);
try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e)
{
informacion.setText("No se pudo cargar el
controlador JDBC-ODBC");}
}
private void Crear_tabla()
{ Statement sentencia;
try
{
conexion=DriverManager.getConnection("jdbc:odbc:Personal");
sentencia=conexion.createStatement();
try
{
sentencia.executeUpdate("DROP TABLE DATOSPERSONALES");
}
catch(SQLException e){informacion.setText("Error
al crear la tabla");
System.out.println(e);}
sentencia.executeUpdate("CREATE TABLE DATOSPERSONALES("+
"NOMBRE CHAR(20) NOT NULL,"+
"APELLIDOS CHAR(20),"+
"EDAD CHAR(3),"+
"TELEFONO CHAR(10))");
informacion.setText("Tabla
creada");
conexion.close();
}
catch(SQLException e){}
}
public void actionPerformed(ActionEvent e)
{ String com=e.getActionCommand();
if ("Crear".equals(com))
{ informacion.setText("");
Crear_tabla();}
else
if ("Insertar".equals(com))
{ new Insertar(this);
}
else
if ("Ver".equals(com))
{
new Ver(this);
}
else
{dispose();System.exit(0);}
}
class Cerrar extends WindowAdapter
{ public
void windowClosing(WindowEvent
e)
{ dispose();
System.exit(0);
}
}
public
static void main(String args[])
{ new DatosPersonales();}
}
class Insertar extends Dialog implements ActionListener
{
private Connection conexion;
private Button incluir,terminar;
private TextField nombre,apellidos,edad,telefono;
Insertar(Frame f)
{ super(f,"Insertar datos",true);
setSize(310,160);
nombre=new TextField(20);
apellidos=new TextField(20);
edad=new TextField(3);
telefono=new TextField(10);
incluir=new Button("Incluir");
incluir.addActionListener(this);
terminar=new Button("Terminar");
terminar.addActionListener(this);
Panel P_Datos=new Panel();
P_Datos.add(new Label("Nombre : "));
P_Datos.add(nombre);
P_Datos.add(new Label("Apellidos:
"));
P_Datos.add(apellidos);
P_Datos.add(new Label("Edad : "));
P_Datos.add(edad);
P_Datos.add(new Label("Telefono : "));
P_Datos.add(telefono);
P_Datos.add(incluir);
P_Datos.add(terminar);
nombre.setEditable(true);
apellidos.setEditable(true);
edad.setEditable(true);
telefono.setEditable(true);
add(P_Datos);
setVisible(true);
}
private
void insertar_fila()
{ Statement
sentencia;
try{
conexion=DriverManager.getConnection("jdbc:odbc:Personal");
sentencia=conexion.createStatement();
sentencia.executeUpdate("INSERT INTO DATOSPERSONALES"+
" VALUES ('"+nombre.getText()+"',"+
"'"+apellidos.getText()+"',"+
"'"+edad.getText()+"',"+
"'"+telefono.getText()+"')");
}
catch(SQLException e){}
}
public void actionPerformed(ActionEvent e)
{ String com=e.getActionCommand();
if ("Incluir".equals(com))
{insertar_fila();
nombre.setText("");
apellidos.setText("");
edad.setText("");
telefono.setText("");
}
else
{
if(conexion!=null)
{
try
{
conexion.close();
}
catch(SQLException ex){}
}
dispose();
}
}
}
class Ver extends Dialog
implements ActionListener
{
private
Connection conexion;
private
ResultSet resultado;
private Button siguiente,terminar;
private TextField nombre,apellidos, edad,telefono;
Ver(Frame f)
{
super(f,"Ver
datos",true);
setSize(310,160);
nombre=new TextField(20);
apellidos=new TextField(20);
edad=new TextField(3);
telefono=new TextField(10);
siguiente=new Button("Siguiente");
siguiente.addActionListener(this);
terminar=new Button("Terminar");
terminar.addActionListener(this);
Panel P_Datos=new Panel();
P_Datos.add(new Label("Nombre : "));
P_Datos.add(nombre);
P_Datos.add(new Label("Apellidos:
"));
P_Datos.add(apellidos);
P_Datos.add(new Label("Edad : "));
P_Datos.add(edad);
P_Datos.add(new Label("Telefono : "));
P_Datos.add(telefono);
P_Datos.add(siguiente);
P_Datos.add(terminar);
add(P_Datos);
nombre.setEditable(false);
apellidos.setEditable(false);
edad.setEditable(false);
telefono.setEditable(false);
mostrar();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ String com=e.getActionCommand();
if ("Siguiente".equals(com))
siguiente();
else
{
if (conexion!=null)
{
try
{
conexion.close();
}
catch(SQLException ex){}
}
dispose();
}
}
private void mostrar()
{
Statement sentencia;
try{
conexion=DriverManager.getConnection("jdbc:odbc:Personal");
sentencia=conexion.createStatement();
resultado=sentencia.executeQuery("SELECT
* FROM DATOSPERSONALES");
siguiente();
}
catch(SQLException e){}
}
private void ver_Datos()
{ try
{
nombre.setText(resultado.getString("NOMBRE"));
apellidos.setText(resultado.getString("APELLIDOS"));
edad.setText(resultado.getString("EDAD"));
telefono.setText(resultado.getString("TELEFONO"));
}
catch (SQLException e){}
}
private void siguiente()
{ try
{
if (resultado.next()) ver_Datos();
}
catch(SQLException e){}
catch(Exception
ex){}
}
}