6

Click here to load reader

Base Datos Completo

Embed Size (px)

Citation preview

Page 1: Base Datos Completo

Interfaz

package interfaz; import java.rmi.Remote; import java.rmi.RemoteException; import java.util.ArrayList; public interface InterfazDatos extends Remote{ public void Nuevo(String n,String a,String d)throws RemoteException; public void actualizar(int id, String n, String a,String d) throws RemoteException; public ArrayList Listar()throws RemoteException; public void eliminar(int id) throws RemoteException; }

Servidor

package servidor; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import com.mysql.jdbc.Statement; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; public class ServidorDatos extends UnicastRemoteObject implements interfaz.InterfazDatos { public ServidorDatos() throws RemoteException { super(); } public static void main(String[] args) throws RemoteException { try { //Create and get reference to rmi registry Registry registry = LocateRegistry.createRegistry(1099);

Page 2: Base Datos Completo

//Instantiate server object ServidorDatos servidor = new ServidorDatos(); //Register server object registry.rebind("server", servidor); System.out.println("Servidor Iniciado!!!"); } catch (Exception e) { System.out.println(e); } } @Override public void Nuevo(String n, String a, String d) throws RemoteException { //conectar con mysql Connection conn = null; PreparedStatement comando = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); //conexion = DriverManager.getConnection(server,user,password); try { conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/datos", "root", "Radamanti$"); try { comando = (PreparedStatement) conn.prepareStatement("insert into cliente (nombre,apellidos,direccion) values('" + n + "','" + a + "','" + d + "')"); comando.execute(); comando.close(); } catch (Exception e1) { System.out.println("Error Statement"); } } catch (Exception e2) { System.out.println("Error Conexion"); } } catch (Exception e3) { System.out.println("Error Driver"); } } @Override public void actualizar(int id, String n, String a, String d) throws RemoteException { //conectar con mysql Connection conn = null; PreparedStatement comando = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance();

Page 3: Base Datos Completo

//conexion = DriverManager.getConnection(server,user,password); try { conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/datos", "root", "Radamanti$"); try { comando = (PreparedStatement) conn.prepareStatement("update cliente set nombre='" + n + "',apellidos='" + a + "',direccion='" + d + "' where idcliente=" + id); comando.execute(); comando.close(); } catch (Exception e1) { System.out.println("Error Statement"); } } catch (Exception e2) { System.out.println("Error Conexion"); } } catch (Exception e3) { System.out.println("Error Driver"); } } @Override public ArrayList Listar() throws RemoteException { //conectar con mysql ArrayList arreglo = new ArrayList(); Connection conn = null; PreparedStatement comando = null; ResultSet resultado = null; String nombre = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); //conexion = DriverManager.getConnection(server,user,password); try { conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/datos", "root", "Radamanti$"); try { comando = (PreparedStatement) conn.prepareStatement("select * from cliente"); resultado = comando.executeQuery(); while (resultado.next()) { nombre = resultado.getString(2); arreglo.add(nombre); } comando.close(); } catch (Exception e1) { System.out.println("Error Resultset"); }

Page 4: Base Datos Completo

} catch (Exception e2) { System.out.println("Error Conexion"); } } catch (Exception e3) { System.out.println("Error Driver"); } return arreglo; } @Override public void eliminar(int id) throws RemoteException { //conectar con mysql Connection conn = null; PreparedStatement comando = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); //conexion = DriverManager.getConnection(server,user,password); try { conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/datos", "root", "Radamanti$"); try { comando = (PreparedStatement) conn.prepareStatement("delete from cliente where idcliente=" + id); comando.execute(); comando.close(); } catch (Exception e1) { System.out.println("Error Statement"); } } catch (Exception e2) { System.out.println("Error Conexion"); } } catch (Exception e3) { System.out.println("Error Driver"); } } }

Cliente

package cliente;

Page 5: Base Datos Completo

import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.util.ArrayList; public class ClienteDatos { public static ClienteDatos cliente; interfaz.InterfazDatos interfaz; public static void main(String[] args) throws RemoteException { cliente = new ClienteDatos(); cliente.conectar(); MenuCliente ventanita = new MenuCliente(); ventanita.setVisible(true); } public void conectar() throws RemoteException { try { Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099); interfaz = (interfaz.InterfazDatos) registry.lookup("server"); } catch (Exception e) { e.printStackTrace(); } } public void llenar(String n1, String a1, String d1) throws RemoteException { interfaz.Nuevo(n1, a1, d1); } public void actualizar(int id, String n1, String a1, String d1) throws RemoteException { interfaz.actualizar(id, n1, a1, d1); } public void elimina(int id) throws RemoteException { interfaz.eliminar(id); } public ArrayList Listado() throws RemoteException { return interfaz.Listar(); } }

ClienteVentana

Page 6: Base Datos Completo

Código Boton

private void btnListadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListadoActionPerformed DefaultTableModel temp = (DefaultTableModel) tablaDatos.getModel(); try { ArrayList arreglo = ClienteDatos.cliente.Listado(); for (int i = 0; i < arreglo.size(); i++) { Object nuevo[] = {arreglo.get(i), "", ""}; temp.addRow(nuevo); } } catch (RemoteException ex) { Logger.getLogger(ClienteVentana.class.getName()).log(Level.SEVERE, null, ex); }