//estou com problema ao usar as opções GUARDAR no banco
package CadastroAluno;
import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* @author joniel
*/
public class CadastroJFrame extends javax.swing.JFrame {
public Connection Connection;
public Connection connetion;
public CadastroJFrame() {
initComponents();
this.TxtId.setVisible(false);//id o do campo chave v ser ocult
}
//CAMPO DE VARIAVEIS GLOBARIS
conection conection;//para conecao
PreparedStatement preparedStatement;//prepara Statement
ResultSet resultSet;//preparar restualtado da consulta
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollBar1 = new javax.swing.JScrollBar();
jLabel1 = new javax.swing.JLabel();
TxrChave = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
TxtTelefonee = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
TxtChave = new javax.swing.JTextField();
TxtNome = new javax.swing.JTextField();
TxtTelefone = new javax.swing.JTextField();
TxtNascimento = new javax.swing.JTextField();
TxtId = new javax.swing.JTextField();
btnBuscar = new javax.swing.JButton();
btnLimpar = new javax.swing.JButton();
btnGuardar = new javax.swing.JButton();
btnModificar = new javax.swing.JButton();
btnEliminar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Cadastro Aluno");
TxrChave.setText("Chave: ");
jLabel3.setText("Nome: ");
TxtTelefonee.setText("Telefone: ");
jLabel5.setText("Nascimento: ");
TxtChave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TxtChaveActionPerformed(evt);
}
});
TxtId.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TxtIdActionPerformed(evt);
}
});
btnBuscar.setText("Buscar");
btnBuscar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBuscarActionPerformed(evt);
}
});
btnLimpar.setText("Limpar");
btnLimpar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLimparActionPerformed(evt);
}
});
btnGuardar.setText("Guardar");
btnGuardar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGuardarActionPerformed(evt);
}
});
btnModificar.setText("Modificar");
btnModificar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnModificarActionPerformed(evt);
}
});
btnEliminar.setText("Eliminar");
btnEliminar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEliminarActionPerformed(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(53, 53, 53)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(btnGuardar))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(btnModificar)
.addGap(44, 44, 44)
.addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(TxtNascimento)
.addGap(110, 110, 110))))
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TxtTelefonee)
.addComponent(jLabel3)
.addComponent(TxrChave))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TxtChave, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(TxtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
.addComponent(TxtTelefone)))
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(TxtId, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(42, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TxrChave)
.addComponent(TxtChave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnBuscar)
.addComponent(TxtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(TxtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnLimpar))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TxtTelefonee)
.addComponent(TxtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(15, 15, 15)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(TxtNascimento, 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(btnGuardar)
.addComponent(btnModificar)
.addComponent(btnEliminar))
.addContainerGap(93, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void TxtIdActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {
try {
connetion = conection.getConnection();
preparedStatement = connetion.prepareStatement("INSERT INTO pessoa (chave, nome, telefone, nascimento) VALORES (?,?,?,?)");
preparedStatement.setString(1, TxtChave.getText());
preparedStatement.setString(2, TxtNome.getText());
preparedStatement.setString(3, TxtTelefone.getText());
preparedStatement.setDate(4, Date.valueOf(TxtNascimento.getText()));
int res = preparedStatement.executeUpdate();
if (res > 0 ){
JOptionPane.showMessageDialog(null,"Aluno Cadastrado(a)");
LimparCaixa();
}else{
JOptionPane.showMessageDialog(null, "Erro ao Guardar o Alunos");
LimparCaixa();
connection.close();
}
} catch (SQLException | HeadlessException ex) {
System.out.println(ex);
}
}
@SuppressWarnings("UseSpecificCatch")
private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {
try {
Connection = conection.getConnection();
preparedStatement = connetion.prepareStatement("SELECT id nome, telefone, nascimento FROM pessoa WHERE chave");
preparedStatement.setString(1, TxtChave.getText());
resultSet = preparedStatement.executeQuery();
if (resultSet.next() ){
TxtId.setText(resultSet.getString("id"));
TxtNome.setText(resultSet.getString("Nome"));
TxtTelefone.setText(resultSet.getString("Telefone"));
TxtNascimento.setText(resultSet.getString("Nascimento"));
TxtChave.setEditable(false);
}else{
JOptionPane.showMessageDialog(null, "Não tem aluno com essa chave");
TxtChave.setEditable(true);
}
conection.close();//fim da conexao
} catch (Exception ex) {
System.out.println(ex);
}
}
private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {
try {
connetion = conection.getConnection();
preparedStatement = connetion.prepareStatement("UPDATE pessoa SET chave=?, nome=?, telefone=?, nascimento=? WHERE id=?");
preparedStatement.setString(1, TxtChave.getText());
preparedStatement.setString(2, TxtNome.getText());
preparedStatement.setString(3, TxtTelefone.getText());
preparedStatement.setDate(4, Date.valueOf(TxtNascimento.getText()));
preparedStatement.setInt(5,Integer.parseInt(TxtId.getText()));
int res = preparedStatement.executeUpdate();
if (res > 0 ){
JOptionPane.showMessageDialog(null,"Aluno Modificado(a)");
LimparCaixa();
}else{
JOptionPane.showMessageDialog(null, "Erro ao Modificar Alunos");
LimparCaixa();
}
conection.close();//fim da conexao
} catch (Exception ex) {
System.out.println(ex);
}
}
private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {
try {
connetion = conection.getConnection();
preparedStatement = connetion.prepareStatement("DELET FRON pessoa WHERE id=?");
preparedStatement.setInt (1, Integer.parseInt(TxtId.getText()));
int res = preparedStatement.executeUpdate();
if (res > 0 ){
JOptionPane.showMessageDialog(null,"Aluno Eliminado(a)");
LimparCaixa();
}else{
JOptionPane.showMessageDialog(null, "Erro ao Eliminar ao Alunos");
LimparCaixa();
}
connetion.close();//fim da conexao
} catch (Exception ex) {
System.out.println(ex);
}
}
private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {
LimparCaixa();
}
public void LimparCaixa(){
TxtId.setText(null);
TxtNome.setText(null);
TxtTelefone.setText(null);
TxtChave.setText(null);
TxtNascimento.setText(null);
TxtChave.setEditable(true);
}
private void TxtChaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CadastroJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CadastroJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CadastroJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CadastroJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>//modificado para exibir melhor no windows de Nimbus
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new CadastroJFrame().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JLabel TxrChave;
private javax.swing.JTextField TxtChave;
private javax.swing.JTextField TxtId;
private javax.swing.JTextField TxtNascimento;
private javax.swing.JTextField TxtNome;
private javax.swing.JTextField TxtTelefone;
private javax.swing.JLabel TxtTelefonee;
private javax.swing.JButton btnBuscar;
private javax.swing.JButton btnEliminar;
private javax.swing.JButton btnGuardar;
private javax.swing.JButton btnLimpar;
private javax.swing.JButton btnModificar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollBar jScrollBar1;
// End of variables declaration
private PreparedStatement connetion(int i, String text) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private static class connection {
private static void close() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public connection() {
}
}
}
Erro gerado ao guardar no banco
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/TrabalhoMoacyBD/
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at CadastroAluno.CadastroJFrame.btnGuardarActionPerformed(CadastroJFrame.java:195)
at CadastroAluno.CadastroJFrame.access$400(CadastroJFrame.java:16)
at CadastroAluno.CadastroJFrame$5.actionPerformed(CadastroJFrame.java:92)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
//banco
package CadastroAluno;
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author joniel
*/
public class conection {
/**
*
*/
public static final String ULR = " jdbc:mysql://localhost:3306/TrabalhoMoacyBD/";//BD
public static final String USERNAME = "root";//nome de usuario do BD
public static final String PASSWORD = "toor";//password do BD
public static Connection getConnection(){
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(ULR,USERNAME ,PASSWORD);
}
catch (Exception e) {
System.out.println(e);
}
return connection;
}
void close() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}