** 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…
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Conexao.inserir(Conexao.java:56)
at Clientes.jButton2ActionPerformed(Clientes.java:115)
at Clientes.access$200(Clientes.java:11)
at Clientes$3.actionPerformed(Clientes.java:83)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
valeu rapaziada…
[size=“11”][color=“red”]* Editado: Lembre-se de utilizar BBCode em seus códigos - Reifel[/color][/size] :joia: