"No value specified for Parameter 1" when executing Preparedstatement

Asked

Viewed 2,008 times

2

I’m testing a insert here and the following error appears to me:

Exception in thread "main" java.sql.Sqlexception: No value specified for Parameter 1

Dao

package modelo;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.util.Calendar;

    public class PessoaDao {
        public void insere(PessoaBean pessoa) throws SQLException{  

            // conectando
            Connection con = new ConexaoMysql().getConexao();

            // cria um preparedStatement
            String sql = "insert into exemplo" +
                    " (nome,numero,dataExemplo)" +
                    " values (?,?,?)";
            PreparedStatement stmt = con.prepareStatement(sql);

            // executa
            stmt.execute();
            stmt.close();

            System.out.println("Gravado!");

            con.close();
        }
    }

Bean

package modelo;

import java.util.Calendar;

public class PessoaBean {
    private int Idexemplo;
    private String nome;
    private float numero; 
    private Calendar dataExemplo;

    public int getIdexemplo() {
        return Idexemplo;
    }
    public void setIdexemplo(int idexemplo) {
        Idexemplo = idexemplo;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public float getNumero() {
        return numero;
    }
    public void setNumero(float numero) {
        this.numero = numero;
    }
    public Calendar getDataExemplo() {
        return dataExemplo;
    }
    public void setDataExemplo(Calendar dataExemplo) {
        this.dataExemplo = dataExemplo;
    }
}

Main

package modelo;



import java.sql.Connection;
import java.sql.SQLException;
import java.util.Calendar;


public class Teste {
    public static void main(String[] args) throws SQLException {
        Connection c = new ConexaoMysql().getConexao();
        System.out.println("Conexão aberta!");
        c.close();

         PessoaBean p = new PessoaBean();  

         PessoaDao dao = new PessoaDao();  

          p.setNome("José da Silva");  
          p.setNumero(1232); 
          p.setDataExemplo(Calendar.getInstance());  


          dao.insere(p);  


    }
}

2 answers

4


In this method:

public class PessoaDao {
    public void insere(PessoaBean pessoa) throws SQLException{  

        // conectando
        Connection con = new ConexaoMysql().getConexao();

        // cria um preparedStatement
        String sql = "insert into exemplo" +
                " (nome,numero,dataExemplo)" +
                " values (?,?,?)";
        PreparedStatement stmt = con.prepareStatement(sql);

        // executa
        stmt.execute();
        stmt.close();

        System.out.println("Gravado!");

        con.close();
    }
}

You forgot to put this:

stmt.setString(1, pessoa.getNome());
stmt.setFloat(2, pessoa.getNumero());
stmt.setTimestamp(3, new Timestamp(pessoa.getDataExemplo().getTimeInMillis()));

You have to put this before the stmt.execute();.

Furthermore, it is good practice to use the Try-with-Resources to avoid that in the case of a SQLException, you keep open resources. This way, your method looks like this:

public class PessoaDao {
    public void insere(PessoaBean pessoa) throws SQLException {

        // cria um preparedStatement
        String sql = "insert into exemplo" +
                " (nome,numero,dataExemplo)" +
                " values (?,?,?)";

        // conectando
        try (Connection con = new ConexaoMysql().getConexao();
             PreparedStatement stmt = con.prepareStatement(sql))
        {

            stmt.setString(1, pessoa.getNome());
            stmt.setFloat(2, pessoa.getNumero());
            stmt.setTimestamp(3, new Timestamp(pessoa.getDataExemplo().getTimeInMillis()));

            // executa
            stmt.execute();
        }

        System.out.println("Gravado!");
    }
}
  • Nuss forgot to setar , but blz guy availed there

  • 1

    @Jarwin When it comes to "answers", just comment on the possible problem to the author. Edit only Portuguese errors and markup.

  • @Jarwin Sorry, it was supposed to be a semicolon, not a comma. I already fixed it.

  • Quiet guy ;)

2

It is necessary to inform the values in the Insert, use the set method of the Prepared statement.

String sql = "insert into exemplo (nome,numero,dataExemplo) values (?,?,?)";
PreparedStatement stmt = con.prepareStatement(sql);

stmt.setString(1,  'valor1');
stmt.setString(2,  'valor2');
stmt.setString(3,  'valor3');

Browser other questions tagged

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