Erro na inserção ao banco usando swing

5 respostas
mococa

** saudações **

to fazendo um programinha swing usando netbeans…tenho um form principal onde faço minha conexao ao banco e mais um onde vou inserir os dados, e fiz uma classe chamada Conexao onde criei os metodos…conexao(), inserir():

*** form principal ***

/*
* SistLocacao.java
*
* Created on 30 de Abril de 2006, 02:14
*/

/**
*
* @author Ricardo
*/
public class SistLocacao extends javax.swing.JFrame {

/** Creates new form SistLocacao */
public SistLocacao() {
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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem10 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem5 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem7 = new javax.swing.JMenuItem();
jMenuItem8 = new javax.swing.JMenuItem();
jMenuItem9 = new javax.swing.JMenuItem();
jMenu4 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
jMenu5 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem11 = new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("SkyAir - Ag\u00eancia de Viagens");
jMenu1.setText("Cadastrar");
jMenu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu1ActionPerformed(evt);
}
});

jMenuItem10.setText("V\u00f4o");
jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem10ActionPerformed(evt);
}
});

jMenu1.add(jMenuItem10);

jMenuBar1.add(jMenu1);

jMenu2.setText("Consultar");
jMenuItem3.setText("Por n\u00famero de v\u00f4o");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});

jMenu2.add(jMenuItem3);

jMenuItem4.setText("Por origem");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});

jMenu2.add(jMenuItem4);

jMenuItem5.setText("Por destino");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem5ActionPerformed(evt);
}
});

jMenu2.add(jMenuItem5);

jMenuBar1.add(jMenu2);

jMenu3.setText("Reservar");
jMenuItem7.setText("Confirmada");
jMenu3.add(jMenuItem7);

jMenuItem8.setText("V\u00f4o Lotado");
jMenu3.add(jMenuItem8);

jMenuItem9.setText("V\u00f4o Inexistente");
jMenu3.add(jMenuItem9);

jMenuBar1.add(jMenu3);

jMenu4.setText("Conectar");
jMenuItem1.setText("MySQL");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});

jMenu4.add(jMenuItem1);

jMenuItem6.setText("HSQLDB");
jMenu4.add(jMenuItem6);

jMenuBar1.add(jMenu4);

jMenu5.setText("Sair");
jMenuItem2.setText("Desconectar Banco de Dados");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});

jMenu5.add(jMenuItem2);

jMenuItem11.setText("Sair do Programa");
jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem11ActionPerformed(evt);
}
});

jMenu5.add(jMenuItem11);

jMenuBar1.add(jMenu5);

setJMenuBar(jMenuBar1);

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 351, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>

private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);

}

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:


}

private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Clientes r = new Clientes();
r.setVisible(true);

}

private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:


}

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Conexao conexao = new Conexao();
conexao.conectar();

}

private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Consultar r = new Consultar();
r.setVisible(true);

}

private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

Consultar r = new Consultar();
r.setVisible(true);

}

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

Consultar r = new Consultar();
r.setVisible(true);


}



/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SistLocacao().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu5;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem10;
private javax.swing.JMenuItem jMenuItem11;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuItem jMenuItem8;
private javax.swing.JMenuItem jMenuItem9;
// End of variables declaration

}

** form pra inserção dos dados **

/*
* Clientes.java
*
* Created on 30 de Abril de 2006, 02:15
*/

/**
*
* @author Ricardo
*/
public class Clientes extends javax.swing.JFrame {

Conexao con;

/** Creates new form Clientes */
public Clientes() {
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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
numeroVoo = new javax.swing.JTextField();
origem = new javax.swing.JTextField();
destino = new javax.swing.JTextField();
lugares = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();

getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Cadastro de Passageiros");
jLabel1.setText("Digite o n\u00famero do v\u00f4o");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, -1, -1));

jLabel2.setText("Digite a origem do v\u00f4o");
getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, -1, -1));

jLabel3.setText("Digite o destino do v\u00f4o");
getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

