
import java.awt.*; 
import java.sql.*; //Se invocan todas las funciones del paquete SQL JDBC 
import java.awt.event.*;

public class DatosPersonales extends Frame implements ActionListener {
	Button crear, ver, insertar, cerrar; 
	TextField informacion; 
	Panel principal; 
	Connection conexion; 
	
	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); 
		principal.setBackground(SystemColor.control); 
		add(principal); setVisible(true); 
		try { /*Cargamos el controlador*/ 
			
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
			} catch(java.lang.ClassNotFoundException e) { 
				System.err.print("ClassNotFoundException: "); 
				System.err.println(e.getMessage()); 
				informacion.setText("No se pudo cargar el controlador JDBC-ODBC"); 
				} 
		}
	private void Crear_tabla() { String url = "jdbc:odbc:Personal"; 
	String createString; 
	Statement sentencia; 
	createString = "create table DATOSPERSONALES " + "(nombre varchar(20), " + "apellidos varchar(20), " + "edad varchar(3), " + "telefono varchar(10))"; 
	//Se carga el controlador 
	
	
	try { Class.forName("jdbc.odbc.JdbcOdbcDriver"); 
	} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: "); 
System.err.println(e.getMessage()); 
} try { 
	//Se carga la conexin 
	conexion = DriverManager.getConnection(url, "jdbc", "jdbc");
	sentencia = conexion.createStatement();
	//Borramos la tabla en caso de existir 
	sentencia.executeUpdate("DROP TABLE DATOSPERSONALES");
	//Creamos nuevamente la tabla 
	sentencia.executeUpdate(createString); 
	informacion.setText("Tabla creada"); 
	conexion.close(); 
	} catch(SQLException ex) {
		System.err.println("SQLException: " + ex.getMessage()); 
		} 	
	}
	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); }
		
	}
	

	public static void main(String[] args) {
		
		new DatosPersonales();			

	}
	/*Implementacion de la clase insertar*/ 
	
	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; 
			String url = "jdbc:odbc:Personal"; 
			try { conexion = DriverManager.getConnection(url, "jdbc", "jdbc"); 
			sentencia=conexion.createStatement(); 
			sentencia.executeUpdate("INSERT INTO DATOSPERSONALES "+"VALUES('"+nombre.getText()+"',"+"'"+apellidos.getText()+"',"+"'"+edad.getText()+"',"+"'"+telefono.getText()+"')"); 
			TextComponent identficacion = null;
			TextComponent direccion = null;
			TextComponent ciudad = null;
			TextComponent credito = null;
			sentencia.executeUpdate("INSERT INTO DATOSPERSONALES "+"VALUES('"+identficacion.getText()+"',"+"'"+apellidos.getText()+"',"+"'"+direccion.getText()+"',"+"'"+telefono.getText()+"',"+"'"+ciudad.getText()+"',"+"'"+credito.getText()+"')"); 
			} catch(SQLException e){} 
			}
		//Por medio de este mtodo se manejan los clicks del usuario 
		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(); 
						} 
			} 
		} /*Implementacion de la clase ver*/ 
	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() { 
				String url = "jdbc:odbc:Personal"; 
				Statement sentencia; 
				try{ 
					/*Se obtiene la conexion*/ 
					conexion = DriverManager.getConnection(url, "jdbc", "jdbc"); 
					/*Se crea una senetencia del tipo Query*/
					sentencia=conexion.createStatement(); 
					//obtenemos el resultado de lanzar la sentencia 
					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{ 
					//nos movemos a la siguiente fila o tupla 
					if(resultado.next()) ver_Datos(); 
					} catch(SQLException e){} 
				catch(Exception ex){} 
				} 
			}
		
		}
