"Error: Can not Issue data Manipulation statements with executeQuery()" no Select

Asked

Viewed 75 times

2

Doing the method to select all data from a table, but is returning me error. console: Open bank.

java.sql.SQLException: No value specified for parameter 1    
No value specified for parameter 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2176)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2100)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:931)
    at persistence.ProdutoDao.findAll(ProdutoDao.java:27)
    at persistence.ProdutoDao.main(ProdutoDao.java:51)

Method findAll:

    public List<Produto> findAll()throws Exception{
    open();//metodo q abre a conexao com o banco
        stmt = con.prepareStatement(SELECT); //constante select*from produto
        stmt.execute();
        rs = stmt.executeQuery();

        List<Produto> lista = new ArrayList<>();

        while(rs.next()){
            lista.add(new Produto(
                    rs.getInt(1),
                    rs.getString(2),
                    rs.getString(3),
                    rs.getString(4),
                    rs.getDouble(5) 
                    )
            );

        }
        close();//metodo que fecha a conexao com o banco
        return lista;
}
     public static void main(String[] args) {

         try {
            ProdutoDao pd = new ProdutoDao();
            System.out.println(pd.findAll());
         } catch (Exception e) {
           e.printStackTrace();
           e.printStackTrace();
           System.out.println("Nao listou " + e.getMessage());
    }
}
  • Apparently the constant is wrong, she calls INSERT and the code looks like a select.

  • Sorry, I just messed up with the method that comes above it, the constant is SELECT and when it comes to copying here I made the mess. String receives "select * from product;"

1 answer

1

The mistake No value specified for parameter 1 usually occurs when you have a query that expects a certain parameters but this parameters is not being passed.

In the code presented above you entered a constant containing the query:

 stmt = con.prepareStatement(INSERT);

Probably the content of this constant has something like select*from produto where id=?. In this case just do as follows:

stmt = con.prepareStatement("SELECT * FROM produto WHERE id=?"); 
stmt.setLong(1, id);
stmt.execute();
rs = stmt.executeQuery();

Where id is a variable that contains the id to be searched for.

Of course you must adapt to your case, do not know what are the columns and parameters you want to pass.

  • In this case I did in a way that searched all the columns and their data in the table, but as it is giving error I will try as you suggested, thanks!

  • strange because as I said this error usually occurs when the query expects a parameter and the same was not passed.

  • Thanks for your help!

Browser other questions tagged

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