jLabel4.setText("Digite o n\u00famero de lugares dispon\u00edveis");
getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, -1, -1));

numeroVoo.setText("jTextField1");
numeroVoo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
numeroVooActionPerformed(evt);
}
});

getContentPane().add(numeroVoo, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 30, 120, -1));

origem.setText("jTextField2");
getContentPane().add(origem, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 70, 120, -1));

destino.setText("jTextField3");
getContentPane().add(destino, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 120, 120, -1));

lugares.setText("jTextField4");
getContentPane().add(lugares, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 160, 120, -1));

jButton1.setText("Retornar ao meu principal");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 190, 180, -1));

jButton2.setText("Salvar");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, 150, -1));

jButton3.setText("Limpar");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 190, -1, -1));

pack();
}// </editor-fold>

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

numeroVoo.setText("");
origem.setText("");
destino.setText("");
lugares.setText("");
numeroVoo.requestFocus();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

con = new Conexao();
con.inserir();

}

private void numeroVooActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

SistLocacao r = new SistLocacao();
r.setVisible(true);




}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Clientes().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JTextField destino;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField lugares;
private javax.swing.JTextField numeroVoo;
private javax.swing.JTextField origem;
// End of variables declaration

}

** classe conexao **

/*
* Conexao.java
*
* Created on 1 de Maio de 2006, 20:51
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author Ricardo
*/
import java.sql.*;
import javax.swing.*;

public class Conexao {


public String driver = "com.mysql.jdbc.Driver";
public String url = "jdbc:mysql://127.0.0.1:3306/aviao";
public String usuario = "root";
public String senha = "catia";
Connection con;
Statement stmt;
JTextField numeroVoo, destino, origem, lugares;

public void conectar(){
try{

Class.forName(driver);
con = DriverManager.getConnection(url, usuario, senha);
stmt = con.createStatement();
JOptionPane.showMessageDialog(null, "Conexão estabelecida","Informação",2);

}catch(SQLException e){ //trata os erros

JOptionPane.showMessageDialog(null, "Problemas na conexão com a fonte dos dados","Mensagem",1);
System.exit(0);


} catch(ClassNotFoundException e){

JOptionPane.showMessageDialog(null,"Driver não encontrado ","Mensagem",1);
System.exit(0);
}

}

public void inserir(){

try{



int id = Integer.parseInt(numeroVoo.getText());
String Orig = origem.getText();
String Dest = destino.getText();
int Lug = Integer.parseInt(lugares.getText());

String sql = "INSERT INTO companhiaarea (idVoo, Origem, Destino, Lugares) VALUES (" + id + ",'" + Orig + " , '" + Dest + ",'" + Lug + "')";

stmt.executeUpdate(sql);


JOptionPane.showMessageDialog( null, " Dados Salvos! ","Mensagem", 2);



} catch( SQLException e){ //trata os erros

JOptionPane.showMessageDialog(null, "Erro Cmdo SQL " + e.getMessage() );
System.exit(0);
}



}


}

** na conexao tudo ocorre normalmente, mas na inserção ta dando o seguinte erro: obs… adicinei no lib do projeto o jar do conector e no runtime (drive) o jar tbem do conector mysql…

valeu rapaziada…

5 Respostas

lucao

Cara, tenta fazer um PreparedStatement no método insere:

