Send data to database

Asked

Viewed 51 times

3

I’m studying for a java test on database and I’m very confused, I was making a program based on another, I made a registration screen and it works perfect, the only thing missing is to send and I do not know how to do this look at the example and I can not find.

Code that Cadastra:

 Produtos t = new Produtos();

 try
 {
    t.setMarca(txtMarca.getText());

    t.setNome(txtNome.getText());
    t.setPreco(Float.parseFloat(txtPreco.getText()));
    t.setQuantia(Integer.parseInt(txtQuantidade.getText()));
    JOptionPane.showMessageDialog(this, "Sucesso a cadastrar", 
            "Sucesso" ,JOptionPane.INFORMATION_MESSAGE);       
    Limpar();
}
catch(Exception ex){
    JOptionPane.showMessageDialog(this, "Erro a cadastrar", 
            "Erro" ,JOptionPane.INFORMATION_MESSAGE);               
}

I have a saving method but I don’t understand it and whether this right or not

public void Salvar() throws SQLException
{ 
    try
    {
        conectaBanco conexao = new conectaBanco();
        String sql = ("insert into produtos (nome,marca,preco,quantia) values (?,?,?,?) ");
        try (PreparedStatement stmt = conexao.prepareStatement(sql)) {
           stmt.setString(1, nome);
           stmt.setString(2,marca);
           stmt.setFloat(3, preco);
           stmt.setInt(4, quantia);
           conexao.desconecta();
           stmt.execute();
        }
    }           
    catch(Exception ex)
    {
         System.out.println("Erro");
    }  
 }
  • Hello Carlos, welcome to Stack Overflow. Your question was not very clear to me reading the code. What exactly are you having trouble with? Your registration code is fairly ok. In the save method I would move this desconecta for a clause finally. It is also worth paying attention to Java code conventions (Classes start with upper case, lower case methods), as well as object orientation and design clean (e.g., I would receive a Produto produto as the save method parameter instead of feeding the class directly, and would treat the exceptions).

1 answer

0


Changes the Save method to the form below, as there is a simple error where you are disconnected from the database before running the Insert on it:

public void Salvar() throws SQLException
{ 
    try
    {
        conectaBanco conexao = new conectaBanco();
        String sql = ("insert into produtos (nome,marca,preco,quantia) values (?,?,?,?) ");
        try (PreparedStatement stmt = conexao.prepareStatement(sql)) {
           stmt.setString(1, nome);
           stmt.setString(2,marca);
           stmt.setFloat(3, preco);
           stmt.setInt(4, quantia);
           stmt.execute();
           conexao.desconecta();
        } catch(Exception ex){
            System.out.println("Erro insert.");
        }// finally { //Ou você pode desconectar usando o finally do try
            //conexao.desconecta();
        //}

    }           
    catch(Exception ex)
    {
         System.out.println("Erro");
    }  
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.