S
S
SankaSanka2019-07-29 11:53:54
MySQL
SankaSanka, 2019-07-29 11:53:54

Why is there an error in sql query?

I am trying to delete a row from a table by id

public void deleteFilmsOlderGivenYears(int givenYears, Statement st) throws SQLException {
        
        for (Film a : films) {                                                                                       //перебираю коллекцию кино
            int dataRelease = Integer.parseInt(a.getDate_release().substring(0, 4));  //формирую дату
            if (dataRelease < givenYears) {                                                                //нахожу нужную дату
              st.executeQuery("DELETE FROM films WHERE id =" + a.getId());         //пытаюсь удалить кино по ID
            }
        }
    }

throws the
following Exception in thread "main" java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at [email protected]/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
at [email protected]/com.mysql.cj.jdbc. exceptions.SQLError.createSQLException(SQLError.java:95)
at [email protected]/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
at [email protected] 8.0.11/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
at [email protected]/com.mysql.cj.jdbc.StatementImpl.checkForDml(StatementImpl.java :390)
at [email protected]/com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1182)
at HomeLibrary.deleteFilmsOlderGivenYears(HomeLibrary.java:135)
at MainStarter.main(MainStarter.java:27 )
and before that, a bunch of methods in this class work perfectly with this base according to the same scheme, getting st. But this one rested ((((
a.getId() is formed normally there int. The problem is in the request to make id string and substitute it in the request also does not help(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Sviridov, 2019-07-29
@SankaSanka

You need to call executeUpdate, not executeQuery. And by the way, what's the point of running database queries in a loop?

K
Konstantin Tsvetkov, 2019-07-29
@tsklab

executeUpdate()V6fjm.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question