public void inserir(){

try{
   int id = Integer.parseInt(numeroVoo.getText());
   String Orig = origem.getText();
   String Dest = destino.getText();
   int Lug = Integer.parseInt(lugares.getText());

   PreparedStatement stmt = this.con.prepareStatement(
				"INSERT INTO companhiaarea (idVoo, Origem, Destino, Lugares) VALUES ( ?, ?, ?, ?);

        stmt.setString(1,id);		
	stmt.setString(2,orig);		
	stmt.setString(3,dest);		
	stmt.setString(4,lug);		
	stmt.execute();
	stmt.close();
     
        JOptionPane.showMessageDialog( null, " Dados Salvos! ","Mensagem", 2);
} 
catch( SQLException e){ //trata os erros

JOptionPane.showMessageDialog(null, "Erro Cmdo SQL " + e.getMessage() );
System.exit(0);
}
}

Ve se funciona, qualquer coisa da um toque.

mococa

** pra naum ficar muito intenso os codigos, deletei os outros forms e agora to com um form de cadastro e outra classe chamada conexao…

** Form Clientes **

/*

  • Clientes.java
  • Created on 30 de Abril de 2006, 02:15
    */

/**
*

  • @author Ricardo
    */
    public class Clientes extends javax.swing.JFrame {

Conexao conexao;

/** Creates new form Clientes */
public Clientes() {
    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.
 */
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
private void initComponents() {
    labelNumeroVoo = new javax.swing.JLabel();
    labelOrigem = new javax.swing.JLabel();
    labelDestino = new javax.swing.JLabel();
    LabelLugares = new javax.swing.JLabel();
    numeroVoo = new javax.swing.JTextField();
    origem = new javax.swing.JTextField();
    destino = new javax.swing.JTextField();
    lugares = new javax.swing.JTextField();
    btnSalvar = new javax.swing.JButton();
    btnLimpar = new javax.swing.JButton();
    btnFechar = new javax.swing.JButton();

    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Cadastro de Passageiros");
    labelNumeroVoo.setText("Digite o n\u00famero do v\u00f4o");
    getContentPane().add(labelNumeroVoo, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, -1, -1));

    labelOrigem.setText("Digite a origem do v\u00f4o");
    getContentPane().add(labelOrigem, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, -1, -1));

    labelDestino.setText("Digite o destino do v\u00f4o");
    getContentPane().add(labelDestino, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

    LabelLugares.setText("Digite o n\u00famero de lugares dispon\u00edveis");
    getContentPane().add(LabelLugares, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, -1, -1));

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

    getContentPane().add(numeroVoo, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 30, 120, -1));

    getContentPane().add(origem, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 70, 120, -1));

    getContentPane().add(destino, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 120, 120, -1));

    getContentPane().add(lugares, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 160, 120, -1));

    btnSalvar.setText("Salvar");
    btnSalvar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnSalvarActionPerformed(evt);
        }
    });

    getContentPane().add(btnSalvar, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, 150, -1));

    btnLimpar.setText("Limpar");
    btnLimpar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnLimparActionPerformed(evt);
        }
    });

    getContentPane().add(btnLimpar, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 190, 160, -1));

    btnFechar.setText("Sair");
    btnFechar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnFecharActionPerformed(evt);
        }
    });

    getContentPane().add(btnFechar, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 240, -1, -1));

    pack();
}// </editor-fold>                        

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

// TODO add your handling code here:

System.exit(0);
    
}                                         

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

// TODO add your handling code here:

numeroVoo.setText("");
    origem.setText("");
    destino.setText("");
    lugares.setText("");
    numeroVoo.requestFocus();
}                                         

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

// TODO add your handling code here:

conexao.inserir();
    
}                                         

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

// TODO add your handling code here:
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Clientes().setVisible(true);
        }
    });
    
     Conexao conexao = new Conexao(); 
}

// Variables declaration - do not modify                     
private javax.swing.JLabel LabelLugares;
private javax.swing.JButton btnFechar;
private javax.swing.JButton btnLimpar;
private javax.swing.JButton btnSalvar;
private javax.swing.JTextField destino;
private javax.swing.JLabel labelDestino;
private javax.swing.JLabel labelNumeroVoo;
private javax.swing.JLabel labelOrigem;
private javax.swing.JTextField lugares;
private javax.swing.JTextField numeroVoo;
private javax.swing.JTextField origem;
// End of variables declaration

}

** classe Conexao **
/*

  • Conexao.java
  • Created on 1 de Maio de 2006, 20:51
  • To change this template, choose Tools | Template Manager
  • and open the template in the editor.
    */

