ResultSet 不是用于 INSERT 查询吗?错误消息:类型不匹配:无法从 int 转换为 String [英] ResultSet is not for INSERT query? Error message: Type mismatch: cannot convert from int to String
问题描述
我想我在结果集"上弄错了.我插入数据的代码是:
I think I got it wrong at the 'resultset'. My code for inserting data is :
ResultSet columns = statement.executeUpdate("INSERT INTO Feedback (Username, Feedbacks) VALUES( '" + user + "','"+ msg +"')");
编译错误为:
类型不匹配:无法从 int 转换为 String
Type mismatch: cannot convert from int to String
你能帮我吗?
推荐答案
Statement#executeUpdate()
不返回 ResultSet
,而是一个 int
表示受影响的行.修复如下.
Statement#executeUpdate()
doesn't return a ResultSet
, but an int
representing the affected rows. Fix it as follows.
int affectedRows = statement.executeUpdate(sql);
或者,如果您对受影响的行数不感兴趣,请忽略它.
Or if you aren't interested in the amount of affected rows, just ignore it.
statement.executeUpdate(sql);
顺便说一下,编译错误本身很奇怪.你不是看错线了吗?该错误表明您正在尝试将 int
结果分配给 String
,但您的代码显示您正在尝试分配 int
结果到 ResultSet
.
By the way, the compilation error at its own is weird. Aren't you looking at the wrong line? The error tells that you're trying to assign an int
result to a String
, but your code shows that you're trying to assign int
result to a ResultSet
.
也就是说,与您当前的问题无关,如果user
和msg
是用户控制输入的一部分,那么这段代码很容易出现SQL 注入 攻击.我强烈建议将 Statement
替换为 PreparedStatement
然后.另请参阅本教程如何使用准备好的语句.
That said and unrelated to your current problem, if user
and msg
are parts of user-controlled input, then this piece of code is prone to SQL injection attacks. I strongly recommend to replace Statement
by PreparedStatement
then. See also this tutorial how to use prepared statement.
这篇关于ResultSet 不是用于 INSERT 查询吗?错误消息:类型不匹配:无法从 int 转换为 String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!