"Error: Can not issue data manipulation statements with executeQuery ()" no Select

2

Doing the method to select all data from a table, however it is returning me error. console: Open bench.

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)

FindAll Method:

    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());
    }
}
    
asked by anonymous 23.09.2016 / 10:27

1 answer

1

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

In the above code you have 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 the following:

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 fetched.

Of course you should adapt to your case, I do not know what columns and parameters you want to pass.

    
23.09.2016 / 14:18