/**
*

  • @author Ricardo
    /
    import java.sql.
    ;
    import javax.swing.*;

public class Conexao {

Statement stmt;
Connection con;
JTextField numeroVoo, destino, origem, lugares;

public Conexao(){

String driver = com.mysql.jdbc.Driver;

String url = jdbc:mysql://127.0.0.1:3306/aviao;

String usuario = root;

String senha = catia;

try{

Class.forName(driver);
con = DriverManager.getConnection(url, usuario, senha);
stmt = con.createStatement();
JOptionPane.showMessageDialog(null, "Conexão estabelecida","Informação",2);
          
}catch(SQLException e){ //trata os erros
    
  JOptionPane.showMessageDialog(null, "Problemas na conexão com a fonte dos dados","Mensagem",1);
  System.exit(0);

  
} catch(ClassNotFoundException e){
    
  JOptionPane.showMessageDialog(null,"Driver não encontrado ","Mensagem",1);
  System.exit(0);
}

}

public void inserir(){

try{

            int id = Integer.parseInt(numeroVoo.getText());
            String Orig = origem.getText();
            String Dest = destino.getText();
            int Lug = Integer.parseInt(lugares.getText());

            String sql = "INSERT INTO companhiaarea (idVoo, Origem, Destino, Lugares) VALUES (" + id + ",'" + Orig + " , '" + Dest + ",'" + Lug + "')";

            stmt.executeUpdate(sql);


           JOptionPane.showMessageDialog( null, " Dados Salvos! ","Mensagem", 2);



      } catch( SQLException e){ //trata os erros

       JOptionPane.showMessageDialog(null, "Erro Cmdo SQL " + e.getMessage() );
       System.exit(0);
      }

}

}

** ja fiz todos os testes possiveis e ainda nao consigo inserir dados …
e os msm erros continuam…
valeu

mococa

bom erko…pelo q entendi refiz o q vc sugeriu…

*** classe conexao ***

/*

  • Conexao.java
  • Created on 1 de Maio de 2006, 20:51
  • To change this template, choose Tools | Template Manager
  • and open the template in the editor.
    */

/**
*

  • @author Ricardo
    /
    import java.sql.
    ;
    import javax.swing.*;

public class Conexao {

Statement stmt;
Connection con;
JTextField numeroVoo, destino, origem, lugares;

public void setNumeroVoo(JTextField n) {
    this.numeroVoo = n;
} 

public void setDestino(JTextField d){
    
    this.destino = d;
}
public void setOrigem(JTextField o){
    
    this.origem = o;
            
}
public void setLugares(JTextField l){
    
    this.lugares = l;
}

public Conexao(){

String driver = com.mysql.jdbc.Driver;

String url = jdbc:mysql://127.0.0.1:3306/aviao;

String usuario = root;

String senha = catia;

try{

Class.forName(driver);
con = DriverManager.getConnection(url, usuario, senha);
stmt = con.createStatement();
JOptionPane.showMessageDialog(null, "Conexão estabelecida","Informação",2);
          
}catch(SQLException e){ //trata os erros
    
  JOptionPane.showMessageDialog(null, "Problemas na conexão com a fonte dos dados","Mensagem",1);
  System.exit(0);

  
} catch(ClassNotFoundException e){
    
  JOptionPane.showMessageDialog(null,"Driver não encontrado ","Mensagem",1);
  System.exit(0);
}

}

public void inserir(){

try{

            int id = Integer.parseInt(numeroVoo.getText());                                                   
            String Orig = origem.getText();
            String Dest = destino.getText();
            int Lug = Integer.parseInt(lugares.getText());
            
            
            

            String sql = "INSERT INTO companhiaarea (idVoo, Origem, Destino, Lugares) VALUES (" + id + ",'" + Orig + " , '" + Dest + ",'" + Lug + "')";

            stmt.executeUpdate(sql);


           JOptionPane.showMessageDialog( null, " Dados Salvos! ","Mensagem", 2);



      } catch( SQLException e){ //trata os erros

       JOptionPane.showMessageDialog(null, "Erro Cmdo SQL " + e.getMessage() );
       System.exit(0);
      }

}

}

** classe clientes **

/*

  • Clientes.java
  • Created on 30 de Abril de 2006, 02:15
    */

