1
After command insert
, i can recover the id generated by the database using a global variable public static int returnID
. But I didn’t think it was very cool to leave the variable static
. Is there any other way to retrieve this information? Code below:
My canvas:
public class TelaPrincipal extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JPanel panelPrincipal,panelCenter,panelButton;
JLabel idlbl,nomelbl,sexolbl,rglbl,cpflbl,dtnasclbl,dtcadlbl,estcivlbl;
JTextField idField,nomeField,sexoField,rgField,cpfField,dtnascField,dtcadField,estcivField;
JButton btnCad,btnLimpar,btnFechar;
public TelaPrincipal(){
UtilTelas.iniciarTelas(300, 300, "Cadastro de Pessoa", this);
panelPrincipal = new JPanel(new BorderLayout());
this.add(panelPrincipal);
Center();
Button();
this.setVisible(true);
pack();
}
private void Center(){
panelCenter = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel gridLayout = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(3, 3, 3, 3);
int x = 0;
int y = 0;
int fieldX = 0;
int fieldY = 0;
idlbl = new JLabel("Código : ");
gbc.gridx = x;
gbc.gridy = ++y;
gbc.anchor = GridBagConstraints.EAST;
gridLayout.add(idlbl,gbc);
nomelbl = new JLabel("Nome : ");
gbc.gridx = x;
gbc.gridy = ++y;
gbc.anchor = GridBagConstraints.EAST;
gridLayout.add(nomelbl,gbc);
sexolbl = new JLabel("Sexo : ");
gbc.gridx = x;
gbc.gridy = ++y;
gbc.anchor = GridBagConstraints.EAST;
gridLayout.add(sexolbl,gbc);
rglbl = new JLabel("RG : ");
gbc.gridx = x;
gbc.gridy = ++y;
gbc.anchor = GridBagConstraints.EAST;
gridLayout.add(rglbl,gbc);
cpflbl = new JLabel("CPF : ");
gbc.gridx = x;
gbc.gridy = ++y;
gbc.anchor = GridBagConstraints.EAST;
gridLayout.add(cpflbl,gbc);
dtnasclbl = new JLabel("Dt Nasci : ");
gbc.gridx = x;
gbc.gridy = ++y;
gbc.anchor = GridBagConstraints.EAST;
gridLayout.add(dtnasclbl,gbc);
dtcadlbl = new JLabel("Dt Cadastro : ");
gbc.gridx = x;
gbc.gridy = ++y;
gbc.anchor = GridBagConstraints.EAST;
gridLayout.add(dtcadlbl,gbc);
estcivlbl = new JLabel("Estado Civil");
gbc.gridx = x;
gbc.gridy = ++y;
gbc.anchor = GridBagConstraints.EAST;
gridLayout.add(estcivlbl,gbc);
idField = new JTextField(5);
gbc.gridx = ++fieldX;
gbc.gridy = ++fieldY;
gbc.anchor = GridBagConstraints.WEST;
gridLayout.add(idField,gbc);
nomeField = new JTextField(20);
gbc.gridx = fieldX;
gbc.gridy = ++fieldY;
gbc.anchor = GridBagConstraints.WEST;
gridLayout.add(nomeField,gbc);
sexoField = new JTextField(10);
gbc.gridx = fieldX;
gbc.gridy = ++fieldY;
gbc.anchor = GridBagConstraints.WEST;
gridLayout.add(sexoField,gbc);
rgField = new JTextField(10);
gbc.gridx = fieldX;
gbc.gridy = ++fieldY;
gbc.anchor = GridBagConstraints.WEST;
gridLayout.add(rgField,gbc);
cpfField = new JTextField(10);
gbc.gridx = fieldX;
gbc.gridy = ++fieldY;
gbc.anchor = GridBagConstraints.WEST;
gridLayout.add(cpfField,gbc);
dtnascField = new JTextField(10);
gbc.gridx = fieldX;
gbc.gridy = ++fieldY;
gbc.anchor = GridBagConstraints.WEST;
gridLayout.add(dtnascField,gbc);
dtcadField = new JTextField(10);
gbc.gridx = fieldX;
gbc.gridy = ++fieldY;
gbc.anchor = GridBagConstraints.WEST;
gridLayout.add(dtcadField,gbc);
estcivField = new JTextField(10);
gbc.gridx = fieldX;
gbc.gridy = ++fieldY;
gbc.anchor = GridBagConstraints.WEST;
gridLayout.add(estcivField,gbc);
panelCenter.add(gridLayout);
panelPrincipal.add(panelCenter,BorderLayout.CENTER);
}
private void Button(){
panelButton = new JPanel(new FlowLayout());
btnCad = new JButton("Cadastrar");
btnCad.addActionListener(this);
panelButton.add(btnCad);
panelPrincipal.add(panelButton,BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
if(btnCad == e.getSource()){
ControllerPessoa ControlPessoa = new ControllerPessoa();
Pessoa pessoa = new Pessoa();
pessoa.setPes_nome(nomeField.getText());
pessoa.setPes_sexo(sexoField.getText());
try {
if(ControlPessoa.cadastroPessoa(pessoa)){
JOptionPane.showMessageDialog(null, "Cadastroado com Sucesso");
idField.setText(String.valueOf(PessoaDAO.returnID));
}
} catch (HeadlessException erro) {
erro.printStackTrace();
} catch (ClassNotFoundException erro) {
JOptionPane.showMessageDialog(null, "ERRO de DriverManager Banco de Dados" +erro.getMessage());
} catch (SQLException erro) {
JOptionPane.showMessageDialog(null, "ERRO de Banco de Dados" +erro.getMessage());
}
}
}
}
Class ControllerPessoa
:
public class ControllerPessoa {
public boolean cadastroPessoa(Pessoa pessoa) throws ClassNotFoundException, SQLException{
PessoaDAO pessoaDAO = new PessoaDAO();
String sql = "INSERT INTO farmacia.pessoa1 (nome,sexo) VALUES ('"+ pessoa.getPes_nome() +"','"+ pessoa.getPes_sexo() +"')";
if(pessoaDAO.insert(sql)){
return true;
}
return false;
}
}
Class DAO
:
public abstract class DAO {
public abstract boolean insert (String SQL);
public abstract boolean update (String SQL);
public abstract boolean delete (String SQL);
public abstract boolean select (String SQL);
}
Class PessoaDAO
:
public class PessoaDAO extends DAO {
PreparedStatement pstm;
private Connection con;
public static int returnID;
public PessoaDAO() throws ClassNotFoundException, SQLException{
BD db = new BD();
con = db.getConn();
}
@Override
public boolean insert(String SQL) {
try {
pstm = con.prepareStatement(SQL,Statement.RETURN_GENERATED_KEYS);
pstm.execute();
int result = pstm.executeUpdate();
ResultSet rs = pstm.getGeneratedKeys();
if (rs.next()){
returnID = rs.getInt("id"); // teria outro modo de recuperar o id? sem usa static?
JOptionPane.showMessageDialog(null, "ID : "+ returnID);
}
return result > 0;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "ERRO : inserir novo pessoa " + e.getMessage());
}
return false;
}
@Override
public boolean update(String SQL) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean delete(String SQL) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean select(String SQL) {
// TODO Auto-generated method stub
return false;
}
}
Can you just put the code that’s important to ask? It’s hard to keep reviewing code after problems and improvements.
– Renan Gomes
has yes excuse, just take a look at the class Personal is where this variable Static int, through it I recover ID generated by the bank.
– Diego Angelo
Since
Pessoa
is an object, rather than returning aboolean
in the insert method you can return an objectPessoa
(ornull
if something has gone wrong). So, at the time of putting in theJTextField
will only take oneInteger.toString(pessoa.getId());
.– Renan Gomes
One way is to make the method
insert
return an integer in the case of the generated ID. There you do not need the static variable.– Franchesco