Muy buenas, estoy haciendo un programa que hace lo siguiente, tengo una clase llamada Persona.java, donde tengo una serie de métodos (Persona, conectar, cerrarSentencia, cerrarConexion, Personac (esta tiene una sentencia SQL SELECT*FROM personas WHERE nombre LIKE...), set, guardar) tengo una clase JFrame donde tengo un elemento Jlist y quiero hacer que cuando se inicie el programa se cargue en la lista los nombres que están almacenados en la base de datos.
Clase Persona:
import java.sql.SQLException;
/*
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ramondevesa
*/
public class Persona {
String Nombre, Telefono, Sexo, Foto
;
public void Persona() throws
String controlador
= "com.mysql.jdbc.Driver"; Class.forName(controlador).newInstance();
conectar();
crearSentencia();
}
String URL_bd
= "jdbc:mysql://localhost:8889/examen3ar";
URL_bd, usuario, contraseña);
System.
out.
println("\nConexion realizada con éxito."); }
sentenciaSQL = conexion.createStatement();
System.
out.
println("\nSentencia creada con éxito."); }
if(cdr != null) cdr.close();
if(sentenciaSQL != null) sentenciaSQL.close();
if(conexion != null) conexion.close();
System.
out.
println("\nConexión cerrada con éxito."); }
String sql
= "SELECT * FROM personas WHERE nombre LIKE ('%" + nombre
+ "%')"; cdr = sentenciaSQL.executeQuery(sql);
Persona persona01 = new Persona();
return cdr;
}
{
Nombre=nombre_;
Telefono = telefono_;
Sexo = sexo_;
Foto = foto_;
}
/*public void guardar(String nombre_, String telefono_, String sexo_, String foto_) throws java.sql.SQLException{
String sql = "INSNERT INTO personas ('"+nombre_+"', '"+telefono_+"', '"+sexo_+"', "+foto_+"')";
cdr = sentenciaSQL.executeQuery(sql);
}*/
String sql
="INSERT INTO personas VALUES('"+nombre_
+"', '"+telefono_
+"', '"+sexo_
+"', '"+foto_
+"')"; cdr = sentenciaSQL.executeQuery(sql);
}
}
Clase JFrame:
import javax.swing.*;
/*
* IGPersona.java
*
* Created on 01-jun-2012, 23:31:09
*/
/**
*
* @author ramondevesa
*/
public class IGPersona
extends javax.
swing.
JFrame {
private static Persona BD;
private static java.
util.
Scanner leer
= new java.
util.
Scanner(System.
in);
/** Creates new form IGPersona */
public IGPersona() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
label_nombre
= new javax.
swing.
JLabel(); label_telefono
= new javax.
swing.
JLabel(); label_sexo
= new javax.
swing.
JLabel(); btnExaminar
= new javax.
swing.
JButton(); pImagen
= new javax.
swing.
JPanel(); btnVaciar
= new javax.
swing.
JButton(); btnGuardar
= new javax.
swing.
JButton(); jList1
= new javax.
swing.
JList();
label_nombre.setText("Nombre:");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { txtNombreActionPerformed(evt);
}
});
label_telefono.setText("Teléfono:");
label_sexo.setText("Sexo:");
buttonGroup1.add(GHombre);
GHombre.setText("Hombre");
buttonGroup1.add(GMujer);
GMujer.setText("Mujer");
btnExaminar.setText("Examinar");
pImagen.
setBorder(javax.
swing.
BorderFactory.
createTitledBorder("Foto"));
org.jdesktop.layout.GroupLayout pImagenLayout = new org.jdesktop.layout.GroupLayout(pImagen);
pImagen.setLayout(pImagenLayout);
pImagenLayout.setHorizontalGroup(
pImagenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.
add(0,
106,
Short.
MAX_VALUE) );
pImagenLayout.setVerticalGroup(
pImagenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.
add(0,
119,
Short.
MAX_VALUE) );
btnVaciar.setText("Vaciar");
btnGuardar.setText("Guardar");
jScrollPane1.setViewportView(jList1);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(label_nombre)
.add(label_telefono)
.add(label_sexo))
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.
add(org.
jdesktop.
layout.
GroupLayout.
TRAILING, txtTelefono, org.
jdesktop.
layout.
GroupLayout.
DEFAULT_SIZE,
247,
Short.
MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.TRAILING, txtNombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 247, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(layout.createSequentialGroup()
.add(GHombre)
.add(18, 18, 18)
.add(GMujer)
.add(98, 98, 98))
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(btnVaciar)
.
addPreferredGap(org.
jdesktop.
layout.
LayoutStyle.
RELATED, org.
jdesktop.
layout.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE) .add(btnGuardar))
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(btnExaminar)
.add(18, 18, 18)
.add(pImagen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.add(27, 27, 27)))
.add(18, 18, 18)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 271, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.
linkSize(new java.
awt.
Component[] {txtNombre, txtTelefono
}, org.
jdesktop.
layout.
GroupLayout.
HORIZONTAL);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.
add(jScrollPane1, org.
jdesktop.
layout.
GroupLayout.
DEFAULT_SIZE,
390,
Short.
MAX_VALUE) .addContainerGap())
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(label_nombre)
.add(txtNombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(label_telefono)
.add(txtTelefono, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(label_sexo)
.add(GHombre)
.add(GMujer))
.add(30, 30, 30)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(btnExaminar)
.add(pImagen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.
addPreferredGap(org.
jdesktop.
layout.
LayoutStyle.
RELATED, org.
jdesktop.
layout.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(btnVaciar)
.add(btnGuardar))
.add(92, 92, 92))))
);
pack();
}// </editor-fold>
private void txtNombreActionPerformed
(java.
awt.
event.
ActionEvent evt
) { // TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
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 {
if ("Nimbus".equals(info.getName())) {
javax.
swing.
UIManager.
setLookAndFeel(info.
getClassName()); break;
}
}
java.util.logging.Logger.getLogger(IGPersona.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(IGPersona.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(IGPersona.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(IGPersona.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
public void run() {
new IGPersona().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.
swing.
JButton btnExaminar
; private javax.
swing.
JButton btnGuardar
; private javax.
swing.
JButton btnVaciar
; private javax.
swing.
JList jList1
; private javax.
swing.
JLabel label_nombre
; private javax.
swing.
JLabel label_sexo
; private javax.
swing.
JLabel label_telefono
; private javax.
swing.
JPanel pImagen
; // End of variables declaration
}