Ola pesoal tudo bem!!
so novo no java e esto fazendo curso ainda!!!
mas esto com um problema que nao consigo achar .
eu monto a tela normal para casastra um cliente ele cadatra tudo normal nao aprensa erro no try catch mas nao salva no banco de dados ele nao aprensenta erro nenhum ao se conectar com o banco de dados !!
ai quando eu vo la no mysql nao mostra nada alguem poderia me dizer oq esta acontecendo eu acho qeu e erro no banco de dados vo posta aqui em baixo os codigos das class
obs:esto usando o netbeans e agradeço des de ja!
1 class de conexao
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Conexao {
public Connection criaConexao(){
Connection con = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/”,“root”,“senha”);
} catch (SQLException ex) {
Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
}
2 class clienteDB
package db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ClienteDB {
public void inserir(String nome,String endereco,int numeroend,String complemento){
Conexao c = new Conexao();
Connection con = c.criaConexao();
PreparedStatement ps;
try {
ps = con.prepareStatement("insert into cliente(nome,endereco,numeroend,complemento)values(?,?,?,?)");
ps.setString(1,nome);
ps.setString(2,endereco);
ps.setInt(3,numeroend);
ps.setString(4,complemento);
ps.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(Clientedb.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
de vez da tela vo colcoar uma clas de test
package Test;
import db.ClienteDB;
public class TesteBanco {
public static void main(String[] args) {
ClienteDB cl = new ClienteDB();
cl.inserir(“Felipe”,“rua nova pero”,20,“cassa 1”);
}
}
mas o codigo da tela esta aqui em baixo tambem
3 class a tela
package tela;
import db.ClienteDB;
import javax.swing.JOptionPane;
public class Cadastro extends javax.swing.JFrame {
/** Creates new form Cadastro */
public Cadastro() {
initComponents();
}
@SuppressWarnings("unchecked")
// <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();
nome = new javax.swing.JTextField();
end = new javax.swing.JTextField();
n = new javax.swing.JTextField();
com = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("CADASTRO");
jLabel1.setText("NOME");
jLabel2.setText("ENDEREÇO");
jLabel3.setText("N°");
jLabel4.setText("COMPLEMENTO");
n.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nActionPerformed(evt);
}
});
jButton1.setText("CADASTRAR");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(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()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(jLabel4)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(13, 13, 13)))
.addGap(4, 4, 4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nome, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(end, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(com, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
.addComponent(n, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(22, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(70, 70, 70)
.addComponent(jButton1)
.addContainerGap(237, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(62, 62, 62)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(end, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(n, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(com, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
.addComponent(jButton1)
.addContainerGap())
);
pack();
}// </editor-fold>
private void nActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ClienteDB cl = new ClienteDB();
String name = nome.getText();
String endereco =end.getText();
int numero =Integer.parseInt(n.getText());
String comple = com.getText();
cl.inserir(name, endereco, numero, comple);
JOptionPane.showMessageDialog(null,“Inserido Corretamenta”,“Inserçao dados”,JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Cadastro().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField com;
private javax.swing.JTextField end;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField n;
private javax.swing.JTextField nome;
// End of variables declaration
}