“Type Mismatch: Cannot Convert from int to ResultSet” [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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!!!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!