Bom dia a todos minha duvbida ocorre entre a linha 177 e 185, já consegui acimilar DAO e PreparedStatement, porém, acho que o grande problema é a pratica, se alguem poder me dá um dica se estou implementando o botão gravar corretamente ficarei muito grato. feliz 2009 a todos
classe conexao
[code]
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.print.attribute.standard.JobMessageFromOperator;
import javax.swing.JOptionPane;
public class ConexaoMySql {
public static Connection conexao;
public static Statement stm;
public void conectar()
{
try {
if (conexao == null) {
Class.forName(“com.mysql.jdbc.Driver”); //carrega os drivers do mysql na memoria
conexao = DriverManager.getConnection(“jdbc:mysql://localhost:3306/cliente?user=root&password=root”); //estabelece a conexao
JOptionPane.showMessageDialog(null,“Conectado!!”);
}
} catch (Exception e) {//Em caso de erro ele entra nessa parte!!!
e.printStackTrace();
}
}
public void executarSql(String nomeTF, String nickTF, String endTF, String numTF, String telTF,String celTF)
{
try {
conectar();
PreparedStatement pstmt = conexao.prepareStatement(
“insert into cliente values(?, ?, ?,?,?)”);
pstmt.setString(1,nomeTF);
pstmt.setString(2,nickTF);
pstmt.setString(3,endTF);
pstmt.setInt(4, Integer.parseInt(numTF) );
pstmt.setInt(5, Integer.parseInt(telTF) );
pstmt.setInt(6, Integer.parseInt(celTF) );
pstmt.executeUpdate();
System.out.println(pstmt);
pstmt.close();
conexao.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}[/code]
classe clientes
import com.mysql.jdbc.PreparedStatement;
import javax.sound.midi.ShortMessage;
import javax.swing.JOptionPane;
/*
* Cadastro.java
*
* Created on 23 de Dezembro de 2008, 23:18
*/
/**
*
* @author Administrador
*/
public class Cadastro extends javax.swing.JFrame {
ConexaoMySql conecta; //criando um objeto da classe ConexaoMySql
/** Creates new form Cadastro */
public Cadastro() {
initComponents();
conecta = new ConexaoMySql(); //inicializando o objeto
conecta.conectar();
}
/** 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();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
nomeTF = new javax.swing.JTextField();
nickTF = new javax.swing.JTextField();
endTF = new javax.swing.JTextField();
numlTF = new javax.swing.JTextField();
telTF = new javax.swing.JTextField();
celTF = new javax.swing.JTextField();
insert = new javax.swing.JButton();
jLabel7 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(0, 255, 255));
jLabel1.setFont(new java.awt.Font("SansSerif", 1, 12));
jLabel1.setText("Nome");
jLabel2.setFont(new java.awt.Font("SansSerif", 1, 12));
jLabel2.setText("Nick");
jLabel3.setFont(new java.awt.Font("SansSerif", 1, 12));
jLabel3.setText("End");
jLabel4.setFont(new java.awt.Font("SansSerif", 1, 12));
jLabel4.setText("Numero");
jLabel5.setFont(new java.awt.Font("SansSerif", 1, 12));
jLabel5.setText("Telefone");
jLabel6.setFont(new java.awt.Font("SansSerif", 1, 12));
jLabel6.setText("Celular");
nomeTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nomeTFActionPerformed(evt);
}
});
nickTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nickTFActionPerformed(evt);
}
});
insert.setText("GRAVAR");
insert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
insertActionPerformed(evt);
}
});
jLabel7.setFont(new java.awt.Font("Tahoma", 1, 36));
jLabel7.setText("CADASTRO DE CLIENTES");
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.TRAILING, layout.createSequentialGroup()
.addContainerGap(72, Short.MAX_VALUE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(46, 46, 46)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(insert)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nomeTF, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
.addComponent(nickTF, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(telTF, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel6))
.addComponent(endTF, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(celTF, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(numlTF, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGap(72, 72, 72))))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {celTF, telTF});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel7)
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(nomeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(8, 8, 8)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(nickTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(numlTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(endTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(celTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(telTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addGap(28, 28, 28)
.addComponent(insert)
.addContainerGap(39, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void nickTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO adicione seu código de manipulação aqui:
}
private void nomeTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO adicione seu código de manipulação aqui:
}
private void insertActionPerformed(java.awt.event.ActionEvent evt, boolean executarSQL) {
conectar();
executarSQL(nomeTF.getText(), nickTF.getText(),endTF.getText(),numTF.getText(), telTF.getText, celTF.getText()); //duvida
if (executarSQL == true){
JOptionPane.showMessageDialog(null, "gravada");
}else
JOptionPane.showMessageDialog(null, "Não foi possível efetuar a gravação");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Cadastro().setVisible(true);
}
});
}
private void conectar() {
throw new UnsupportedOperationException("Not yet implemented");
}
// Declaração de variáveis - não modifique
private javax.swing.JTextField celTF;
private javax.swing.JTextField endTF;
private javax.swing.JButton insert;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JTextField nickTF;
private javax.swing.JTextField nomeTF;
private javax.swing.JTextField numlTF;
private javax.swing.JTextField telTF;
// Fim da declaração de variáveis
}