Duvida simples

4 respostas
N

Esse é o codigo o erro esta no final do codigo em vermelho…

import java.sql.SQLException;

import javax.swing.JOptionPane;

import utilitarios.conexao;

public class cad_cidade extends javax.swing.JFrame {

conexao con_cidade; //criei uma variavel do tipo conexão, para efetuar a conexao com o banco.

/** Creates new form cad_cidade */

public cad_cidade() throws SQLException {

initComponents();

con_cidade=new conexao(); //inicializou a variavel.

con_cidade.conecta(); //conecta ao BD, utilizando  o con_conecta q é uma variaveldo tipo conexao, que é a classe q eu tenho.

//con_cidade.executeSQL(“select * from cidade”); //esse método ira executar a SQL de acordo com a classe criada na conexao;
try
    {
        //while(con_cidade.resultset.next()) //enquanto contiver cidades ele vai avancando.
        
            con_cidade.resultset.next();
         
            tf_codigo.setText(con_cidade.resultset.getString("codigo")); //essa linha pega os valores adicionados e especifica o campo q sera selecionado
            tf_nome.setText(con_cidade.resultset.getString("nome"));
            System.out.println(con_cidade.resultset.getString("UF"));

           
    }
    catch (SQLException erro)
    {
        JOptionPane.showMessageDialog(null, "Não localizou Dados " +erro);
    }
}

/** 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.
 */
// <editor-fold defaultstate="collapsed" desc=" Código Gerado ">
private void initComponents() {
    jLabel1 = new javax.swing.JLabel();
    tf_pesquisar = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    botao_gravar = new javax.swing.JButton();
    botao_inserir = new javax.swing.JButton();
    botao_alterar = new javax.swing.JButton();
    botao_excluir = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();
    jButton6 = new javax.swing.JButton();
    jButton7 = new javax.swing.JButton();
    jButton8 = new javax.swing.JButton();
    tf_codigo = new javax.swing.JTextField();
    tf_nome = new javax.swing.JTextField();
    cb_pesquisa = new javax.swing.JComboBox();
    cb_uf = new javax.swing.JComboBox();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Manutenc\\ao do Cadastro de Cidades");
    jLabel1.setText("Cadastro Cidades");

    jLabel2.setText("Pesquisar");

    jLabel3.setText("Codigo");

    jLabel4.setText("Nome");

    jLabel5.setText("UF:");

    botao_gravar.setText("Gravar");

    botao_inserir.setText("Inserir");

    botao_alterar.setText("Alterar");

    botao_excluir.setText("Excluir");

    jButton5.setText("<<");

    jButton6.setText("<");

    jButton7.setText(">");

    jButton8.setText(">>");

    tf_codigo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            tf_codigoActionPerformed(evt);
        }
    });

    cb_pesquisa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

    cb_uf.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SC", "RS", "SP", "RJ", "PR", "MG", "GO" }));

    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()
            .add(140, 140, 140)
            .add(jLabel1)
            .addContainerGap(183, Short.MAX_VALUE))
        .add(layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(31, 31, 31)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(botao_gravar)
                        .add(jButton5))
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(layout.createSequentialGroup()
                            .add(botao_inserir)
                            .add(18, 18, 18)
                            .add(botao_alterar))
                        .add(layout.createSequentialGroup()
                            .add(jButton6)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jButton7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 44, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(21, 21, 21))
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel2)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel3)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel4)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel5))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 23, Short.MAX_VALUE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, tf_nome)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, tf_pesquisar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))
                        .add(tf_codigo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 78, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(cb_uf, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(54, 54, 54)))
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(botao_excluir)
                    .add(jButton8))
                .add(layout.createSequentialGroup()
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(cb_pesquisa, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 65, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(56, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(33, 33, 33)
            .add(jLabel1)
            .add(42, 42, 42)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jLabel2)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(tf_pesquisar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(cb_pesquisa, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
            .add(14, 14, 14)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(jLabel3)
                .add(tf_codigo, 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(jLabel4)
                .add(tf_nome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(19, 19, 19)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(jLabel5)
                .add(cb_uf, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(25, 25, 25)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(jButton6)
                .add(jButton7)
                .add(jButton8)
                .add(jButton5))
            .add(21, 21, 21)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(botao_gravar)
                .add(botao_inserir)
                .add(botao_excluir)
                .add(botao_alterar))
            .addContainerGap(19, Short.MAX_VALUE))
    );
    pack();
}// </editor-fold>

private void tf_codigoActionPerformed(java.awt.event.ActionEvent evt) {

// TODO adicione seu código de manipulação aqui:
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new cad_cidade().setVisible(true); [color=red]****Nessa linha tahh dando o erro:  "E:\Proj JAVA\Estoque\src\cadastros\cad_cidade.java:205: unreported exception java.sql.SQLException; must be caught or declared to be thrown"[/color]

        }
    });
}

4 Respostas

Andre_Brito

Cara, pelamordedeus usa as tags code.

Ninguém vai ler isso se não tiver bem arrumado.

http://www.guj.com.br/posts/list/50115.java

ateubh

seu método lança uma exceção, mas quando você o chama, vc não está tratando ela…

tente

...
try {
 new cad_cidade().setVisible(true);
} catch(SQLException e) {
}
...
N

ateubh:
seu método lança uma exceção, mas quando você o chama, vc não está tratando ela…

tente

... try { new cad_cidade().setVisible(true); } catch(SQLException e) { } ...

Blz funciono…muito obrigado…

peczenyj

Dê uma olhada nesses posts do Luca Bastos


Agora, preste atenção quando um metodo lançar uma exception (isto é, tem a declaração throws …) e veja aonde é o melhor lugar pra trata-la. As vezes vc tem que lançar a exception pra outro lugar, as vezes tem que re-empacota-la e ainda tem a clausula finally que existe para facilitar a nossa vida.

Criado 7 de janeiro de 2008
Ultima resposta 8 de jan. de 2008
Respostas 4
Participantes 4