/**
*

  • @author Ricardo
    */
    public class Clientes extends javax.swing.JFrame {

Conexao conexao;

/** Creates new form Clientes */
public Clientes() {
    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.
 */
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
private void initComponents() {
    labelNumeroVoo = new javax.swing.JLabel();
    labelOrigem = new javax.swing.JLabel();
    labelDestino = new javax.swing.JLabel();
    LabelLugares = new javax.swing.JLabel();
    numeroVoo = new javax.swing.JTextField();
    origem = new javax.swing.JTextField();
    destino = new javax.swing.JTextField();
    lugares = new javax.swing.JTextField();
    btnSalvar = new javax.swing.JButton();
    btnLimpar = new javax.swing.JButton();
    btnFechar = new javax.swing.JButton();

    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Cadastro de Passageiros");
    labelNumeroVoo.setText("Digite o n\u00famero do v\u00f4o");
    getContentPane().add(labelNumeroVoo, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, -1, -1));

    labelOrigem.setText("Digite a origem do v\u00f4o");
    getContentPane().add(labelOrigem, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, -1, -1));

    labelDestino.setText("Digite o destino do v\u00f4o");
    getContentPane().add(labelDestino, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

    LabelLugares.setText("Digite o n\u00famero de lugares dispon\u00edveis");
    getContentPane().add(LabelLugares, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, -1, -1));

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

    getContentPane().add(numeroVoo, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 30, 120, -1));

    getContentPane().add(origem, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 70, 120, -1));

    getContentPane().add(destino, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 120, 120, -1));

    getContentPane().add(lugares, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 160, 120, -1));

    btnSalvar.setText("Salvar");
    btnSalvar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnSalvarActionPerformed(evt);
        }
    });

    getContentPane().add(btnSalvar, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, 150, -1));

    btnLimpar.setText("Limpar");
    btnLimpar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnLimparActionPerformed(evt);
        }
    });

    getContentPane().add(btnLimpar, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 190, 160, -1));

    btnFechar.setText("Sair");
    btnFechar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnFecharActionPerformed(evt);
        }
    });

    getContentPane().add(btnFechar, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 240, -1, -1));

    pack();
}// </editor-fold>                        

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

// TODO add your handling code here:

System.exit(0);
    
}                                         

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

// TODO add your handling code here:

numeroVoo.setText("");
    origem.setText("");
    destino.setText("");
    lugares.setText("");
    numeroVoo.requestFocus();
}                                         

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

// TODO add your handling code here:

conexao.setNumeroVoo(numeroVoo);
  conexao.setOrigem(origem);
  conexao.setDestino(destino);
  conexao.setLugares(lugares);
  conexao.inserir();
    
}                                         

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

// TODO add your handling code here:
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Clientes().setVisible(true);
        }
    });
    
     Conexao conexao = new Conexao(); 
}

// Variables declaration - do not modify                     
private javax.swing.JLabel LabelLugares;
private javax.swing.JButton btnFechar;
private javax.swing.JButton btnLimpar;
private javax.swing.JButton btnSalvar;
private javax.swing.JTextField destino;
private javax.swing.JLabel labelDestino;
private javax.swing.JLabel labelNumeroVoo;
private javax.swing.JLabel labelOrigem;
private javax.swing.JTextField lugares;
private javax.swing.JTextField numeroVoo;
private javax.swing.JTextField origem;
// End of variables declaration

}

** entaum os metodos set q vc sugeriu eu coloquei dessa maneira, naum rodou e por duvida fiz metodos get tbem e msm assim naum rodou tbem…
naum sei se é assim q tu sugeriu…tenho duvidas ainda…
** os msm erros permanecem…

valeu

mococa

** continuo sem resolver o problema ***

os erros continuam

hehe

mococa

** o problema continua**** alguem pode me solucionar o problema***
valeu

Criado 4 de maio de 2006
Ultima resposta 6 de mai. de 2006
Respostas 5
Participantes 2