可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has an answer here:
I am using JCBC API to connect to mySQL server in order to perform some SQL queries (create tables, delete rows, etc.).
However, I receive a "Type Mismatch: Cannot Convert from int to ResultSet" error in the following function:
private static void deleteUnpopularArtists(Statement statement, int min_rank) throws SQLException { String rank = Integer.toString(min_rank); ResultSet resultSet = statement.executeUpdate("DELETE FROM record_artist WHERE high_chart_pos < " + rank + ";"); }
I have tried to use String.valueOf(min_rank); and int rank = min_rank + "";
So my question is, why am I getting this error? What can I do to fix it?
回答1:
executeUpdate returns the number of rows affected in the query. The notion of a ResultSet for a database write operation makes no sense
int rows = statement.executeUpdate ("DELETE FROM record_artist WHERE high_chart_pos < " + rank + ";");
回答2:
1. ExecuteUpdate returns int - for update, insert and delete operations where the result is expected to be int ( Number of rows affected )
2. ExecuteQuery returns resultset - useful for query statements (eg. select * from table) where result type is resultset (data from the table)
3. execute returns boolean - when you do not know what function to use for your query or for unknown dynamic sql use this function. It returns true if result is a resultSet and false if answer is an updateCount
or no results founds
Hope this helps!!!