본문 바로가기
기술 기록/java

(Eng) error) java, MySQL) Exception : Statement.executeQuery() cannot issue statements that do not produce result sets

by Fola 2022. 3. 16.

Eng) java, MySQL ) Exception :  Statement.executeQuery() cannot issue statements that do not produce result sets


Error message :
Statement.executeQuery() cannot issue statements that do not produce result sets

When I did my first project using MySQL DB, this error made me stuck for a time.

In conclusion, this error is occurred by the incorrect use of java SQL statement syntax.

If you want to execute INSERT, UPDATE, DELETE query(aka DML), you should use .execute() or .executeUpdate().
The otherwise .executeQuery() is fit used for SELECT query not likes INSERT query things.

 


Explain >

There are three kinds of SQL statement method. 
.executeQuery()
.execute()
.executeUpdate()

 


1) .executeQuery()
.execyteQuery() must return ResultSet.
So, a ResultSet object should be on the left side of this method
That is the reason why the SQL Exception occurred.
INSERT query can not produce ResultSet.

2) .executeUpdate()
It returns int type value which is the count of manipulated rows.
If the query is CREATE or DROP, it returns -1.
And it would be OK if there's no return variable.
The query will execute without return. No problem.

3) .execute()
It returns boolean after execute query.
If the result of the query makes ResultSet, it returns true
but it doesn't mean that this query returns ResultSet.
It just confirms that the query can make ResultSet or not. 
It returns false if the query statement is DML; Data Manipulation Language like INSERT or DELETE or UPDATE.
It doesn't have to need a return variable. Similarly .executeUpdate().
Your query will execute well.

 



I hope this post helps someone who starts programming with Database
Also, I hope that safety from Covid-19. 

Finally, attached is my INSERT SQL Java code.

 

import com.folaSmile.apartSearch.databaseModel.ConnectDB;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class InsertLogDAO extends ConnectDB {

    public boolean insertLog(String clientVer) {

        PreparedStatement insertLogQuery = null;

        try {

            connDB();

            insertLogQuery = conn.prepareStatement("""
                    INSERT INTO connect_log (client_ver, connect_time) VALUES (?,NOW())
                     """);

            insertLogQuery.setString(1, clientVer);
            insertLogQuery.execute();

            return true;


        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (insertLogQuery != null) {
                try {
                    insertLogQuery.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


    }

}

댓글