Tenho um banco chamado cadastro e duas tabelas cad_cidades e cad_estados, e gostaria que depois de cadastrados, fossem exibidos em um JTable, mas esta dando um erro
olha meu codigo
[code]
package Visualizar;
import utilitarios.conexao;
import javax.swing.JOptionPane;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
public class Visualizar_Cidades_Estados extends javax.swing.JFrame {
int navega = 0;
conexao mostrar;
public Visualizar_Cidades_Estados() {
initComponents();
mostrar = new conexao();
mostrar.conecta();
mostrar.executeSQL("select * from cad_cidades"); ---------->[b]Aqui é uma tabela do banco cadastro[/b]
mostrar.executeSQL("select * from cad_estados"); ---------->[b]Aqui é outra tabela do banco[/b]
preencher_jtable(); ------------------------------------------------->[b]Aqui é o metodo de preencher o Jtable[/b]
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
painel_topo = new javax.swing.JPanel();
label_topo = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
painel_topo.setBackground(new java.awt.Color(153, 153, 153));
label_topo.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
label_topo.setText("Visualizar Cidades e Estados Cadastrados");
javax.swing.GroupLayout painel_topoLayout = new javax.swing.GroupLayout(painel_topo);
painel_topo.setLayout(painel_topoLayout);
painel_topoLayout.setHorizontalGroup(
painel_topoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(painel_topoLayout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(label_topo)
.addContainerGap(64, Short.MAX_VALUE))
);
painel_topoLayout.setVerticalGroup(
painel_topoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(painel_topoLayout.createSequentialGroup()
.addContainerGap()
.addComponent(label_topo, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"Cidades", "Estados"
}
));
jScrollPane1.setViewportView(jTable);
jButton1.setText("Fechar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 482, Short.MAX_VALUE)
.addComponent(painel_topo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(218, 218, 218)
.addComponent(jButton1)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(painel_topo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int fechar = JOptionPane.showConfirmDialog(null,"Deseja fechar? ","Visualização de dados",JOptionPane.YES_NO_OPTION);
if (fechar == JOptionPane.YES_OPTION)
{
dispose();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Visualizar_Cidades_Estados().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable;
private javax.swing.JLabel label_topo;
private javax.swing.JPanel painel_topo;
// End of variables declaration
public void preencher_jtable()
{
jTable.getColumnModel().getColumn(0).setPreferredWidth(20);
jTable.getColumnModel().getColumn(1).setPreferredWidth(20);
DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();
//criamos um objeto modelo do tipo DefaultTableModel
modelo.setNumRows(0);
//toda vez que chamar este metodo, zera tudo e preenche de novo
try
{
while (mostrar.resultset.next())
modelo.addRow(new Object[]{mostrar.resultset.getString("cid_nome")
,mostrar.resultset.getString("est_nome")});
mostrar.resultset.first();
}
catch(SQLException erro)
{
JOptionPane.showMessageDialog(null,"Erro ao listar");
}
}
}[/code]