JAVA

JDBC: executeQuery VS excuteUpdate

superbono 2021. 4. 25. 15:07

execute:

어떤 타입의 SQL 문장이든 사용할 수 있으며 boolean 타입의 결과값을 리턴한다. 만약 true가 리턴되었다면 getResultSet으로  결과 값을 받을 수 있으며 false가 리턴되었다면 그 쿼리가 int 값 또는 void를 리턴할 때이다. select와 insert/update 등 모두 사용할 수 있다.

 

boolean execute(String sql) throws SQLException

 

Executes the given SQL statement, which may return multiple results. In some (uncommon) situations, a single SQL statement may return multiple result sets and/or update counts. Normally you can ignore this unless you are (1) executing a stored procedure that you know may return multiple results or (2) you are dynamically executing an unknown SQL string.

The execute method executes an SQL statement and indicates the form of the first result. You must then use the methods getResultSet or getUpdateCount to retrieve the result, and getMoreResults to move to any subsequent result(s).

Note:This method cannot be called on a PreparedStatement or CallableStatement.

Parameters:sql - any SQL statement

Returns:true if the first result is a ResultSet object; false if it is an update count or there are no results

Throws:SQLException - if a database access error occurs, this method is called on a closed Statement, the method is called on a PreparedStatement or CallableStatementSQLTimeoutException - when the driver has determined that the timeout value that was specified by the setQueryTimeout method has been exceeded and has at least attempted to cancel the currently running Statement

 

executeQuery:

executeQuery 메소드는 데이터 베이스에서 가져온 데이터를 resultSet에 담아 그 resultSet을 리턴한다. select 문에서만 사용할 수 있다.

 

ResultSet executeQuery(String sql) throws SQLException

 

Executes the given SQL statement, which returns a single ResultSet object.

Note:This method cannot be called on a PreparedStatement or CallableStatement.

Parameters:sql - an SQL statement to be sent to the database, typically a static SQL SELECT statement

Returns:a ResultSet object that contains the data produced by the given query; never null

 

executeUpdate:

executeUpdate 메소드는 insert/update/delete 등의 문장만 실행할 수 있다. 이 메소드는 int 값을 리턴하는데 이 int 값은 영향을 받은 레코드의 수를 나타낸다(DDL의 경우에). return 0 은 그 쿼리가 아무것도 리턴하지 않음을 나타낸다. select 문에서는 사용할 수 없지만 그 외의 모든 SQL을 실행할 수 있다.  

 

int executeUpdate(String sql) throws SQLException

 

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

Note:This method cannot be called on a PreparedStatement or CallableStatement.

Parameters:sql - an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

Returns:either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing

 

 

출처 : www.javapedia.net/JDBC/1793#:~:text=Difference%20between%20execute%2C%20executeQuery%20and%20executeUpdate%20in%20JDBC.,-

 

Difference between execute, executeQuery and executeUpdate in JDBC.

execute method can be used with any type of SQL statements and it returns a boolean. A true indicates that the execute method returned a result set object which can be retrieved using getResultSet method. false indicates that the query returned an int valu

www.javapedia.net

docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#execute(java.lang.String)

'JAVA' 카테고리의 다른 글

Interface(인터페이스)  (0) 2021.06.30
String과 StringBuilder 비교하기 (String vs StringBuilder)  (0) 2021.06.21
메모리 구조(Stack, Heap)  (0) 2021.06.20
상속(Inheritance)  (0) 2021.01.29
1. 자바 시작하기  (0) 2021.01.29