Inserir no banco de dados MySql

Olá pessoal estou tentando inserir dados no banco de dados mas não estou conseguindo.

Classe: Cadastro.java


public class Cadastro {

    public String Nome;

    public void setNome(String Nome){
        this.Nome = Nome;
    }

    public String getNome(){
        return Nome;
    }

}

Classe:Incluir.java

[code]import java.sql.*;
import javax.swing.JOptionPane;

public class Incluir {
public void incluir(Cadastro cadastro){

    try{
        String driver  = "org.gjt.mm.mysql.Driver";
        String url =  "jdbc:mysql://localhost/java";
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url,"root","key1451");

        String sql = "INSERT INTO cadastro (nome)" + "VALUES (?)";

        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,cadastro.getNome());
        ps.execute();
        conn.close();
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, "Não foi inserido");
    }

}

}
[/code]

Classe: Formulario

[code]import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */

/*

  • Formulario.java
  • Created on 04/10/2010, 09:46:34
    */

/**
*

  • @author Mauro
    */
    public class Formulario extends javax.swing.JFrame {

    /** Creates new form Formulario */
    public Formulario() {
    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.
      */
      @SuppressWarnings(“unchecked”)
      //
      private void initComponents() {

      lbl_nome = new javax.swing.JLabel();
      tf_nome = new javax.swing.JTextField();
      bt_enviar = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      lbl_nome.setText(“Nome:”);

      tf_nome.setText(“jTextField1”);

      bt_enviar.setText(“Enviar”);
      bt_enviar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      bt_enviarActionPerformed(evt);
      }
      });

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      .addComponent(lbl_nome)
      .addGap(18, 18, 18)
      .addComponent(tf_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
      .addComponent(bt_enviar)
      .addContainerGap(207, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
      .addComponent(lbl_nome)
      .addComponent(tf_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addComponent(bt_enviar))
      .addContainerGap(266, Short.MAX_VALUE))
      );

      pack();
      }//

    //Aqui seria a ação do botão mas não sei o que colocar
    private void bt_enviarActionPerformed(java.awt.event.ActionEvent evt) {

    }

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Formulario().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton bt_enviar;
    private javax.swing.JLabel lbl_nome;
    private javax.swing.JTextField tf_nome;
    // End of variables declaration

}
[/code]

Qual a mens. de erro?

Dica: troque isto:

         catch(Exception e){  
             JOptionPane.showMessageDialog(null, "Não foi inserido");  
         }  
   

por isto:

       catch (Throwable ex) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            thr.printStackTrace(pw);
            JOptionPane.showMessageDialog(null, pw.toString());  
       }

Simplesmente não insere no banco.

No action do botao enviar, coloquei o seguinte.

private void bt_enviarActionPerformed(java.awt.event.ActionEvent evt) { Incluir obj = new Incluir(); obj.incluir(null); }

Mas ele exibe não inserido

Faça o que o entanglement disse para você conseguir capturar a exception.

Outro detalhe: Faz tempo que não mexo com MySQL, mas a classe (Class.forName) para carregar não é outra? com.mysql.jdbc.Driver, ou já existe um driver JDBC melhor?

[quote=mauraoemau]Simplesmente não insere no banco.

No action do botao enviar, coloquei o seguinte.

private void bt_enviarActionPerformed(java.awt.event.ActionEvent evt) { Incluir obj = new Incluir(); obj.incluir(null); }

Mas ele exibe não inserido[/quote]

vc está passando null como parametro, concerteza está dando um nullpointerexception vc deve passar um objeto cadastro como parametro