Bueno a ver si capte un poco la idea, les voy a poner esto, son
3 archivos, una es una clase encargada de la conexion, llamado
Conexion.Java, el otro es el MDIPrincipal llamado
ventanaprincipal, desde donde todas las ventanas se llaman, y el otro es una ventanaHija llamado
DevolverForm
Conexion
Código:
[FONT=Courier New][COLOR=#000000]package paqueteprincipal;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]import java.awt.event.KeyEvent;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]import java.sql.Connection;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]import java.sql.DriverManager;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]import java.sql.ResultSet;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]import java.sql.Statement;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]import javax.swing.JOptionPane;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]public class Conexion {[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] protected static Connection conexion;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] protected static Statement sentencia;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] protected static final String Driver = "com.mysql.jdbc.Driver";[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] protected static final String Server = "localhost";[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] protected static final String DataBase = "MSDN";[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] protected static final String DSN = "jdbc:mysql://" + Server + ":3306/" + DataBase;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] protected static final String User = "usuarioMSDN";[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] protected static final String Pass = "usuario_007#";[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] public static void Abrir_Conexion(){[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]//Driver para conectarse (mysql)[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] try{[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]Class.forName(Driver).newInstance();[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] }[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] catch(Exception e){[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] JOptionPane.showMessageDialog(null, e);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] }[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] //Abrimos conexion[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] try{[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] conexion = DriverManager.getConnection(DSN, User, Pass);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] }[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] catch(Exception e){[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] JOptionPane.showMessageDialog(null, e);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] }[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] //Creamos Sentencia[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] try{[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] sentencia = conexion.createStatement(ResultSet.CONCUR_READ_ONLY,[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] ResultSet.TYPE_SCROLL_INSENSITIVE);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] }[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] catch(Exception e){[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] JOptionPane.showMessageDialog(null, e);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] }[/COLOR][/FONT]
Esta es
ventanaprincipal
Código:
[FONT=Courier New][COLOR=#000000]package paqueteprincipal;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]import javax.swing.JFrame;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]public class ventanaprincipal extends javax.swing.JFrame {[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]/** Creates new form ventanaprincipal */[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] //Esta es mi Ventana MDI[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] public ventanaprincipal() {[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] initComponents();[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] ConfiguracionVentana();[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] [B]Conexion.Abrir_Conexion();[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] private void menuDevolverMousePressed(java.awt.event.MouseEvent evt) { [/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] // TODO add your handling code here:[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]//Aqui mando llamar a la ventana interna desde aqui[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]DevolverForm VentanaInternaDevolver = new DevolverForm();[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] VentanaInternaDevolver.setResizable(false);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]VentanaInternaDevolver.setMaximizable(false);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] VentanaInternaDevolver.setIconifiable(false);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] VentanaInternaDevolver.setClosable(false);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] VentanaInternaDevolver.setSize(640, 300);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] panelInterno.add(VentanaInternaDevolver);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] VentanaInternaDevolver.setVisible(true);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] } [/COLOR][/FONT]
y esta es la
ventanahija
Código:
[FONT=Courier New][COLOR=#000000]package paqueteprincipal;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]import java.sql.ResultSet;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]import javax.swing.JOptionPane;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]import javax.swing.table.DefaultTableModel;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]public class DevolverForm extends javax.swing.JInternalFrame {[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] DefaultTableModel m;[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] /** Creates new form PlantillaForm */[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]public DevolverForm() {[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] initComponents();[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] MostrarAlquileres();[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] }[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] private void MostrarAlquileres(){[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] String SQL = "SELECT AlquilerID, SocioDNI, CodBarras, FechaAlquiler, Devuelta FROM Alquiler WHERE Devuelta = 0";[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] String Titulos[] = {"AlquilerID", "SocioDNI", "CodBarras","FechaAlquiler","Devuelta"};[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]String DatosTabla[] = new String[5];[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] m = new DefaultTableModel(null, Titulos);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] try{[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] ResultSet r = [B]Conexion.sentencia.executeQuery(SQL);[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] r.beforeFirst();[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] while (r.next()){[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]DatosTabla[0] = r.getString("AlquilerID");[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] DatosTabla[1] = r.getString("SocioDNI");[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] DatosTabla[2] = r.getString("CodBarras");[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] DatosTabla[3] = r.getString("FechaAlquiler");[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] DatosTabla[4] = r.getString("Devuelta");[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] m.addRow(DatosTabla);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] }[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] tablaAlquileres.setModel(m);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] }[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] catch(Exception e){[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] JOptionPane.showMessageDialog(rootPane, e);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] }[/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000] }[/COLOR][/FONT]
les puse en negrita lo que considero importante, ya que con lo que hice, no necesite crear ningun objeto de la clase
Conexion para poder usar el metodo
Abrir_Conexion como se ve en el constructor de la MDI
ventanaprincipal
las variables de la clase Conexion las hice de tipo
Protected que son las que solo se heredan, pero no se pueden accesar desde otros lados fuera de la clase,
Static para poder ser accesadas desde cualquier parte y
Final para que no sean modificadas desde ningun lado, es lo que se me ocurrio segun lo que vi en el video que les muestro. Espero que sea lo mas correcto, si me pudieran orientar,
Es mi primer sistema en Java, de hecho estoy basandome en uno que esta hecho para que los chicos aprendan a programar en .NET pero lo estoy haciendo con la programacion de Java
aca les pongo una captura de como va el pequeño sistema hecho con Linux y Netbeans ya compilado con su ejecutable en escritorio