Estou com uma pequena duvida neste código, toda vez em que eu clico em excluir, alterar ou gravar , ele aparece A mensagem de erro o ComboBox em que criei! pois criei um metodo de atualizar os dados do ComboBox e com isso ele da o erro !
Codigo abaixo :
*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Cadastros;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import utilitarios.conexao;
public class cad_cidade extends javax.swing.JFrame {
conexao conecta_cidade;
int navega = 0;
int inicia_combo = 0;
public cad_cidade() {
initComponents();
conecta_cidade = new conexao();
conecta_cidade.conecta();
conecta_cidade.executeSQL("select * from cidade");
try {
while( conecta_cidade.resultset.next()){
comboBox_pesq.addItem(conecta_cidade.resultset.get String("nome"));
}
conecta_cidade.resultset.first();
navega = 1;
mostra_dados();
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não localizou dados "+ex);
}
preencher_table();
}
/**
* 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_titulo = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
label_codigo = new javax.swing.JLabel();
label_nome = new javax.swing.JLabel();
textField_cod = new javax.swing.JTextField();
textField_nome = new javax.swing.JTextField();
label_uf = new javax.swing.JLabel();
comboBox_uf = new javax.swing.JComboBox();
buttun_gravar = new javax.swing.JButton();
buttun_inserir = new javax.swing.JButton();
buttun_excluir = new javax.swing.JButton();
buttun_alterar = new javax.swing.JButton();
jSeparator2 = new javax.swing.JSeparator();
jSeparator3 = new javax.swing.JSeparator();
buttao_ultimo = new javax.swing.JButton();
buttao_anterior = new javax.swing.JButton();
buttao_proximo = new javax.swing.JButton();
buttao_primeiro = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
comboBox_pesq = new javax.swing.JComboBox();
buttun_pesquisar = new javax.swing.JButton();
textField_pesq = new javax.swing.JTextField();
label_pesquisar = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstan ts.DISPOSE_ON_CLOSE);
setTitle("Manutenção do Cadastro de Cidades");
label_titulo.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
label_titulo.setForeground(new java.awt.Color(255, 0, 0));
label_titulo.setText("Manutenção do Cadastro de Cidades");
label_codigo.setText("Codigo.:");
label_nome.setText("Nome.:");
label_uf.setText("UF.:");
comboBox_uf.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "UF", "RS", "SC", "DF", "PR", "SP", "RJ", "TO", "MG", "GO", " " }));
buttun_gravar.setText("Gravar");
buttun_gravar.setToolTipText("Gravar o Registro");
buttun_gravar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttun_gravarActionPerformed(evt);
}
});
buttun_inserir.setText("Inserir");
buttun_inserir.setToolTipText("Inserir Registros");
buttun_inserir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttun_inserirActionPerformed(evt);
}
});
buttun_excluir.setText("Excluir");
buttun_excluir.setToolTipText("Excluir Registro");
buttun_excluir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttun_excluirActionPerformed(evt);
}
});
buttun_alterar.setText("Alterar");
buttun_alterar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttun_alterarActionPerformed(evt);
}
});
buttao_ultimo.setText(">>");
buttao_ultimo.setToolTipText("vai para o Ultimo Registro");
buttao_ultimo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttao_ultimoActionPerformed(evt);
}
});
buttao_anterior.setText("<");
buttao_anterior.setToolTipText("Vai para o Registro Anterior");
buttao_anterior.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttao_anteriorActionPerformed(evt);
}
});
buttao_proximo.setText(">");
buttao_proximo.setToolTipText("vai para o proximo Registro");
buttao_proximo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttao_proximoActionPerformed(evt);
}
});
buttao_primeiro.setText("<<");
buttao_primeiro.setToolTipText("Vai para o primeiro Registro");
buttao_primeiro.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttao_primeiroActionPerformed(evt);
}
});
jPanel1.setBackground(new java.awt.Color(255, 102, 102));
comboBox_pesq.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
comboBox_pesqActionPerformed(evt);
}
});
buttun_pesquisar.setText("Pesquisar");
buttun_pesquisar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttun_pesquisarActionPerformed(evt);
}
});
textField_pesq.setToolTipText("Digite as iniciais da Cidade para localizar");
textField_pesq.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textField_pesqActionPerformed(evt);
}
});
label_pesquisar.setText("Pesquisar.:");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE)
.addComponent(label_pesquisar)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(textField_pesq, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(buttun_pesquisar)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(comboBox_pesq, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(comboBox_pesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttun_pesquisar)
.addComponent(textField_pesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(label_pesquisar))
.addContainerGap())
);
jTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null}
},
new String [] {
"Código", "Nome da Cidade", "UF"
}
));
jScrollPane1.setViewportView(jTable);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1))
.addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(jSeparator1)
.addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
.addComponent(buttun_gravar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttao_primeiro, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(buttun_inserir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttao_anterior, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
.addComponent(buttun_excluir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttao_proximo, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(buttun_alterar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttao_ultimo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(label_codigo)
.addComponent(label_nome)
.addComponent(label_uf))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
.addComponent(comboBox_uf, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textField_nome, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
.addComponent(textField_cod))
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING)))
.addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(label_titulo)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(label_titulo)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(label_codigo)
.addComponent(textField_cod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(label_nome)
.addComponent(textField_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(label_uf)
.addComponent(comboBox_uf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(buttao_primeiro)
.addComponent(buttao_anterior)
.addComponent(buttao_proximo)
.addComponent(buttao_ultimo))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(buttun_gravar)
.addComponent(buttun_inserir)
.addComponent(buttun_excluir)
.addComponent(buttun_alterar))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(23, Short.MAX_VALUE))
);
setBounds(0, 0, 600, 631);
}// </editor-fold>
private void buttao_proximoActionPerformed(java.awt.event.Actio nEvent evt) {
try {
conecta_cidade.resultset.next();
mostra_dados();
navega = 2;
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não foi possivel ir para proximo registro!");
}
}
private void buttao_primeiroActionPerformed(java.awt.event.Acti onEvent evt) {
try {
conecta_cidade.resultset.first();
mostra_dados();
navega= 1;
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não foi possivel ir para primeiro registro!");
}
}
private void buttao_ultimoActionPerformed(java.awt.event.Action Event evt) {
try {
conecta_cidade.resultset.last();
mostra_dados();
navega = 2;
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não foi possivel ir para o ultimo registro!");
}
}
private void buttao_anteriorActionPerformed(java.awt.event.Acti onEvent evt) {
try {
conecta_cidade.resultset.previous();
mostra_dados();
navega = 1;
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não foi possivel ir para o registro anterior!");
}
}
private void buttun_inserirActionPerformed(java.awt.event.Actio nEvent evt) {
textField_cod.setText("");
textField_nome.setText("");
comboBox_uf.setSelectedItem("RS");
textField_nome.requestFocus();
textField_cod.setEditable(false);
}
private void buttun_gravarActionPerformed(java.awt.event.Action Event evt) {
try{
String sqlinsert = "insert into cidade (nome,uf) values("
+ "'"+textField_nome.getText()+"'"
+ ",'"+comboBox_uf.getSelectedItem()+"')";
conecta_cidade.statement.executeUpdate(sqlinsert);
JOptionPane.showMessageDialog(null, "Gravação realizado com sucesso!");
atualiza_cb_pesq();
conecta_cidade.resultset = conecta_cidade.statement.executeQuery("Select * from cidade");
conecta_cidade.resultset.first();
}
catch(SQLException erro){
JOptionPane.showMessageDialog(null, "Erro ao tentar Gravar o Registro!");
}
}
private void buttun_excluirActionPerformed(java.awt.event.Actio nEvent evt) {
try{
String sql = "Select * from cidade where codigo = "+textField_cod.getText();
conecta_cidade.executeSQL(sql);
conecta_cidade.resultset.first();
String nome = "Deletar a Cidade : "+conecta_cidade.resultset.getString("nome");
int opcao_escolhida = JOptionPane.showConfirmDialog(null, nome," Exclusão ",JOptionPane.YES_NO_OPTION);
if( opcao_escolhida == JOptionPane.YES_OPTION){
sql = "DELETE FROM cidade where codigo ="+textField_cod.getText();
int conseguiu_excluir = conecta_cidade.statement.executeUpdate(sql);
if(conseguiu_excluir == 1){
JOptionPane.showMessageDialog(null, "Exclusão realizada com sucesso!");
conecta_cidade.executeSQL("Select * from cidade");
atualiza_cb_pesq();
conecta_cidade.resultset.first();
mostra_dados();
}
}
else{
return;
}
}
catch(SQLException erro){
JOptionPane.showMessageDialog(null,"Erro ao tentar excluir o registro !");
}
}
private void buttun_alterarActionPerformed(java.awt.event.Actio nEvent evt) {
try{
String sql = "UPDATE cidade SET nome ='"+textField_nome.getText()+"',"+
"uf = '"+comboBox_uf.getSelectedItem()+"' where codigo = "+textField_cod.getText();
conecta_cidade.statement.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "Alteração realizada com sucesso!");
conecta_cidade.executeSQL("Select * from cidade");
atualiza_cb_pesq();
conecta_cidade.resultset.next();
mostra_dados();
}
catch( SQLException erro){
JOptionPane.showMessageDialog(null, "Erro ao tentar Alterar o registro!");
}
}
private void buttun_pesquisarActionPerformed(java.awt.event.Act ionEvent evt) {
try{
conecta_cidade.resultset.first();
String igual = "n";
int tamanho_pesquisa = textField_pesq.getText().length();
while(igual == "n"){
String pesquisando = conecta_cidade.resultset.getString("nome").substri ng(0,(tamanho_pesquisa));
if(pesquisando.equals(textField_pesq.getText())){
igual = "s";
}
else{
conecta_cidade.resultset.next();
}
}
textField_cod.setText(conecta_cidade.resultset.get String("codigo"));
mostra_dados();
}
catch(Exception erro){
JOptionPane.showMessageDialog(null, "Nenhum registro Encontrado ");
}
}
private void comboBox_pesqActionPerformed(java.awt.event.Action Event evt) {
if( inicia_combo == 1){
try{
conecta_cidade.resultset.first();
String igual = "n";
while("n".equals(igual) ){
if( conecta_cidade.resultset.getString("nome").equals( comboBox_pesq.getSelectedItem())){
igual = "s";
}
else{
conecta_cidade.resultset.next();
}
}
textField_cod.setText(conecta_cidade.resultset.get String("codigo"));
mostra_dados();
}
// este é o erro em que aparece logo apos em que eu pucho o metodo atualiza dados
catch(Exception erro){
JOptionPane.showMessageDialog(null, "Não conseguiu localizar via digitação ");
}
}
inicia_combo = 1;
}
private void textField_pesqActionPerformed(java.awt.event.Actio nEvent evt) {
try{
conecta_cidade.resultset.first();
String igual = "n";
int tamanho_pesquisa = textField_pesq.getText().length();
while("n".equals(igual)){
String pesquisando = conecta_cidade.resultset.getString("nome").substri ng(0,(tamanho_pesquisa));
if(pesquisando.equals(textField_pesq.getText())){
igual = "s";
}
else{
conecta_cidade.resultset.next();
}
}
textField_cod.setText(conecta_cidade.resultset.get String("codigo"));
mostra_dados();
comboBox_pesq.setSelectedItem((textField_nome.getT ext()));
}
catch(Exception erro){
JOptionPane.showMessageDialog(null, "Nenhum registro Encontrado ");
}
}
/**
* @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 Apenas usuários registrados e ativados podem ver os links., Clique aqui para se cadastrar...
*/
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(cad_cidade.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(cad_cidade.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(cad_cidade.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(cad_cidade.clas s.getName()).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 cad_cidade().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton buttao_anterior;
private javax.swing.JButton buttao_primeiro;
private javax.swing.JButton buttao_proximo;
private javax.swing.JButton buttao_ultimo;
private javax.swing.JButton buttun_alterar;
private javax.swing.JButton buttun_excluir;
private javax.swing.JButton buttun_gravar;
private javax.swing.JButton buttun_inserir;
private javax.swing.JButton buttun_pesquisar;
private javax.swing.JComboBox comboBox_pesq;
private javax.swing.JComboBox comboBox_uf;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JTable jTable;
private javax.swing.JLabel label_codigo;
private javax.swing.JLabel label_nome;
private javax.swing.JLabel label_pesquisar;
private javax.swing.JLabel label_titulo;
private javax.swing.JLabel label_uf;
private javax.swing.JTextField textField_cod;
private javax.swing.JTextField textField_nome;
private javax.swing.JTextField textField_pesq;
// End of variables declaration
public void mostra_dados () throws SQLException{
try {
textField_cod.setText(conecta_cidade.resultset.get String("codigo"));
textField_nome.setText(conecta_cidade.resultset.ge tString("nome"));
comboBox_uf.setSelectedItem(conecta_cidade.results et.getString("uf"));
}
catch (SQLException ex) {
if( navega == 1){
JOptionPane.showMessageDialog(null, "Você ja esta no primeiro registro!");
conecta_cidade.resultset.first();
}else{
if(navega == 2){
JOptionPane.showMessageDialog(null, "Você ja esta no ultimo registro!");
conecta_cidade.resultset.previous();
}
else{
JOptionPane.showMessageDialog(null, "Não localizou dados "+ex);
}
}
}
}
public void preencher_table(){
//especificar o tamanho cokuna
jTable.getColumnModel().getColumn(0).setPreferredW idth(20);
jTable.getColumnModel().getColumn(1).setPreferredW idth(150);
jTable.getColumnModel().getColumn(2).setPreferredW idth(20);
DefaultTableModel modelo = (DefaultTableModel) jTable.getModel();
modelo.setNumRows(0);
try{
while(conecta_cidade.resultset.next()){
modelo.addRow(new Object[]{conecta_cidade.resultset.getString("codigo"),cone cta_cidade.resultset.getString("nome"),conecta_cid ade.resultset.getString("uf")});
}
conecta_cidade.resultset.first();
}
catch(SQLException erro){
JOptionPane.showMessageDialog(null, "Erro ao listar no JTable! \n Erro :"+erro);
}
}
public void atualiza_cb_pesq(){
try {
// estou com duvida neste removeAllItems();
comboBox_pesq.removeAllItems();
conecta_cidade.resultset.first();
conecta_cidade.executeSQL("select * from cidade");
while( conecta_cidade.resultset.next()){
comboBox_pesq.addItem(conecta_cidade.resultset.get String("nome"));
}
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não localizou dados "+ex);
}
}
}
Codigo abaixo :
*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Cadastros;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import utilitarios.conexao;
public class cad_cidade extends javax.swing.JFrame {
conexao conecta_cidade;
int navega = 0;
int inicia_combo = 0;
public cad_cidade() {
initComponents();
conecta_cidade = new conexao();
conecta_cidade.conecta();
conecta_cidade.executeSQL("select * from cidade");
try {
while( conecta_cidade.resultset.next()){
comboBox_pesq.addItem(conecta_cidade.resultset.get String("nome"));
}
conecta_cidade.resultset.first();
navega = 1;
mostra_dados();
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não localizou dados "+ex);
}
preencher_table();
}
/**
* 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_titulo = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
label_codigo = new javax.swing.JLabel();
label_nome = new javax.swing.JLabel();
textField_cod = new javax.swing.JTextField();
textField_nome = new javax.swing.JTextField();
label_uf = new javax.swing.JLabel();
comboBox_uf = new javax.swing.JComboBox();
buttun_gravar = new javax.swing.JButton();
buttun_inserir = new javax.swing.JButton();
buttun_excluir = new javax.swing.JButton();
buttun_alterar = new javax.swing.JButton();
jSeparator2 = new javax.swing.JSeparator();
jSeparator3 = new javax.swing.JSeparator();
buttao_ultimo = new javax.swing.JButton();
buttao_anterior = new javax.swing.JButton();
buttao_proximo = new javax.swing.JButton();
buttao_primeiro = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
comboBox_pesq = new javax.swing.JComboBox();
buttun_pesquisar = new javax.swing.JButton();
textField_pesq = new javax.swing.JTextField();
label_pesquisar = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstan ts.DISPOSE_ON_CLOSE);
setTitle("Manutenção do Cadastro de Cidades");
label_titulo.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
label_titulo.setForeground(new java.awt.Color(255, 0, 0));
label_titulo.setText("Manutenção do Cadastro de Cidades");
label_codigo.setText("Codigo.:");
label_nome.setText("Nome.:");
label_uf.setText("UF.:");
comboBox_uf.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "UF", "RS", "SC", "DF", "PR", "SP", "RJ", "TO", "MG", "GO", " " }));
buttun_gravar.setText("Gravar");
buttun_gravar.setToolTipText("Gravar o Registro");
buttun_gravar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttun_gravarActionPerformed(evt);
}
});
buttun_inserir.setText("Inserir");
buttun_inserir.setToolTipText("Inserir Registros");
buttun_inserir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttun_inserirActionPerformed(evt);
}
});
buttun_excluir.setText("Excluir");
buttun_excluir.setToolTipText("Excluir Registro");
buttun_excluir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttun_excluirActionPerformed(evt);
}
});
buttun_alterar.setText("Alterar");
buttun_alterar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttun_alterarActionPerformed(evt);
}
});
buttao_ultimo.setText(">>");
buttao_ultimo.setToolTipText("vai para o Ultimo Registro");
buttao_ultimo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttao_ultimoActionPerformed(evt);
}
});
buttao_anterior.setText("<");
buttao_anterior.setToolTipText("Vai para o Registro Anterior");
buttao_anterior.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttao_anteriorActionPerformed(evt);
}
});
buttao_proximo.setText(">");
buttao_proximo.setToolTipText("vai para o proximo Registro");
buttao_proximo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttao_proximoActionPerformed(evt);
}
});
buttao_primeiro.setText("<<");
buttao_primeiro.setToolTipText("Vai para o primeiro Registro");
buttao_primeiro.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttao_primeiroActionPerformed(evt);
}
});
jPanel1.setBackground(new java.awt.Color(255, 102, 102));
comboBox_pesq.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
comboBox_pesqActionPerformed(evt);
}
});
buttun_pesquisar.setText("Pesquisar");
buttun_pesquisar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttun_pesquisarActionPerformed(evt);
}
});
textField_pesq.setToolTipText("Digite as iniciais da Cidade para localizar");
textField_pesq.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textField_pesqActionPerformed(evt);
}
});
label_pesquisar.setText("Pesquisar.:");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE)
.addComponent(label_pesquisar)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(textField_pesq, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(buttun_pesquisar)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(comboBox_pesq, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(comboBox_pesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttun_pesquisar)
.addComponent(textField_pesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(label_pesquisar))
.addContainerGap())
);
jTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null}
},
new String [] {
"Código", "Nome da Cidade", "UF"
}
));
jScrollPane1.setViewportView(jTable);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1))
.addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(jSeparator1)
.addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
.addComponent(buttun_gravar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttao_primeiro, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(buttun_inserir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttao_anterior, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
.addComponent(buttun_excluir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttao_proximo, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(buttun_alterar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttao_ultimo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(label_codigo)
.addComponent(label_nome)
.addComponent(label_uf))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
.addComponent(comboBox_uf, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textField_nome, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
.addComponent(textField_cod))
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING)))
.addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(label_titulo)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(label_titulo)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(label_codigo)
.addComponent(textField_cod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(label_nome)
.addComponent(textField_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(label_uf)
.addComponent(comboBox_uf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(buttao_primeiro)
.addComponent(buttao_anterior)
.addComponent(buttao_proximo)
.addComponent(buttao_ultimo))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(buttun_gravar)
.addComponent(buttun_inserir)
.addComponent(buttun_excluir)
.addComponent(buttun_alterar))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(23, Short.MAX_VALUE))
);
setBounds(0, 0, 600, 631);
}// </editor-fold>
private void buttao_proximoActionPerformed(java.awt.event.Actio nEvent evt) {
try {
conecta_cidade.resultset.next();
mostra_dados();
navega = 2;
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não foi possivel ir para proximo registro!");
}
}
private void buttao_primeiroActionPerformed(java.awt.event.Acti onEvent evt) {
try {
conecta_cidade.resultset.first();
mostra_dados();
navega= 1;
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não foi possivel ir para primeiro registro!");
}
}
private void buttao_ultimoActionPerformed(java.awt.event.Action Event evt) {
try {
conecta_cidade.resultset.last();
mostra_dados();
navega = 2;
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não foi possivel ir para o ultimo registro!");
}
}
private void buttao_anteriorActionPerformed(java.awt.event.Acti onEvent evt) {
try {
conecta_cidade.resultset.previous();
mostra_dados();
navega = 1;
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não foi possivel ir para o registro anterior!");
}
}
private void buttun_inserirActionPerformed(java.awt.event.Actio nEvent evt) {
textField_cod.setText("");
textField_nome.setText("");
comboBox_uf.setSelectedItem("RS");
textField_nome.requestFocus();
textField_cod.setEditable(false);
}
private void buttun_gravarActionPerformed(java.awt.event.Action Event evt) {
try{
String sqlinsert = "insert into cidade (nome,uf) values("
+ "'"+textField_nome.getText()+"'"
+ ",'"+comboBox_uf.getSelectedItem()+"')";
conecta_cidade.statement.executeUpdate(sqlinsert);
JOptionPane.showMessageDialog(null, "Gravação realizado com sucesso!");
atualiza_cb_pesq();
conecta_cidade.resultset = conecta_cidade.statement.executeQuery("Select * from cidade");
conecta_cidade.resultset.first();
}
catch(SQLException erro){
JOptionPane.showMessageDialog(null, "Erro ao tentar Gravar o Registro!");
}
}
private void buttun_excluirActionPerformed(java.awt.event.Actio nEvent evt) {
try{
String sql = "Select * from cidade where codigo = "+textField_cod.getText();
conecta_cidade.executeSQL(sql);
conecta_cidade.resultset.first();
String nome = "Deletar a Cidade : "+conecta_cidade.resultset.getString("nome");
int opcao_escolhida = JOptionPane.showConfirmDialog(null, nome," Exclusão ",JOptionPane.YES_NO_OPTION);
if( opcao_escolhida == JOptionPane.YES_OPTION){
sql = "DELETE FROM cidade where codigo ="+textField_cod.getText();
int conseguiu_excluir = conecta_cidade.statement.executeUpdate(sql);
if(conseguiu_excluir == 1){
JOptionPane.showMessageDialog(null, "Exclusão realizada com sucesso!");
conecta_cidade.executeSQL("Select * from cidade");
atualiza_cb_pesq();
conecta_cidade.resultset.first();
mostra_dados();
}
}
else{
return;
}
}
catch(SQLException erro){
JOptionPane.showMessageDialog(null,"Erro ao tentar excluir o registro !");
}
}
private void buttun_alterarActionPerformed(java.awt.event.Actio nEvent evt) {
try{
String sql = "UPDATE cidade SET nome ='"+textField_nome.getText()+"',"+
"uf = '"+comboBox_uf.getSelectedItem()+"' where codigo = "+textField_cod.getText();
conecta_cidade.statement.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "Alteração realizada com sucesso!");
conecta_cidade.executeSQL("Select * from cidade");
atualiza_cb_pesq();
conecta_cidade.resultset.next();
mostra_dados();
}
catch( SQLException erro){
JOptionPane.showMessageDialog(null, "Erro ao tentar Alterar o registro!");
}
}
private void buttun_pesquisarActionPerformed(java.awt.event.Act ionEvent evt) {
try{
conecta_cidade.resultset.first();
String igual = "n";
int tamanho_pesquisa = textField_pesq.getText().length();
while(igual == "n"){
String pesquisando = conecta_cidade.resultset.getString("nome").substri ng(0,(tamanho_pesquisa));
if(pesquisando.equals(textField_pesq.getText())){
igual = "s";
}
else{
conecta_cidade.resultset.next();
}
}
textField_cod.setText(conecta_cidade.resultset.get String("codigo"));
mostra_dados();
}
catch(Exception erro){
JOptionPane.showMessageDialog(null, "Nenhum registro Encontrado ");
}
}
private void comboBox_pesqActionPerformed(java.awt.event.Action Event evt) {
if( inicia_combo == 1){
try{
conecta_cidade.resultset.first();
String igual = "n";
while("n".equals(igual) ){
if( conecta_cidade.resultset.getString("nome").equals( comboBox_pesq.getSelectedItem())){
igual = "s";
}
else{
conecta_cidade.resultset.next();
}
}
textField_cod.setText(conecta_cidade.resultset.get String("codigo"));
mostra_dados();
}
// este é o erro em que aparece logo apos em que eu pucho o metodo atualiza dados
catch(Exception erro){
JOptionPane.showMessageDialog(null, "Não conseguiu localizar via digitação ");
}
}
inicia_combo = 1;
}
private void textField_pesqActionPerformed(java.awt.event.Actio nEvent evt) {
try{
conecta_cidade.resultset.first();
String igual = "n";
int tamanho_pesquisa = textField_pesq.getText().length();
while("n".equals(igual)){
String pesquisando = conecta_cidade.resultset.getString("nome").substri ng(0,(tamanho_pesquisa));
if(pesquisando.equals(textField_pesq.getText())){
igual = "s";
}
else{
conecta_cidade.resultset.next();
}
}
textField_cod.setText(conecta_cidade.resultset.get String("codigo"));
mostra_dados();
comboBox_pesq.setSelectedItem((textField_nome.getT ext()));
}
catch(Exception erro){
JOptionPane.showMessageDialog(null, "Nenhum registro Encontrado ");
}
}
/**
* @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 Apenas usuários registrados e ativados podem ver os links., Clique aqui para se cadastrar...
*/
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(cad_cidade.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(cad_cidade.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(cad_cidade.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(cad_cidade.clas s.getName()).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 cad_cidade().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton buttao_anterior;
private javax.swing.JButton buttao_primeiro;
private javax.swing.JButton buttao_proximo;
private javax.swing.JButton buttao_ultimo;
private javax.swing.JButton buttun_alterar;
private javax.swing.JButton buttun_excluir;
private javax.swing.JButton buttun_gravar;
private javax.swing.JButton buttun_inserir;
private javax.swing.JButton buttun_pesquisar;
private javax.swing.JComboBox comboBox_pesq;
private javax.swing.JComboBox comboBox_uf;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JTable jTable;
private javax.swing.JLabel label_codigo;
private javax.swing.JLabel label_nome;
private javax.swing.JLabel label_pesquisar;
private javax.swing.JLabel label_titulo;
private javax.swing.JLabel label_uf;
private javax.swing.JTextField textField_cod;
private javax.swing.JTextField textField_nome;
private javax.swing.JTextField textField_pesq;
// End of variables declaration
public void mostra_dados () throws SQLException{
try {
textField_cod.setText(conecta_cidade.resultset.get String("codigo"));
textField_nome.setText(conecta_cidade.resultset.ge tString("nome"));
comboBox_uf.setSelectedItem(conecta_cidade.results et.getString("uf"));
}
catch (SQLException ex) {
if( navega == 1){
JOptionPane.showMessageDialog(null, "Você ja esta no primeiro registro!");
conecta_cidade.resultset.first();
}else{
if(navega == 2){
JOptionPane.showMessageDialog(null, "Você ja esta no ultimo registro!");
conecta_cidade.resultset.previous();
}
else{
JOptionPane.showMessageDialog(null, "Não localizou dados "+ex);
}
}
}
}
public void preencher_table(){
//especificar o tamanho cokuna
jTable.getColumnModel().getColumn(0).setPreferredW idth(20);
jTable.getColumnModel().getColumn(1).setPreferredW idth(150);
jTable.getColumnModel().getColumn(2).setPreferredW idth(20);
DefaultTableModel modelo = (DefaultTableModel) jTable.getModel();
modelo.setNumRows(0);
try{
while(conecta_cidade.resultset.next()){
modelo.addRow(new Object[]{conecta_cidade.resultset.getString("codigo"),cone cta_cidade.resultset.getString("nome"),conecta_cid ade.resultset.getString("uf")});
}
conecta_cidade.resultset.first();
}
catch(SQLException erro){
JOptionPane.showMessageDialog(null, "Erro ao listar no JTable! \n Erro :"+erro);
}
}
public void atualiza_cb_pesq(){
try {
// estou com duvida neste removeAllItems();
comboBox_pesq.removeAllItems();
conecta_cidade.resultset.first();
conecta_cidade.executeSQL("select * from cidade");
while( conecta_cidade.resultset.next()){
comboBox_pesq.addItem(conecta_cidade.resultset.get String("nome"));
}
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Não localizou dados "+ex);
}
}
}