hola que tal amigos , quisiera pedirles ayuda para desarrollar este problema, lo que pasa es que deseo que me carge en un jtable la tabla que tengo de mi base de datos , es decir mi proyecto se conecta netbeans6.8 con sql server2008 para insertar datos como codigo ,nombre ,descripcion y pues entro a sql y agrega los valores que coloco ahi, pero el problema es que deseo a mi proyecto colocar un jtable d modo que cuando levante el proyecto tambien lo haga en el jtable , a continuacion les mando mi codigo , si por favorme ayudaran , es muy importante ...grax
package ejercicio;
import java.sql.*; //importando librerias de sql
import javax.swing.*; //importando librerias de swing
public class edgar extends javax.swing.JFrame {
static Connection con=null;
static Statement st=null;
static ResultSet rs=null;
public String cmdSQL="";
boolean op=false;
private String cmdsql;
public void mensaje (String msj){
JOptionPane.showMessageDialog(null, msj);
}
public static boolean confirma(String msj){
int r=JOptionPane.showConfirmDialog(null,null,"",JOpti onPane.YES_NO_OPTION);
if(r==JOptionPane.YES_NO_OPTION)
return true;
else
return false;
}
public void mostrar(){
try{
this.jTextField1.setText(rs.getString("codigo"));
this.jTextField2.setText(rs.getString("nombre"));
this.jTextField3.setText(rs.getString("descripcion "));
this.jTextField4.setText(rs.getString("celeste"));
}catch(Exception e){
}
}
public edgar() {
initComponents();
iniciar();
try{
conectar();
}catch(Exception e){
}
}
public void conectar(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver");
con=DriverManager.getConnection("jdbc:sqlserver://localhost;"+"databaseName=SOLITARIO; user=sa; password=123");
JOptionPane.showMessageDialog(null,"EXITO","Ingres ando",1);
st=con.createStatement(ResultSet.TYPE_SCROLL_INSEN SITIVE,ResultSet.CONCUR_UPDATABLE);
}catch(Exception e){
}
}
private void iniciar(){
this.setLocation(200,200);
this.setVisible(true);
this.setSize(600,500);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jTextField4 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jLabel1.setText("CODIGO");
getContentPane().add(jLabel1);
jLabel1.setBounds(80, 40, 90, 14);
jLabel2.setText("NOMBRE");
getContentPane().add(jLabel2);
jLabel2.setBounds(80, 70, 80, 14);
jLabel3.setText("DESCRIPCION");
getContentPane().add(jLabel3);
jLabel3.setBounds(80, 100, 100, 14);
getContentPane().add(jTextField1);
jTextField1.setBounds(210, 30, 150, 20);
getContentPane().add(jTextField2);
jTextField2.setBounds(210, 60, 150, 20);
getContentPane().add(jTextField3);
jTextField3.setBounds(210, 90, 150, 20);
jButton1.setText("GUARDAR");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(40, 170, 90, 30);
jButton2.setText("NUEVO");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(160, 170, 90, 30);
jButton3.setText("BUSCAR");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(290, 170, 80, 30);
jButton6.setText("SALIR");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
getContentPane().add(jButton6);
jButton6.setBounds(420, 170, 80, 30);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"CODIGO", "NOMBRE", "DESCRIPCION", "C.CELESTE"
}
));
jScrollPane1.setViewportView(jTable1);
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(20, 210, 510, 130);
jLabel4.setText("OLIMPO");
getContentPane().add(jLabel4);
jLabel4.setBounds(420, 10, 100, 30);
jLabel5.setText("C.CELESTE");
getContentPane().add(jLabel5);
jLabel5.setBounds(80, 130, 80, 14);
getContentPane().add(jTextField4);
jTextField4.setBounds(210, 120, 150, 20);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
try{
String cod=String.valueOf(this.jTextField1.getText().trim ());
String nom=String.valueOf(this.jTextField2.getText().trim ());
String ape=String.valueOf(this.jTextField3.getText().trim ());
CallableStatement proc=con.prepareCall("{call guardar(?,?,?}");
proc.setString(1,cod);
proc.setString(2,nom);
proc.setString(3,ape);
proc.execute();
mensaje("Guardado Correctamente");
}catch(Exception x){
mensaje("ERROR en guardar"+ x.getMessage());
}
}//GEN-LAST:event_jButton1ActionPerformed
}//GEN-LAST:event_jButton3ActionPerformed
}//GEN-LAST:event_jButton6ActionPerformed
}//GEN-LAST:event_jButton2ActionPerformed
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 {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClass Name());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(edgar.class.get Name()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(edgar.class.get Name()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(edgar.class.get Name()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(edgar.class.get Name()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new edgar().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton6;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration//GEN-END:variables
}