OBS: Eu tenho um sistema mais robusto, mas irei mandar o de teste
Tenho Classes Usuario, Secretario, Fornenecedor, etc que irão receber Telefone
Por esse motivo não quis inserir Telefone em cada uma das pessoas
Do jeito que o codigo está, eu consigo salvar em ambas tabelas, mas elas não se conectam, ficam como informações soltas
Outra dúvida que tenho, é vocês acham melhor no meu caso criar uma Classe Pessoa, e as demais herdando características dessa Pessoa?
Segue meu codigo abaixo
package MODELS;
public class ModeloContato {
private int id_cont, id_usu;
private String email_cont;
public int getId_cont() {
return id_cont;
}
public void setId_cont(int id_cont) {
this.id_cont = id_cont;
}
public String getEmail_cont() {
return email_cont;
}
public void setEmail_cont(String email_cont) {
this.email_cont = email_cont;
}
public int getId_usu() {
return id_usu;
}
public void setId_usu(int id_usu) {
this.id_usu = id_usu;
}
}
package CONTROL;
import DATABASE.ConectBD;
import MODELS.ModeloUsuario;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class ControleUsuario {
ConectBD conectausu = new ConectBD();
public void InserirUsuario(ModeloUsuario modusu){
//JOptionPane.showMessageDialog(null, modusu.getNome_usu());
conectausu.conecta();
try{
//PreparedStatement pst = conectausu.conn.prepareStatement("insert into usuario (nome_usu, id_cont, id_perm)values(?,?,?)");
PreparedStatement pst = conectausu.conn.prepareStatement("insert into usuario (nome_usu)values(?)");
pst.setString(1,modusu.getNome_usu());
/*pst.setInt(4,modusu.getCod_cont());
pst.setInt(5,modusu.getCod_perm());*/
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Dados do usuário: "+modusu.getNome_usu()+" armazenados com sucesso!");
}catch(SQLException ex){
JOptionPane.showMessageDialog(null, "Erro ao cadastrar usuario:" + ex);
}
conectausu.desconecta();
}
}
package MODELS;
public class ModeloUsuario {
private int id_usu;
private int id_perm;
private String nome_usu;
public int getId_usu() {
return id_usu;
}
public void setId_usu(int id_usu) {
this.id_usu = id_usu;
}
public int getId_perm() {
return id_perm;
}
public void setId_perm(int id_perm) {
this.id_perm = id_perm;
}
public String getNome_usu() {
return nome_usu;
}
public void setNome_usu(String nome_usu) {
this.nome_usu = nome_usu;
}
}
package CONTROL;
import DATABASE.ConectBD;
import MODELS.ModeloUsuario;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class ControleUsuario {
ConectBD conectausu = new ConectBD();
public void InserirUsuario(ModeloUsuario modusu){
//JOptionPane.showMessageDialog(null, modusu.getNome_usu());
conectausu.conecta();
try{
//PreparedStatement pst = conectausu.conn.prepareStatement("insert into usuario (nome_usu, id_cont, id_perm)values(?,?,?)");
PreparedStatement pst = conectausu.conn.prepareStatement("insert into usuario (nome_usu)values(?)");
pst.setString(1,modusu.getNome_usu());
/*pst.setInt(4,modusu.getCod_cont());
pst.setInt(5,modusu.getCod_perm());*/
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Dados do usuário: "+modusu.getNome_usu()+" armazenados com sucesso!");
}catch(SQLException ex){
JOptionPane.showMessageDialog(null, "Erro ao cadastrar usuario:" + ex);
}
conectausu.desconecta();
}
}
package FORMS;
import CONTROL.ControleUsuario;
import CONTROL.ControleContato;
import DATABASE.ConectBD;
import MODELS.ModeloContato;
import MODELS.ModeloUsuario;
import javax.swing.JOptionPane;
public class CadUsuario extends javax.swing.JFrame {
//ConectBD conectaperm = new ConectBD();
ConectBD conectausu = new ConectBD();
ConectBD conectacont = new ConectBD();
ModeloUsuario modusu = new ModeloUsuario();
ModeloContato modcont = new ModeloContato();
ControleUsuario controlusu = new ControleUsuario();
ControleContato controlcont = new ControleContato();
public CadUsuario() {
initComponents();
//conectaperm.conecta();
conectausu.conecta();
conectacont.conecta();
}
public void salvarcadmodal () {
modusu.setNome_usu(jTextField1.getText());
controlusu.InserirUsuario(modusu);
modcont.setEmail_cont(jTextField2.getText());
controlcont.InserirContato(modcont);
JOptionPane.showMessageDialog(rootPane,"Usuario Cadastrado");
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox<>();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("NOME");
jLabel2.setText("EMAIL");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jButton1.setText("jButton1");
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(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addGap(96, 96, 96)
.addComponent(jButton1)))
.addContainerGap(41, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(19, 19, 19))
);
setSize(new java.awt.Dimension(333, 231));
setLocationRelativeTo(null);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
salvarcadmodal();
}
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 ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CadUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CadUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CadUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CadUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CadUsuario().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}